[695] | 1 | # PyKota |
---|
| 2 | # |
---|
| 3 | # PyKota : Print Quotas for CUPS |
---|
| 4 | # |
---|
| 5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 6 | # This program is free software; you can redistribute it and/or modify |
---|
| 7 | # it under the terms of the GNU General Public License as published by |
---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | # (at your option) any later version. |
---|
[695] | 10 | # |
---|
[873] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
| 17 | # along with this program; if not, write to the Free Software |
---|
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
[695] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
| 22 | # $Log$ |
---|
[929] | 23 | # Revision 1.28 2003/04/17 09:26:21 jalet |
---|
| 24 | # repykota now reports account balances too. |
---|
| 25 | # |
---|
[927] | 26 | # Revision 1.27 2003/04/16 12:35:49 jalet |
---|
| 27 | # Groups quota work now ! |
---|
| 28 | # |
---|
[925] | 29 | # Revision 1.26 2003/04/16 08:53:14 jalet |
---|
| 30 | # Printing can now be limited either by user's account balance or by |
---|
| 31 | # page quota (the default). Quota report doesn't include account balance |
---|
| 32 | # yet, though. |
---|
| 33 | # |
---|
[921] | 34 | # Revision 1.25 2003/04/15 21:58:33 jalet |
---|
| 35 | # edpykota now accepts a --delete option. |
---|
| 36 | # Preparation to allow edpykota to accept much more command line options |
---|
| 37 | # (WARNING : docstring is OK, but code isn't !) |
---|
| 38 | # |
---|
[917] | 39 | # Revision 1.24 2003/04/15 13:55:28 jalet |
---|
| 40 | # Options --limitby and --balance added to edpykota |
---|
| 41 | # |
---|
[915] | 42 | # Revision 1.23 2003/04/15 11:30:57 jalet |
---|
| 43 | # More work done on money print charging. |
---|
| 44 | # Minor bugs corrected. |
---|
| 45 | # All tools now access to the storage as priviledged users, repykota excepted. |
---|
| 46 | # |
---|
[900] | 47 | # Revision 1.22 2003/04/10 21:47:20 jalet |
---|
| 48 | # Job history added. Upgrade script neutralized for now ! |
---|
| 49 | # |
---|
[887] | 50 | # Revision 1.21 2003/04/08 20:38:08 jalet |
---|
| 51 | # The last job Id is saved now for each printer, this will probably |
---|
| 52 | # allow other accounting methods in the future. |
---|
| 53 | # |
---|
[873] | 54 | # Revision 1.20 2003/03/29 13:45:27 jalet |
---|
| 55 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 56 | # Two README files were added. |
---|
| 57 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 58 | # |
---|
[812] | 59 | # Revision 1.19 2003/02/27 08:41:49 jalet |
---|
| 60 | # DATETIME is not supported anymore in PostgreSQL 7.3 it seems, but |
---|
| 61 | # TIMESTAMP is. |
---|
| 62 | # |
---|
[791] | 63 | # Revision 1.18 2003/02/10 12:07:31 jalet |
---|
| 64 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 65 | # |
---|
[784] | 66 | # Revision 1.17 2003/02/10 08:41:36 jalet |
---|
| 67 | # edpykota's --reset command line option resets the limit date too. |
---|
| 68 | # |
---|
[768] | 69 | # Revision 1.16 2003/02/08 22:39:46 jalet |
---|
| 70 | # --reset command line option added |
---|
| 71 | # |
---|
[765] | 72 | # Revision 1.15 2003/02/08 22:12:09 jalet |
---|
| 73 | # Life time counter for users and groups added. |
---|
| 74 | # |
---|
[748] | 75 | # Revision 1.14 2003/02/07 22:13:13 jalet |
---|
| 76 | # Perhaps edpykota is now able to add printers !!! Oh, stupid me ! |
---|
| 77 | # |
---|
[732] | 78 | # Revision 1.13 2003/02/07 00:08:52 jalet |
---|
| 79 | # Typos |
---|
| 80 | # |
---|
[729] | 81 | # Revision 1.12 2003/02/06 23:20:03 jalet |
---|
| 82 | # warnpykota doesn't need any user/group name argument, mimicing the |
---|
| 83 | # warnquota disk quota tool. |
---|
| 84 | # |
---|
[723] | 85 | # Revision 1.11 2003/02/06 15:05:13 jalet |
---|
| 86 | # self was forgotten |
---|
| 87 | # |
---|
[722] | 88 | # Revision 1.10 2003/02/06 15:03:11 jalet |
---|
| 89 | # added a method to set the limit date |
---|
| 90 | # |
---|
[721] | 91 | # Revision 1.9 2003/02/06 14:52:35 jalet |
---|
| 92 | # Forgotten import |
---|
| 93 | # |
---|
[720] | 94 | # Revision 1.8 2003/02/06 14:49:04 jalet |
---|
| 95 | # edpykota should be ok now |
---|
| 96 | # |
---|
[719] | 97 | # Revision 1.7 2003/02/06 14:28:59 jalet |
---|
| 98 | # edpykota should be ok, minus some typos |
---|
| 99 | # |
---|
[713] | 100 | # Revision 1.6 2003/02/06 09:19:02 jalet |
---|
| 101 | # More robust behavior (hopefully) when the user or printer is not managed |
---|
| 102 | # correctly by the Quota System : e.g. cupsFilter added in ppd file, but |
---|
| 103 | # printer and/or user not 'yet?' in storage. |
---|
| 104 | # |
---|
[708] | 105 | # Revision 1.5 2003/02/05 23:26:22 jalet |
---|
| 106 | # Incorrect handling of grace delay |
---|
| 107 | # |
---|
[706] | 108 | # Revision 1.4 2003/02/05 23:02:10 jalet |
---|
| 109 | # Typo |
---|
| 110 | # |
---|
[705] | 111 | # Revision 1.3 2003/02/05 23:00:12 jalet |
---|
| 112 | # Forgotten import |
---|
| 113 | # Bad datetime conversion |
---|
| 114 | # |
---|
[700] | 115 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
| 116 | # More robust storage |
---|
| 117 | # |
---|
[695] | 118 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 119 | # Initial import into CVS |
---|
| 120 | # |
---|
| 121 | # |
---|
| 122 | # |
---|
| 123 | |
---|
[721] | 124 | import fnmatch |
---|
| 125 | |
---|
[695] | 126 | class SQLStorage : |
---|
[719] | 127 | def getMatchingPrinters(self, printerpattern) : |
---|
[900] | 128 | """Returns the list of all printers as tuples (id, name) for printer names which match a certain pattern.""" |
---|
[719] | 129 | printerslist = [] |
---|
| 130 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
| 131 | # but we don't because other storages semantics may be different, so every |
---|
| 132 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
[915] | 133 | result = self.doQuery("SELECT id, printername FROM printers") |
---|
[720] | 134 | result = self.doParseResult(result) |
---|
[719] | 135 | if result is not None : |
---|
| 136 | for printer in result : |
---|
| 137 | if fnmatch.fnmatchcase(printer["printername"], printerpattern) : |
---|
[900] | 138 | printerslist.append((printer["id"], printer["printername"])) |
---|
[719] | 139 | return printerslist |
---|
| 140 | |
---|
[900] | 141 | def getPrinterId(self, printername) : |
---|
| 142 | """Returns a printerid given a printername.""" |
---|
[915] | 143 | result = self.doQuery("SELECT id FROM printers WHERE printername=%s" % self.doQuote(printername)) |
---|
[900] | 144 | try : |
---|
| 145 | return self.doParseResult(result)[0]["id"] |
---|
| 146 | except TypeError : # Not found |
---|
| 147 | return |
---|
| 148 | |
---|
[915] | 149 | def getPrinterPrices(self, printerid) : |
---|
| 150 | """Returns a printer prices per page and per job given a printerid.""" |
---|
| 151 | result = self.doQuery("SELECT priceperpage, priceperjob FROM printers WHERE id=%s" % self.doQuote(printerid)) |
---|
| 152 | try : |
---|
| 153 | printerprices = self.doParseResult(result)[0] |
---|
| 154 | return (printerprices["priceperpage"], printerprices["priceperjob"]) |
---|
| 155 | except TypeError : # Not found |
---|
| 156 | return |
---|
| 157 | |
---|
| 158 | def setPrinterPrices(self, printerid, perpage, perjob) : |
---|
| 159 | """Sets prices per job and per page for a given printer.""" |
---|
| 160 | self.doQuery("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(perpage), self.doQuote(perjob), self.doQuote(printerid))) |
---|
| 161 | |
---|
[695] | 162 | def getUserId(self, username) : |
---|
[719] | 163 | """Returns a userid given a username.""" |
---|
[915] | 164 | result = self.doQuery("SELECT id FROM users WHERE username=%s" % self.doQuote(username)) |
---|
[700] | 165 | try : |
---|
| 166 | return self.doParseResult(result)[0]["id"] |
---|
| 167 | except TypeError : # Not found |
---|
| 168 | return |
---|
[695] | 169 | |
---|
[900] | 170 | def getGroupId(self, groupname) : |
---|
| 171 | """Returns a groupid given a grupname.""" |
---|
[915] | 172 | result = self.doQuery("SELECT id FROM groups WHERE groupname=%s" % self.doQuote(groupname)) |
---|
[700] | 173 | try : |
---|
| 174 | return self.doParseResult(result)[0]["id"] |
---|
[900] | 175 | except TypeError : # Not found |
---|
[700] | 176 | return |
---|
[695] | 177 | |
---|
[900] | 178 | def getJobHistoryId(self, jobid, userid, printerid) : |
---|
| 179 | """Returns the history line's id given a (jobid, userid, printerid).""" |
---|
[915] | 180 | 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))) |
---|
[700] | 181 | try : |
---|
[900] | 182 | return self.doParseResult(result)[0]["id"] |
---|
| 183 | except TypeError : # Not found |
---|
[700] | 184 | return |
---|
[900] | 185 | |
---|
| 186 | def getPrinterUsers(self, printerid) : |
---|
| 187 | """Returns the list of usernames which uses a given printer.""" |
---|
[915] | 188 | 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)) |
---|
[900] | 189 | result = self.doParseResult(result) |
---|
| 190 | if result is None : |
---|
| 191 | return [] |
---|
| 192 | else : |
---|
| 193 | return [(record["id"], record["username"]) for record in result] |
---|
[695] | 194 | |
---|
[900] | 195 | def getPrinterGroups(self, printerid) : |
---|
| 196 | """Returns the list of groups which uses a given printer.""" |
---|
[915] | 197 | result = self.doQuery("SELECT DISTINCT id, groupname FROM groups WHERE id IN (SELECT groupid FROM grouppquota WHERE printerid=%s)" % self.doQuote(printerid)) |
---|
[900] | 198 | result = self.doParseResult(result) |
---|
| 199 | if result is None : |
---|
| 200 | return [] |
---|
| 201 | else : |
---|
| 202 | return [(record["id"], record["groupname"]) for record in result] |
---|
[695] | 203 | |
---|
[927] | 204 | def getGroupMembersNames(self, groupname) : |
---|
| 205 | """Returns the list of user's names which are member of this group.""" |
---|
| 206 | groupid = self.getGroupId(groupname) |
---|
| 207 | if groupid is None : |
---|
| 208 | return [] |
---|
| 209 | else : |
---|
| 210 | result = self.doQuery("SELECT DISTINCT username FROM users WHERE id IN (SELECT userid FROM groupsmembers WHERE groupid=%s)" % self.doQuote(groupid)) |
---|
| 211 | return [record["username"] for record in (self.doParseResult(result) or [])] |
---|
| 212 | |
---|
| 213 | def getUserGroupsNames(self, userid) : |
---|
| 214 | """Returns the list of groups' names the user is a member of.""" |
---|
| 215 | result = self.doQuery("SELECT DISTINCT groupname FROM groups WHERE id IN (SELECT groupid FROM groupsmembers WHERE userid=%s)" % self.doQuote(userid)) |
---|
| 216 | return [record["groupname"] for record in (self.doParseResult(result) or [])] |
---|
| 217 | |
---|
[900] | 218 | def addPrinter(self, printername) : |
---|
| 219 | """Adds a printer to the quota storage, returns its id.""" |
---|
[915] | 220 | self.doQuery("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername)) |
---|
[900] | 221 | return self.getPrinterId(printername) |
---|
| 222 | |
---|
| 223 | def addUser(self, username) : |
---|
| 224 | """Adds a user to the quota storage, returns its id.""" |
---|
[915] | 225 | self.doQuery("INSERT INTO users (username) VALUES (%s)" % self.doQuote(username)) |
---|
[900] | 226 | return self.getUserId(username) |
---|
| 227 | |
---|
| 228 | def addGroup(self, groupname) : |
---|
| 229 | """Adds a group to the quota storage, returns its id.""" |
---|
[915] | 230 | self.doQuery("INSERT INTO groups (groupname) VALUES (%s)" % self.doQuote(groupname)) |
---|
[900] | 231 | return self.getGroupId(groupname) |
---|
| 232 | |
---|
| 233 | def addUserPQuota(self, username, printerid) : |
---|
| 234 | """Initializes a user print quota on a printer, adds the user to the quota storage if needed.""" |
---|
| 235 | userid = self.getUserId(username) |
---|
[695] | 236 | if userid is None : |
---|
[900] | 237 | userid = self.addUser(username) |
---|
[927] | 238 | uqexists = (self.getUserPQuota(userid, printerid) is not None) |
---|
| 239 | if not uqexists : |
---|
| 240 | self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
[900] | 241 | return (userid, printerid) |
---|
[695] | 242 | |
---|
[900] | 243 | def addGroupPQuota(self, groupname, printerid) : |
---|
| 244 | """Initializes a group print quota on a printer, adds the group to the quota storage if needed.""" |
---|
| 245 | groupid = self.getGroupId(groupname) |
---|
| 246 | if groupid is None : |
---|
[927] | 247 | groupid = self.addGroup(groupname) |
---|
| 248 | gqexists = (self.getGroupPQuota(groupid, printerid) is not None) |
---|
| 249 | if not gqexists : |
---|
| 250 | self.doQuery("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(groupid), self.doQuote(printerid))) |
---|
[900] | 251 | return (groupid, printerid) |
---|
[713] | 252 | |
---|
[915] | 253 | def increaseUserBalance(self, userid, amount) : |
---|
| 254 | """Increases (or decreases) an user's account balance by a given amount.""" |
---|
| 255 | self.doQuery("UPDATE users SET balance=balance+(%s), lifetimepaid=lifetimepaid+(%s) WHERE id=%s" % (self.doQuote(amount), self.doQuote(amount), self.doQuote(userid))) |
---|
| 256 | |
---|
| 257 | def getUserBalance(self, userid) : |
---|
| 258 | """Returns the current account balance for a given user.""" |
---|
[929] | 259 | result = self.doQuery("SELECT balance, lifetimepaid FROM users WHERE id=%s" % self.doQuote(userid)) |
---|
[915] | 260 | try : |
---|
[929] | 261 | result = self.doParseResult(result)[0] |
---|
[915] | 262 | except TypeError : # Not found |
---|
| 263 | return |
---|
[929] | 264 | else : |
---|
| 265 | return (result["balance"], result["lifetimepaid"]) |
---|
[915] | 266 | |
---|
[927] | 267 | def getGroupBalance(self, groupid) : |
---|
| 268 | """Returns the current account balance for a given group, as the sum of each of its users' account balance.""" |
---|
[929] | 269 | result = self.doQuery("SELECT SUM(balance) AS balance, SUM(lifetimepaid) AS lifetimepaid FROM users WHERE id in (SELECT userid FROM groupsmembers WHERE groupid=%s)" % self.doQuote(groupid)) |
---|
[927] | 270 | try : |
---|
[929] | 271 | result = self.doParseResult(result)[0] |
---|
[927] | 272 | except TypeError : # Not found |
---|
| 273 | return |
---|
[929] | 274 | else : |
---|
| 275 | return (result["balance"], result["lifetimepaid"]) |
---|
[927] | 276 | |
---|
[925] | 277 | def getUserLimitBy(self, userid) : |
---|
| 278 | """Returns the way in which user printing is limited.""" |
---|
| 279 | result = self.doQuery("SELECT limitby FROM users WHERE id=%s" % self.doQuote(userid)) |
---|
| 280 | try : |
---|
| 281 | return self.doParseResult(result)[0]["limitby"] |
---|
| 282 | except TypeError : # Not found |
---|
| 283 | return |
---|
| 284 | |
---|
| 285 | def getGroupLimitBy(self, groupid) : |
---|
| 286 | """Returns the way in which group printing is limited.""" |
---|
| 287 | result = self.doQuery("SELECT limitby FROM groups WHERE id=%s" % self.doQuote(groupid)) |
---|
| 288 | try : |
---|
| 289 | return self.doParseResult(result)[0]["limitby"] |
---|
| 290 | except TypeError : # Not found |
---|
| 291 | return |
---|
| 292 | |
---|
[915] | 293 | def setUserBalance(self, userid, balance) : |
---|
| 294 | """Sets the account balance for a given user to a fixed value.""" |
---|
[929] | 295 | (current, lifetimepaid) = self.getUserBalance(userid) |
---|
[915] | 296 | difference = balance - current |
---|
| 297 | self.increaseUserBalance(userid, difference) |
---|
| 298 | |
---|
[917] | 299 | def limitUserBy(self, userid, limitby) : |
---|
| 300 | """Limits a given user based either on print quota or on account balance.""" |
---|
| 301 | self.doQuery("UPDATE users SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(userid))) |
---|
[915] | 302 | |
---|
[917] | 303 | def limitGroupBy(self, groupid, limitby) : |
---|
| 304 | """Limits a given group based either on print quota or on sum of its users' account balances.""" |
---|
| 305 | self.doQuery("UPDATE groups SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(groupid))) |
---|
[915] | 306 | |
---|
[900] | 307 | def setUserPQuota(self, userid, printerid, softlimit, hardlimit) : |
---|
| 308 | """Sets soft and hard limits for a user quota on a specific printer given (userid, printerid).""" |
---|
[915] | 309 | 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))) |
---|
[695] | 310 | |
---|
[927] | 311 | def setGroupPQuota(self, groupid, printerid, softlimit, hardlimit) : |
---|
| 312 | """Sets soft and hard limits for a group quota on a specific printer given (groupid, printerid).""" |
---|
| 313 | self.doQuery("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE groupid=%s AND printerid=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(groupid), self.doQuote(printerid))) |
---|
| 314 | |
---|
[900] | 315 | def resetUserPQuota(self, userid, printerid) : |
---|
| 316 | """Resets the page counter to zero for a user on a printer. Life time page counter is kept unchanged.""" |
---|
[915] | 317 | self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
[695] | 318 | |
---|
[927] | 319 | def resetGroupPQuota(self, groupid, printerid) : |
---|
| 320 | """Resets the page counter to zero for a group on a printer. Life time page counter is kept unchanged.""" |
---|
| 321 | self.doQuery("UPDATE grouppquota SET pagecounter=0, datelimit=NULL WHERE groupid=%s AND printerid=%s" % (self.doQuote(groupid), self.doQuote(printerid))) |
---|
| 322 | |
---|
[900] | 323 | def updateUserPQuota(self, userid, printerid, pagecount) : |
---|
| 324 | """Updates the used user Quota information given (userid, printerid) and a job size in pages.""" |
---|
[915] | 325 | jobprice = self.computePrinterJobPrice(printerid, pagecount) |
---|
| 326 | queries = [] |
---|
| 327 | 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))) |
---|
| 328 | queries.append("UPDATE users SET balance=balance-(%s) WHERE id=%s" % (self.doQuote(jobprice), self.doQuote(userid))) |
---|
| 329 | self.doQuery(queries) |
---|
[768] | 330 | |
---|
[900] | 331 | def getUserPQuota(self, userid, printerid) : |
---|
| 332 | """Returns the Print Quota information for a given (userid, printerid).""" |
---|
[915] | 333 | result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
[900] | 334 | try : |
---|
| 335 | return self.doParseResult(result)[0] |
---|
| 336 | except TypeError : # Not found |
---|
| 337 | return |
---|
[722] | 338 | |
---|
[927] | 339 | def getGroupPQuota(self, groupid, printerid) : |
---|
| 340 | """Returns the Print Quota information for a given (groupid, printerid).""" |
---|
| 341 | result = self.doQuery("SELECT softlimit, hardlimit, datelimit FROM grouppquota WHERE groupid=%s AND printerid=%s" % (self.doQuote(groupid), self.doQuote(printerid))) |
---|
| 342 | try : |
---|
| 343 | grouppquota = self.doParseResult(result)[0] |
---|
| 344 | except TypeError : |
---|
| 345 | return |
---|
| 346 | else : |
---|
| 347 | result = self.doQuery("SELECT SUM(lifepagecounter) as lifepagecounter, SUM(pagecounter) as pagecounter FROM userpquota WHERE printerid=%s AND userid in (SELECT userid FROM groupsmembers WHERE groupid=%s)" % (self.doQuote(printerid), self.doQuote(groupid))) |
---|
| 348 | try : |
---|
| 349 | result = self.doParseResult(result)[0] |
---|
| 350 | except TypeError : # Not found |
---|
| 351 | return |
---|
| 352 | else : |
---|
| 353 | grouppquota.update({"lifepagecounter": result["lifepagecounter"], "pagecounter": result["pagecounter"]}) |
---|
| 354 | return grouppquota |
---|
| 355 | |
---|
[900] | 356 | def setUserDateLimit(self, userid, printerid, datelimit) : |
---|
| 357 | """Sets the limit date for a soft limit to become an hard one given (userid, printerid).""" |
---|
[915] | 358 | 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))) |
---|
[695] | 359 | |
---|
[927] | 360 | def setGroupDateLimit(self, groupid, printerid, datelimit) : |
---|
| 361 | """Sets the limit date for a soft limit to become an hard one given (groupid, printerid).""" |
---|
| 362 | self.doQuery("UPDATE grouppquota SET datelimit=%s::TIMESTAMP WHERE groupid=%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(groupid), self.doQuote(printerid))) |
---|
| 363 | |
---|
[900] | 364 | def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) : |
---|
| 365 | """Adds a job to the history: (jobid, userid, printerid, last page counter taken from requester).""" |
---|
[915] | 366 | 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))) |
---|
[900] | 367 | return self.getJobHistoryId(jobid, userid, printerid) # in case jobid is not sufficient |
---|
| 368 | |
---|
| 369 | def updateJobSizeInHistory(self, historyid, jobsize) : |
---|
| 370 | """Updates a job size in the history given the history line's id.""" |
---|
| 371 | self.doQuery("UPDATE jobhistory SET jobsize=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(historyid))) |
---|
| 372 | |
---|
| 373 | def getPrinterPageCounter(self, printerid) : |
---|
| 374 | """Returns the last page counter value for a printer given its id, also returns last username, last jobid and history line id.""" |
---|
[915] | 375 | 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)) |
---|
[900] | 376 | try : |
---|
| 377 | return self.doParseResult(result)[0] |
---|
| 378 | except TypeError : # Not found |
---|
| 379 | return |
---|
| 380 | |
---|
[927] | 381 | def addUserToGroup(self, userid, groupid) : |
---|
| 382 | """Adds an user to a group.""" |
---|
| 383 | result = self.doQuery("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(groupid), self.doQuote(userid))) |
---|
| 384 | try : |
---|
| 385 | mexists = self.doParseResult(result)[0]["mexists"] |
---|
| 386 | except TypeError : |
---|
| 387 | mexists = 0 |
---|
| 388 | if not mexists : |
---|
| 389 | self.doQuery("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(groupid), self.doQuote(userid))) |
---|
| 390 | |
---|
[921] | 391 | def deleteUser(self, userid) : |
---|
| 392 | """Completely deletes an user from the Quota Storage.""" |
---|
| 393 | queries = [] |
---|
| 394 | queries.append("DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(userid)) |
---|
| 395 | queries.append("DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(userid)) |
---|
| 396 | queries.append("DELETE FROM userpquota WHERE userid=%s" % self.doQuote(userid)) |
---|
| 397 | queries.append("DELETE FROM users WHERE id=%s" % self.doQuote(userid)) |
---|
| 398 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
| 399 | self.doQuery(queries) |
---|
| 400 | |
---|
| 401 | def deleteGroup(self, groupid) : |
---|
| 402 | """Completely deletes an user from the Quota Storage.""" |
---|
| 403 | queries = [] |
---|
| 404 | queries.append("DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(groupid)) |
---|
| 405 | queries.append("DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(groupid)) |
---|
| 406 | queries.append("DELETE FROM groups WHERE id=%s" % self.doQuote(groupid)) |
---|
| 407 | self.doQuery(queries) |
---|
| 408 | |
---|
[915] | 409 | def computePrinterJobPrice(self, printerid, jobsize) : |
---|
| 410 | """Returns the price for a job on a given printer.""" |
---|
| 411 | prices = self.getPrinterPrices(printerid) |
---|
| 412 | if prices is None : |
---|
| 413 | perpage = perjob = 0.0 |
---|
| 414 | else : |
---|
| 415 | (perpage, perjob) = prices |
---|
| 416 | return perjob + (perpage * jobsize) |
---|