Changeset 915
- Timestamp:
- 04/15/03 13:30:57 (22 years ago)
- Location:
- pykota/trunk
- Files:
-
- 7 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/pykota
r914 r915 23 23 # 24 24 # $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 # 25 30 # Revision 1.22 2003/04/15 11:09:04 jalet 26 31 # Small bug was fixed when a printer was never used and its internal … … 111 116 """Class for the PyKota filter.""" 112 117 def __init__(self, username) : 113 PyKotaTool.__init__(self , isfilter=1)118 PyKotaTool.__init__(self) 114 119 self.username = username 115 120 self.requester = openRequester(self.config, self.printername) -
pykota/trunk/bin/repykota
r912 r915 23 23 # 24 24 # $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 # 25 30 # Revision 1.24 2003/04/14 20:05:20 jalet 26 31 # Reversed test. … … 156 161 class RePyKota(PyKotaTool) : 157 162 """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 158 167 def main(self, options) : 159 168 """Print Quota reports generator.""" … … 181 190 print (" " * 43) + (_("Total : %9i") % total) 182 191 printerpagecounter = self.storage.getPrinterPageCounter(printerid) 183 if printerpagecounter is None : 192 try : 193 msg = "%9i" % printerpagecounter["pagecounter"] 194 except TypeError : 184 195 msg = _("unknown") 185 else :186 msg = "%9i" % printerpagecounter["pagecounter"]187 196 print (" " * 44) + (_("Real : %s") % msg) 188 197 print -
pykota/trunk/initscripts/pykota-postgresql.sql
r911 r915 20 20 -- 21 21 -- $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 -- 22 27 -- Revision 1.12 2003/04/14 20:01:02 jalet 23 28 -- Typo … … 99 104 -- 100 105 CREATE 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'); 102 108 103 109 -- -
pykota/trunk/NEWS
r913 r915 71 71 - A small display bug in repykota was introduced in preliminary 72 72 1.03 versions, and fixed later on. 73 74 - Some minor bugs which happened in rare situations were fixed. 73 75 74 76 - 1.02 : -
pykota/trunk/pykota/storages/postgresql.py
r873 r915 21 21 # 22 22 # $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 # 23 28 # Revision 1.6 2003/03/29 13:45:27 jalet 24 29 # GPL paragraphs were incorrectly (from memory) copied into the sources. … … 82 87 def doQuery(self, query) : 83 88 """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;") 84 95 try : 85 re turnself.database.query(query)96 result = self.database.query(query) 86 97 except pg.error, msg : 98 self.database.query("ROLLBACK;") 87 99 raise PyKotaStorageError, msg 100 else : 101 self.database.query("COMMIT;") 102 return result 88 103 89 104 def doQuote(self, field) : -
pykota/trunk/pykota/storages/sql.py
r900 r915 21 21 # 22 22 # $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 # 23 28 # Revision 1.22 2003/04/10 21:47:20 jalet 24 29 # Job history added. Upgrade script neutralized for now ! … … 107 112 # but we don't because other storages semantics may be different, so every 108 113 # 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") 110 115 result = self.doParseResult(result) 111 116 if result is not None : … … 117 122 def getPrinterId(self, printername) : 118 123 """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 125 143 def getUserId(self, username) : 126 144 """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)) 128 146 try : 129 147 return self.doParseResult(result)[0]["id"] … … 133 151 def getGroupId(self, groupname) : 134 152 """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)) 136 154 try : 137 155 return self.doParseResult(result)[0]["id"] … … 141 159 def getJobHistoryId(self, jobid, userid, printerid) : 142 160 """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))) 144 162 try : 145 163 return self.doParseResult(result)[0]["id"] … … 149 167 def getPrinterUsers(self, printerid) : 150 168 """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)) 152 170 result = self.doParseResult(result) 153 171 if result is None : … … 158 176 def getPrinterGroups(self, printerid) : 159 177 """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)) 161 179 result = self.doParseResult(result) 162 180 if result is None : … … 167 185 def addPrinter(self, printername) : 168 186 """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)) 170 188 return self.getPrinterId(printername) 171 189 172 190 def addUser(self, username) : 173 191 """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)) 175 193 return self.getUserId(username) 176 194 177 195 def addGroup(self, groupname) : 178 196 """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)) 180 198 return self.getGroupId(groupname) 181 199 … … 185 203 if userid is None : 186 204 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))) 188 206 return (userid, printerid) 189 207 … … 193 211 if groupid is None : 194 212 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))) 196 214 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)) 197 241 198 242 def setUserPQuota(self, userid, printerid, softlimit, hardlimit) : 199 243 """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))) 201 245 202 246 def resetUserPQuota(self, userid, printerid) : 203 247 """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))) 205 249 206 250 def updateUserPQuota(self, userid, printerid, pagecount) : 207 251 """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) 209 257 210 258 def getUserPQuota(self, userid, printerid) : 211 259 """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))) 213 261 try : 214 262 return self.doParseResult(result)[0] … … 218 266 def setUserDateLimit(self, userid, printerid, datelimit) : 219 267 """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))) 221 269 222 270 def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) : 223 271 """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))) 225 273 return self.getJobHistoryId(jobid, userid, printerid) # in case jobid is not sufficient 226 274 … … 231 279 def getPrinterPageCounter(self, printerid) : 232 280 """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)) 234 282 try : 235 283 return self.doParseResult(result)[0] … … 237 285 return 238 286 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 21 21 # 22 22 # $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 # 23 28 # Revision 1.30 2003/04/10 21:47:20 jalet 24 29 # Job history added. Upgrade script neutralized for now ! … … 153 158 class PyKotaTool : 154 159 """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__)) : 156 161 """Initializes the command line tool.""" 157 162 # locale stuff … … 166 171 self.config = config.PyKotaConfig("/etc") 167 172 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) 169 174 self.printername = os.environ.get("PRINTER", None) 170 175 self.smtpserver = self.config.getSMTPServer()