Changeset 915 for pykota

Show
Ignore:
Timestamp:
04/15/03 13:30:57 (22 years ago)
Author:
jalet
Message:

More work done on money print charging.
Minor bugs corrected.
All tools now access to the storage as priviledged users, repykota excepted.

Location:
pykota/trunk
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pykota

    r914 r915  
    2323# 
    2424# $Log$ 
     25# Revision 1.23  2003/04/15 11:30:57  jalet 
     26# More work done on money print charging. 
     27# Minor bugs corrected. 
     28# All tools now access to the storage as priviledged users, repykota excepted. 
     29# 
    2530# Revision 1.22  2003/04/15 11:09:04  jalet 
    2631# Small bug was fixed when a printer was never used and its internal 
     
    111116    """Class for the PyKota filter.""" 
    112117    def __init__(self, username) : 
    113         PyKotaTool.__init__(self, isfilter=1) 
     118        PyKotaTool.__init__(self) 
    114119        self.username = username 
    115120        self.requester = openRequester(self.config, self.printername) 
  • pykota/trunk/bin/repykota

    r912 r915  
    2323# 
    2424# $Log$ 
     25# Revision 1.25  2003/04/15 11:30:57  jalet 
     26# More work done on money print charging. 
     27# Minor bugs corrected. 
     28# All tools now access to the storage as priviledged users, repykota excepted. 
     29# 
    2530# Revision 1.24  2003/04/14 20:05:20  jalet 
    2631# Reversed test. 
     
    156161class RePyKota(PyKotaTool) :         
    157162    """A class for repykota.""" 
     163    def __init__(self, doc) : 
     164        """Initializes the tool as a non-priviledged storage user.""" 
     165        PyKotaTool.__init__(self, asadmin=0, doc=doc) 
     166         
    158167    def main(self, options) : 
    159168        """Print Quota reports generator.""" 
     
    181190                print (" " * 43) + (_("Total : %9i") % total) 
    182191            printerpagecounter = self.storage.getPrinterPageCounter(printerid) 
    183             if printerpagecounter is None :     
     192            try : 
     193                msg = "%9i" % printerpagecounter["pagecounter"] 
     194            except TypeError :      
    184195                msg = _("unknown") 
    185             else : 
    186                 msg = "%9i" % printerpagecounter["pagecounter"] 
    187196            print (" " * 44) + (_("Real : %s") % msg) 
    188197            print         
  • pykota/trunk/initscripts/pykota-postgresql.sql

    r911 r915  
    2020-- 
    2121-- $Log$ 
     22-- Revision 1.13  2003/04/15 11:30:57  jalet 
     23-- More work done on money print charging. 
     24-- Minor bugs corrected. 
     25-- All tools now access to the storage as priviledged users, repykota excepted. 
     26-- 
    2227-- Revision 1.12  2003/04/14 20:01:02  jalet 
    2328-- Typo 
     
    99104-- 
    100105CREATE TABLE groups(id SERIAL PRIMARY KEY NOT NULL, 
    101                     groupname TEXT UNIQUE NOT NULL); 
     106                    groupname TEXT UNIQUE NOT NULL, 
     107                    limitby TEXT DEFAULT 'quota'); 
    102108                     
    103109-- 
  • pykota/trunk/NEWS

    r913 r915  
    7171        - A small display bug in repykota was introduced in preliminary  
    7272          1.03 versions, and fixed later on. 
     73           
     74        - Some minor bugs which happened in rare situations were fixed. 
    7375         
    7476    - 1.02 : 
  • pykota/trunk/pykota/storages/postgresql.py

    r873 r915  
    2121# 
    2222# $Log$ 
     23# Revision 1.7  2003/04/15 11:30:57  jalet 
     24# More work done on money print charging. 
     25# Minor bugs corrected. 
     26# All tools now access to the storage as priviledged users, repykota excepted. 
     27# 
    2328# Revision 1.6  2003/03/29 13:45:27  jalet 
    2429# GPL paragraphs were incorrectly (from memory) copied into the sources. 
     
    8287    def doQuery(self, query) : 
    8388        """Does a query.""" 
     89        if type(query) in (type([]), type(())) : 
     90            query = ";".join(query) 
     91        query = query.strip()     
     92        if not query.endswith(';') :     
     93            query += ';' 
     94        self.database.query("BEGIN;") 
    8495        try : 
    85             return self.database.query(query) 
     96            result = self.database.query(query) 
    8697        except pg.error, msg :     
     98            self.database.query("ROLLBACK;") 
    8799            raise PyKotaStorageError, msg 
     100        else :     
     101            self.database.query("COMMIT;") 
     102            return result 
    88103         
    89104    def doQuote(self, field) : 
  • pykota/trunk/pykota/storages/sql.py

    r900 r915  
    2121# 
    2222# $Log$ 
     23# Revision 1.23  2003/04/15 11:30:57  jalet 
     24# More work done on money print charging. 
     25# Minor bugs corrected. 
     26# All tools now access to the storage as priviledged users, repykota excepted. 
     27# 
    2328# Revision 1.22  2003/04/10 21:47:20  jalet 
    2429# Job history added. Upgrade script neutralized for now ! 
     
    107112        # but we don't because other storages semantics may be different, so every 
    108113        # storage should use fnmatch to match patterns and be storage agnostic 
    109         result = self.doQuery("SELECT id, printername FROM printers;") 
     114        result = self.doQuery("SELECT id, printername FROM printers") 
    110115        result = self.doParseResult(result) 
    111116        if result is not None : 
     
    117122    def getPrinterId(self, printername) :         
    118123        """Returns a printerid given a printername.""" 
    119         result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) 
    120         try : 
    121             return self.doParseResult(result)[0]["id"] 
    122         except TypeError :      # Not found     
    123             return 
    124              
     124        result = self.doQuery("SELECT id FROM printers WHERE printername=%s" % self.doQuote(printername)) 
     125        try : 
     126            return self.doParseResult(result)[0]["id"] 
     127        except TypeError :      # Not found     
     128            return 
     129             
     130    def getPrinterPrices(self, printerid) :         
     131        """Returns a printer prices per page and per job given a printerid.""" 
     132        result = self.doQuery("SELECT priceperpage, priceperjob FROM printers WHERE id=%s" % self.doQuote(printerid)) 
     133        try : 
     134            printerprices = self.doParseResult(result)[0] 
     135            return (printerprices["priceperpage"], printerprices["priceperjob"]) 
     136        except TypeError :      # Not found     
     137            return 
     138             
     139    def setPrinterPrices(self, printerid, perpage, perjob) : 
     140        """Sets prices per job and per page for a given printer.""" 
     141        self.doQuery("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(perpage), self.doQuote(perjob), self.doQuote(printerid))) 
     142     
    125143    def getUserId(self, username) : 
    126144        """Returns a userid given a username.""" 
    127         result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) 
     145        result = self.doQuery("SELECT id FROM users WHERE username=%s" % self.doQuote(username)) 
    128146        try : 
    129147            return self.doParseResult(result)[0]["id"] 
     
    133151    def getGroupId(self, groupname) : 
    134152        """Returns a groupid given a grupname.""" 
    135         result = self.doQuery("SELECT id FROM groups WHERE groupname=%s;" % self.doQuote(groupname)) 
     153        result = self.doQuery("SELECT id FROM groups WHERE groupname=%s" % self.doQuote(groupname)) 
    136154        try : 
    137155            return self.doParseResult(result)[0]["id"] 
     
    141159    def getJobHistoryId(self, jobid, userid, printerid) :         
    142160        """Returns the history line's id given a (jobid, userid, printerid).""" 
    143         result = self.doQuery("SELECT id FROM jobhistory WHERE jobid=%s AND userid=%s AND printerid=%s;" % (self.doQuote(jobid), self.doQuote(userid), self.doQuote(printerid))) 
     161        result = self.doQuery("SELECT id FROM jobhistory WHERE jobid=%s AND userid=%s AND printerid=%s" % (self.doQuote(jobid), self.doQuote(userid), self.doQuote(printerid))) 
    144162        try : 
    145163            return self.doParseResult(result)[0]["id"] 
     
    149167    def getPrinterUsers(self, printerid) :         
    150168        """Returns the list of usernames which uses a given printer.""" 
    151         result = self.doQuery("SELECT DISTINCT id, username FROM users WHERE id IN (SELECT userid FROM userpquota WHERE printerid=%s) ORDER BY username;" % self.doQuote(printerid)) 
     169        result = self.doQuery("SELECT DISTINCT id, username FROM users WHERE id IN (SELECT userid FROM userpquota WHERE printerid=%s) ORDER BY username" % self.doQuote(printerid)) 
    152170        result = self.doParseResult(result) 
    153171        if result is None : 
     
    158176    def getPrinterGroups(self, printerid) :         
    159177        """Returns the list of groups which uses a given printer.""" 
    160         result = self.doQuery("SELECT DISTINCT id, groupname FROM groups WHERE id IN (SELECT groupid FROM grouppquota WHERE printerid=%s);" % self.doQuote(printerid)) 
     178        result = self.doQuery("SELECT DISTINCT id, groupname FROM groups WHERE id IN (SELECT groupid FROM grouppquota WHERE printerid=%s)" % self.doQuote(printerid)) 
    161179        result = self.doParseResult(result) 
    162180        if result is None : 
     
    167185    def addPrinter(self, printername) :         
    168186        """Adds a printer to the quota storage, returns its id.""" 
    169         self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) 
     187        self.doQuery("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername)) 
    170188        return self.getPrinterId(printername) 
    171189         
    172190    def addUser(self, username) :         
    173191        """Adds a user to the quota storage, returns its id.""" 
    174         self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) 
     192        self.doQuery("INSERT INTO users (username) VALUES (%s)" % self.doQuote(username)) 
    175193        return self.getUserId(username) 
    176194         
    177195    def addGroup(self, groupname) :         
    178196        """Adds a group to the quota storage, returns its id.""" 
    179         self.doQuery("INSERT INTO groups (groupname) VALUES (%s);" % self.doQuote(groupname)) 
     197        self.doQuery("INSERT INTO groups (groupname) VALUES (%s)" % self.doQuote(groupname)) 
    180198        return self.getGroupId(groupname) 
    181199         
     
    185203        if userid is None :     
    186204            userid = self.addUser(username) 
    187         self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) 
     205        self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(userid), self.doQuote(printerid))) 
    188206        return (userid, printerid) 
    189207         
     
    193211        if groupid is None :     
    194212            groupid = self.addUser(groupname) 
    195         self.doQuery("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s);" % (self.doQuote(groupid), self.doQuote(printerid))) 
     213        self.doQuery("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(groupid), self.doQuote(printerid))) 
    196214        return (groupid, printerid) 
     215         
     216    def increaseUserBalance(self, userid, amount) :     
     217        """Increases (or decreases) an user's account balance by a given amount.""" 
     218        self.doQuery("UPDATE users SET balance=balance+(%s), lifetimepaid=lifetimepaid+(%s) WHERE id=%s" % (self.doQuote(amount), self.doQuote(amount), self.doQuote(userid))) 
     219         
     220    def getUserBalance(self, userid) :     
     221        """Returns the current account balance for a given user.""" 
     222        result = self.doQuery("SELECT balance FROM users WHERE id=%s" % self.doQuote(userid)) 
     223        try : 
     224            return self.doParseResult(result)[0]["balance"] 
     225        except TypeError :      # Not found     
     226            return 
     227         
     228    def setUserBalance(self, userid, balance) :     
     229        """Sets the account balance for a given user to a fixed value.""" 
     230        current = self.getUserBalance(userid) 
     231        difference = balance - current 
     232        self.increaseUserBalance(userid, difference) 
     233         
     234    def limitUserByQuota(self, userid) :     
     235        """Limits a given user based on print quota.""" 
     236        self.doQuery("UPDATE users SET limitby='quota' WHERE id=%s" % self.doQuote(userid)) 
     237         
     238    def limitUserByBalance(self, userid) :     
     239        """Limits a given user based on account balance.""" 
     240        self.doQuery("UPDATE users SET limitby='balance' WHERE id=%s" % self.doQuote(userid)) 
    197241         
    198242    def setUserPQuota(self, userid, printerid, softlimit, hardlimit) : 
    199243        """Sets soft and hard limits for a user quota on a specific printer given (userid, printerid).""" 
    200         self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userid), self.doQuote(printerid))) 
     244        self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userid), self.doQuote(printerid))) 
    201245         
    202246    def resetUserPQuota(self, userid, printerid) :     
    203247        """Resets the page counter to zero for a user on a printer. Life time page counter is kept unchanged.""" 
    204         self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) 
     248        self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s" % (self.doQuote(userid), self.doQuote(printerid))) 
    205249         
    206250    def updateUserPQuota(self, userid, printerid, pagecount) : 
    207251        """Updates the used user Quota information given (userid, printerid) and a job size in pages.""" 
    208         self.doQuery("UPDATE userpquota SET lifepagecounter=lifepagecounter+(%s), pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(pagecount), self.doQuote(userid), self.doQuote(printerid))) 
     252        jobprice = self.computePrinterJobPrice(printerid, pagecount) 
     253        queries = []     
     254        queries.append("UPDATE userpquota SET lifepagecounter=lifepagecounter+(%s), pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s" % (self.doQuote(pagecount), self.doQuote(pagecount), self.doQuote(userid), self.doQuote(printerid))) 
     255        queries.append("UPDATE users SET balance=balance-(%s) WHERE id=%s" % (self.doQuote(jobprice), self.doQuote(userid))) 
     256        self.doQuery(queries) 
    209257         
    210258    def getUserPQuota(self, userid, printerid) : 
    211259        """Returns the Print Quota information for a given (userid, printerid).""" 
    212         result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) 
     260        result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s" % (self.doQuote(userid), self.doQuote(printerid))) 
    213261        try : 
    214262            return self.doParseResult(result)[0] 
     
    218266    def setUserDateLimit(self, userid, printerid, datelimit) : 
    219267        """Sets the limit date for a soft limit to become an hard one given (userid, printerid).""" 
    220         self.doQuery("UPDATE userpquota SET datelimit=%s::TIMESTAMP WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(userid), self.doQuote(printerid))) 
     268        self.doQuery("UPDATE userpquota SET datelimit=%s::TIMESTAMP WHERE userid=%s AND printerid=%s" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(userid), self.doQuote(printerid))) 
    221269         
    222270    def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) : 
    223271        """Adds a job to the history: (jobid, userid, printerid, last page counter taken from requester).""" 
    224         self.doQuery("INSERT INTO jobhistory (jobid, userid, printerid, pagecounter, action) VALUES (%s, %s, %s, %s, %s);" % (self.doQuote(jobid), self.doQuote(userid), self.doQuote(printerid), self.doQuote(pagecounter), self.doQuote(action))) 
     272        self.doQuery("INSERT INTO jobhistory (jobid, userid, printerid, pagecounter, action) VALUES (%s, %s, %s, %s, %s)" % (self.doQuote(jobid), self.doQuote(userid), self.doQuote(printerid), self.doQuote(pagecounter), self.doQuote(action))) 
    225273        return self.getJobHistoryId(jobid, userid, printerid) # in case jobid is not sufficient 
    226274     
     
    231279    def getPrinterPageCounter(self, printerid) : 
    232280        """Returns the last page counter value for a printer given its id, also returns last username, last jobid and history line id.""" 
    233         result = self.doQuery("SELECT jobhistory.id, jobid, userid, username, pagecounter FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1;" % self.doQuote(printerid)) 
     281        result = self.doQuery("SELECT jobhistory.id, jobid, userid, username, pagecounter FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printerid)) 
    234282        try : 
    235283            return self.doParseResult(result)[0] 
     
    237285            return 
    238286         
     287    def computePrinterJobPrice(self, printerid, jobsize) :     
     288        """Returns the price for a job on a given printer.""" 
     289        prices = self.getPrinterPrices(printerid) 
     290        if prices is None : 
     291            perpage = perjob = 0.0 
     292        else :     
     293            (perpage, perjob) = prices 
     294        return perjob + (perpage * jobsize) 
  • pykota/trunk/pykota/tool.py

    r900 r915  
    2121# 
    2222# $Log$ 
     23# Revision 1.31  2003/04/15 11:30:57  jalet 
     24# More work done on money print charging. 
     25# Minor bugs corrected. 
     26# All tools now access to the storage as priviledged users, repykota excepted. 
     27# 
    2328# Revision 1.30  2003/04/10 21:47:20  jalet 
    2429# Job history added. Upgrade script neutralized for now ! 
     
    153158class PyKotaTool :     
    154159    """Base class for all PyKota command line tools.""" 
    155     def __init__(self, isfilter=0, doc="PyKota %s (c) 2003 %s" % (version.__version__, version.__author__)) : 
     160    def __init__(self, asadmin=1, doc="PyKota %s (c) 2003 %s" % (version.__version__, version.__author__)) : 
    156161        """Initializes the command line tool.""" 
    157162        # locale stuff 
     
    166171        self.config = config.PyKotaConfig("/etc") 
    167172        self.logger = logger.openLogger(self.config) 
    168         self.storage = storage.openConnection(self.config, asadmin=(not isfilter)) 
     173        self.storage = storage.openConnection(self.config, asadmin=asadmin) 
    169174        self.printername = os.environ.get("PRINTER", None) 
    170175        self.smtpserver = self.config.getSMTPServer()