[1327] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
| 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | # $Log$ |
---|
[1787] | 24 | # Revision 1.55 2004/10/07 09:37:53 jalet |
---|
| 25 | # Fixes recently introduced bug wrt users groups (was it three days ago ?) |
---|
| 26 | # |
---|
[1778] | 27 | # Revision 1.54 2004/10/05 10:05:04 jalet |
---|
| 28 | # UnicodeEncodeError isn't defined in Python2.1 |
---|
| 29 | # |
---|
[1777] | 30 | # Revision 1.53 2004/10/05 09:59:20 jalet |
---|
| 31 | # Restore compatibility with Python 2.1 |
---|
| 32 | # |
---|
[1773] | 33 | # Revision 1.52 2004/10/04 22:23:54 jalet |
---|
| 34 | # Charset conversions for dumps from the PostgreSQL backend |
---|
| 35 | # |
---|
[1770] | 36 | # Revision 1.51 2004/10/04 16:11:38 jalet |
---|
| 37 | # Now outputs page counters when dumping user groups quotas |
---|
| 38 | # |
---|
[1769] | 39 | # Revision 1.50 2004/10/04 16:01:15 jalet |
---|
| 40 | # More complete dumps for groups and groups quotas |
---|
| 41 | # |
---|
[1762] | 42 | # Revision 1.49 2004/10/02 13:33:13 jalet |
---|
| 43 | # Some work done of user's charset handling in database dumps. |
---|
| 44 | # |
---|
[1761] | 45 | # Revision 1.48 2004/10/02 05:48:56 jalet |
---|
| 46 | # Should now correctly deal with charsets both when storing into databases and when |
---|
| 47 | # retrieving datas. Works with both PostgreSQL and LDAP. |
---|
| 48 | # |
---|
[1719] | 49 | # Revision 1.47 2004/09/15 07:26:20 jalet |
---|
| 50 | # Data dumps are now ordered by entry creation date if applicable. |
---|
| 51 | # Now dumpykota exits with a message when there's a broken pipe like |
---|
| 52 | # in dumpykota --data history | head -3 |
---|
| 53 | # |
---|
[1718] | 54 | # Revision 1.46 2004/09/15 06:58:25 jalet |
---|
| 55 | # User groups membership and printer groups membership can now be dumped too |
---|
| 56 | # |
---|
[1717] | 57 | # Revision 1.45 2004/09/14 22:29:13 jalet |
---|
| 58 | # First version of dumpykota. Works fine but only with PostgreSQL backend |
---|
| 59 | # for now. |
---|
| 60 | # |
---|
[1711] | 61 | # Revision 1.44 2004/09/10 21:32:54 jalet |
---|
| 62 | # Small fixes for incomplete entry intialization |
---|
| 63 | # |
---|
[1582] | 64 | # Revision 1.43 2004/07/01 17:45:49 jalet |
---|
| 65 | # Added code to handle the description field for printers |
---|
| 66 | # |
---|
[1531] | 67 | # Revision 1.42 2004/06/08 17:44:43 jalet |
---|
| 68 | # Payment now gets deleted when the user is deleted |
---|
| 69 | # |
---|
[1522] | 70 | # Revision 1.41 2004/06/05 22:03:50 jalet |
---|
| 71 | # Payments history is now stored in database |
---|
| 72 | # |
---|
[1520] | 73 | # Revision 1.40 2004/06/03 23:14:11 jalet |
---|
| 74 | # Now stores the job's size in bytes in the database. |
---|
| 75 | # Preliminary work on payments storage : database schemas are OK now, |
---|
| 76 | # but no code to store payments yet. |
---|
| 77 | # Removed schema picture, not relevant anymore. |
---|
| 78 | # |
---|
[1502] | 79 | # Revision 1.39 2004/05/26 14:50:12 jalet |
---|
| 80 | # First try at saving the job-originating-hostname in the database |
---|
| 81 | # |
---|
[1451] | 82 | # Revision 1.38 2004/05/06 12:37:47 jalet |
---|
| 83 | # pkpgcounter : comments |
---|
| 84 | # pkprinters : when --add is used, existing printers are now skipped. |
---|
| 85 | # |
---|
[1358] | 86 | # Revision 1.37 2004/02/23 22:53:21 jalet |
---|
| 87 | # Don't retrieve data when it's not needed, to avoid database queries |
---|
| 88 | # |
---|
[1332] | 89 | # Revision 1.36 2004/02/04 13:24:41 jalet |
---|
| 90 | # pkprinters can now remove printers from printers groups. |
---|
| 91 | # |
---|
[1330] | 92 | # Revision 1.35 2004/02/04 11:17:00 jalet |
---|
| 93 | # pkprinters command line tool added. |
---|
| 94 | # |
---|
[1327] | 95 | # Revision 1.34 2004/02/02 22:44:16 jalet |
---|
| 96 | # Preliminary work on Relationnal Database Independance via DB-API 2.0 |
---|
| 97 | # |
---|
| 98 | # |
---|
| 99 | # |
---|
| 100 | |
---|
[1762] | 101 | from types import StringType |
---|
[1327] | 102 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
| 103 | |
---|
| 104 | class SQLStorage : |
---|
[1717] | 105 | def prepareRawResult(self, result) : |
---|
| 106 | """Prepares a raw result by including the headers.""" |
---|
| 107 | if result.ntuples() > 0 : |
---|
| 108 | entries = [result.listfields()] |
---|
| 109 | entries.extend(result.getresult()) |
---|
[1773] | 110 | nbfields = len(entries[0]) |
---|
| 111 | for i in range(1, len(entries)) : |
---|
| 112 | fields = list(entries[i]) |
---|
| 113 | for j in range(nbfields) : |
---|
| 114 | field = fields[j] |
---|
| 115 | if type(field) == StringType : |
---|
| 116 | try : |
---|
[1777] | 117 | fields[j] = unicode(field, "UTF-8").encode(self.tool.getCharset()) |
---|
[1778] | 118 | except UnicodeError : # takes care of old jobs in history not stored as UTF-8 |
---|
[1773] | 119 | pass |
---|
| 120 | entries[i] = tuple(fields) |
---|
[1717] | 121 | return entries |
---|
| 122 | |
---|
| 123 | def extractPrinters(self) : |
---|
| 124 | """Extracts all printer records.""" |
---|
[1719] | 125 | result = self.doRawSearch("SELECT * FROM printers ORDER BY id ASC") |
---|
[1717] | 126 | return self.prepareRawResult(result) |
---|
| 127 | |
---|
| 128 | def extractUsers(self) : |
---|
| 129 | """Extracts all user records.""" |
---|
[1719] | 130 | result = self.doRawSearch("SELECT * FROM users ORDER BY id ASC") |
---|
[1717] | 131 | return self.prepareRawResult(result) |
---|
| 132 | |
---|
| 133 | def extractGroups(self) : |
---|
| 134 | """Extracts all group records.""" |
---|
[1769] | 135 | result = self.doRawSearch("SELECT groups.*,sum(balance) AS balance, sum(lifetimepaid) as lifetimepaid FROM groups,users WHERE users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) GROUP BY groups.id,groups.groupname,groups.limitby ORDER BY groups.id ASC") |
---|
[1717] | 136 | return self.prepareRawResult(result) |
---|
| 137 | |
---|
| 138 | def extractPayments(self) : |
---|
| 139 | """Extracts all payment records.""" |
---|
[1719] | 140 | result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid ORDER BY payments.id ASC") |
---|
[1717] | 141 | return self.prepareRawResult(result) |
---|
| 142 | |
---|
| 143 | def extractUpquotas(self) : |
---|
| 144 | """Extracts all userpquota records.""" |
---|
[1719] | 145 | result = self.doRawSearch("SELECT users.username,printers.printername,userpquota.* FROM users,printers,userpquota WHERE users.id=userpquota.userid AND printers.id=userpquota.printerid ORDER BY userpquota.id ASC") |
---|
[1717] | 146 | return self.prepareRawResult(result) |
---|
| 147 | |
---|
| 148 | def extractGpquotas(self) : |
---|
| 149 | """Extracts all grouppquota records.""" |
---|
[1770] | 150 | result = self.doRawSearch("SELECT groups.groupname,printers.printername,grouppquota.*,sum(pagecounter) AS pagecounter,sum(lifepagecounter) AS lifepagecounter FROM groups,printers,grouppquota,userpquota WHERE groups.id=grouppquota.groupid AND printers.id=grouppquota.printerid AND userpquota.printerid=grouppquota.printerid AND userpquota.userid IN (SELECT userid FROM groupsmembers WHERE groupsmembers.groupid=grouppquota.groupid) GROUP BY grouppquota.id,grouppquota.groupid,grouppquota.printerid,grouppquota.softlimit,grouppquota.hardlimit,grouppquota.datelimit,groups.groupname,printers.printername ORDER BY grouppquota.id") |
---|
[1717] | 151 | return self.prepareRawResult(result) |
---|
| 152 | |
---|
[1718] | 153 | def extractUmembers(self) : |
---|
| 154 | """Extracts all user groups members.""" |
---|
[1719] | 155 | result = self.doRawSearch("SELECT groups.groupname, users.username, groupsmembers.* FROM groups,users,groupsmembers WHERE users.id=groupsmembers.userid AND groups.id=groupsmembers.groupid ORDER BY groupsmembers.groupid, groupsmembers.userid ASC") |
---|
[1718] | 156 | return self.prepareRawResult(result) |
---|
| 157 | |
---|
| 158 | def extractPmembers(self) : |
---|
| 159 | """Extracts all printer groups members.""" |
---|
[1719] | 160 | result = self.doRawSearch("SELECT p1.printername as pgroupname, p2.printername as printername, printergroupsmembers.* FROM printers p1, printers p2, printergroupsmembers WHERE p1.id=printergroupsmembers.groupid AND p2.id=printergroupsmembers.printerid ORDER BY printergroupsmembers.groupid, printergroupsmembers.printerid ASC") |
---|
[1718] | 161 | return self.prepareRawResult(result) |
---|
| 162 | |
---|
[1717] | 163 | def extractHistory(self) : |
---|
| 164 | """Extracts all jobhistory records.""" |
---|
[1719] | 165 | result = self.doRawSearch("SELECT users.username,printers.printername,jobhistory.* FROM users,printers,jobhistory WHERE users.id=jobhistory.userid AND printers.id=jobhistory.printerid ORDER BY jobhistory.id ASC") |
---|
[1717] | 166 | return self.prepareRawResult(result) |
---|
| 167 | |
---|
[1327] | 168 | def getAllUsersNames(self) : |
---|
| 169 | """Extracts all user names.""" |
---|
| 170 | usernames = [] |
---|
| 171 | result = self.doSearch("SELECT username FROM users") |
---|
| 172 | if result : |
---|
| 173 | usernames = [record["username"] for record in result] |
---|
| 174 | return usernames |
---|
| 175 | |
---|
| 176 | def getAllGroupsNames(self) : |
---|
| 177 | """Extracts all group names.""" |
---|
| 178 | groupnames = [] |
---|
| 179 | result = self.doSearch("SELECT groupname FROM groups") |
---|
| 180 | if result : |
---|
| 181 | groupnames = [record["groupname"] for record in result] |
---|
| 182 | return groupnames |
---|
| 183 | |
---|
| 184 | def getUserFromBackend(self, username) : |
---|
| 185 | """Extracts user information given its name.""" |
---|
| 186 | user = StorageUser(self, username) |
---|
| 187 | result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1" % self.doQuote(username)) |
---|
| 188 | if result : |
---|
| 189 | fields = result[0] |
---|
| 190 | user.ident = fields.get("id") |
---|
[1451] | 191 | user.Name = fields.get("username", username) |
---|
[1327] | 192 | user.LimitBy = fields.get("limitby") |
---|
| 193 | user.AccountBalance = fields.get("balance") |
---|
| 194 | user.LifeTimePaid = fields.get("lifetimepaid") |
---|
| 195 | user.Email = fields.get("email") |
---|
| 196 | user.Exists = 1 |
---|
| 197 | return user |
---|
| 198 | |
---|
| 199 | def getGroupFromBackend(self, groupname) : |
---|
| 200 | """Extracts group information given its name.""" |
---|
| 201 | group = StorageGroup(self, groupname) |
---|
[1787] | 202 | result = self.doSearch("SELECT * FROM groups WHERE groupname=%s LIMIT 1" % self.doQuote(groupname)) |
---|
[1327] | 203 | if result : |
---|
| 204 | fields = result[0] |
---|
| 205 | group.ident = fields.get("id") |
---|
[1451] | 206 | group.Name = fields.get("groupname", groupname) |
---|
[1327] | 207 | group.LimitBy = fields.get("limitby") |
---|
[1787] | 208 | result = self.doSearch("SELECT SUM(balance) AS balance, SUM(lifetimepaid) AS lifetimepaid FROM users WHERE id IN (SELECT userid FROM groupsmembers WHERE groupid=%s)" % self.doQuote(group.ident)) |
---|
| 209 | if result : |
---|
| 210 | fields = result[0] |
---|
| 211 | group.AccountBalance = fields.get("balance") or 0.0 |
---|
| 212 | group.LifeTimePaid = fields.get("lifetimepaid") or 0.0 |
---|
[1327] | 213 | group.Exists = 1 |
---|
| 214 | return group |
---|
| 215 | |
---|
| 216 | def getPrinterFromBackend(self, printername) : |
---|
| 217 | """Extracts printer information given its name.""" |
---|
| 218 | printer = StoragePrinter(self, printername) |
---|
| 219 | result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" % self.doQuote(printername)) |
---|
| 220 | if result : |
---|
| 221 | fields = result[0] |
---|
| 222 | printer.ident = fields.get("id") |
---|
[1451] | 223 | printer.Name = fields.get("printername", printername) |
---|
[1582] | 224 | printer.PricePerJob = fields.get("priceperjob") or 0.0 |
---|
| 225 | printer.PricePerPage = fields.get("priceperpage") or 0.0 |
---|
[1777] | 226 | printer.Description = unicode((fields.get("description") or ""), "UTF-8").encode(self.tool.getCharset()) |
---|
[1327] | 227 | printer.Exists = 1 |
---|
| 228 | return printer |
---|
| 229 | |
---|
| 230 | def getUserPQuotaFromBackend(self, user, printer) : |
---|
| 231 | """Extracts a user print quota.""" |
---|
| 232 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 233 | if printer.Exists and user.Exists : |
---|
| 234 | result = self.doSearch("SELECT id, lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s" % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
| 235 | if result : |
---|
| 236 | fields = result[0] |
---|
| 237 | userpquota.ident = fields.get("id") |
---|
| 238 | userpquota.PageCounter = fields.get("pagecounter") |
---|
| 239 | userpquota.LifePageCounter = fields.get("lifepagecounter") |
---|
| 240 | userpquota.SoftLimit = fields.get("softlimit") |
---|
| 241 | userpquota.HardLimit = fields.get("hardlimit") |
---|
| 242 | userpquota.DateLimit = fields.get("datelimit") |
---|
| 243 | userpquota.Exists = 1 |
---|
| 244 | return userpquota |
---|
| 245 | |
---|
| 246 | def getGroupPQuotaFromBackend(self, group, printer) : |
---|
| 247 | """Extracts a group print quota.""" |
---|
| 248 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
| 249 | if group.Exists : |
---|
[1787] | 250 | result = self.doSearch("SELECT id, softlimit, hardlimit, datelimit FROM grouppquota WHERE groupid=%s AND printerid=%s" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
[1327] | 251 | if result : |
---|
| 252 | fields = result[0] |
---|
| 253 | grouppquota.ident = fields.get("id") |
---|
| 254 | grouppquota.SoftLimit = fields.get("softlimit") |
---|
| 255 | grouppquota.HardLimit = fields.get("hardlimit") |
---|
| 256 | grouppquota.DateLimit = fields.get("datelimit") |
---|
[1787] | 257 | result = self.doSearch("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(printer.ident), self.doQuote(group.ident))) |
---|
| 258 | if result : |
---|
| 259 | fields = result[0] |
---|
| 260 | grouppquota.PageCounter = fields.get("pagecounter") or 0 |
---|
| 261 | grouppquota.LifePageCounter = fields.get("lifepagecounter") or 0 |
---|
[1327] | 262 | grouppquota.Exists = 1 |
---|
| 263 | return grouppquota |
---|
| 264 | |
---|
| 265 | def getPrinterLastJobFromBackend(self, printer) : |
---|
| 266 | """Extracts a printer's last job information.""" |
---|
| 267 | lastjob = StorageLastJob(self, printer) |
---|
[1502] | 268 | result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobprice, filename, title, copies, options, hostname, jobdate FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printer.ident)) |
---|
[1327] | 269 | if result : |
---|
| 270 | fields = result[0] |
---|
| 271 | lastjob.ident = fields.get("id") |
---|
| 272 | lastjob.JobId = fields.get("jobid") |
---|
[1358] | 273 | lastjob.UserName = fields.get("username") |
---|
[1327] | 274 | lastjob.PrinterPageCounter = fields.get("pagecounter") |
---|
| 275 | lastjob.JobSize = fields.get("jobsize") |
---|
| 276 | lastjob.JobPrice = fields.get("jobprice") |
---|
| 277 | lastjob.JobAction = fields.get("action") |
---|
[1777] | 278 | lastjob.JobFileName = unicode((fields.get("filename") or ""), "UTF-8").encode(self.tool.getCharset()) |
---|
| 279 | lastjob.JobTitle = unicode((fields.get("title") or ""), "UTF-8").encode(self.tool.getCharset()) |
---|
[1327] | 280 | lastjob.JobCopies = fields.get("copies") |
---|
[1777] | 281 | lastjob.JobOptions = unicode((fields.get("options") or ""), "UTF-8").encode(self.tool.getCharset()) |
---|
[1327] | 282 | lastjob.JobDate = fields.get("jobdate") |
---|
[1502] | 283 | lastjob.JobHostName = fields.get("hostname") |
---|
[1520] | 284 | lastjob.JobSizeBytes = fields.get("jobsizebytes") |
---|
[1327] | 285 | lastjob.Exists = 1 |
---|
| 286 | return lastjob |
---|
| 287 | |
---|
| 288 | def getGroupMembersFromBackend(self, group) : |
---|
| 289 | """Returns the group's members list.""" |
---|
| 290 | groupmembers = [] |
---|
| 291 | result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident)) |
---|
| 292 | if result : |
---|
| 293 | for record in result : |
---|
| 294 | user = StorageUser(self, record.get("username")) |
---|
| 295 | user.ident = record.get("userid") |
---|
| 296 | user.LimitBy = record.get("limitby") |
---|
| 297 | user.AccountBalance = record.get("balance") |
---|
| 298 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
| 299 | user.Email = record.get("email") |
---|
| 300 | user.Exists = 1 |
---|
| 301 | groupmembers.append(user) |
---|
| 302 | self.cacheEntry("USERS", user.Name, user) |
---|
| 303 | return groupmembers |
---|
| 304 | |
---|
| 305 | def getUserGroupsFromBackend(self, user) : |
---|
| 306 | """Returns the user's groups list.""" |
---|
| 307 | groups = [] |
---|
| 308 | result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident)) |
---|
| 309 | if result : |
---|
| 310 | for record in result : |
---|
| 311 | groups.append(self.getGroup(record.get("groupname"))) |
---|
| 312 | return groups |
---|
| 313 | |
---|
| 314 | def getParentPrintersFromBackend(self, printer) : |
---|
| 315 | """Get all the printer groups this printer is a member of.""" |
---|
| 316 | pgroups = [] |
---|
| 317 | result = self.doSearch("SELECT groupid,printername FROM printergroupsmembers JOIN printers ON groupid=id WHERE printerid=%s" % self.doQuote(printer.ident)) |
---|
| 318 | if result : |
---|
| 319 | for record in result : |
---|
| 320 | if record["groupid"] != printer.ident : # in case of integrity violation |
---|
| 321 | parentprinter = self.getPrinter(record.get("printername")) |
---|
| 322 | if parentprinter.Exists : |
---|
| 323 | pgroups.append(parentprinter) |
---|
| 324 | return pgroups |
---|
| 325 | |
---|
| 326 | def getMatchingPrinters(self, printerpattern) : |
---|
| 327 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
| 328 | printers = [] |
---|
| 329 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
| 330 | # but we don't because other storages semantics may be different, so every |
---|
| 331 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
| 332 | result = self.doSearch("SELECT * FROM printers") |
---|
| 333 | if result : |
---|
| 334 | for record in result : |
---|
| 335 | if self.tool.matchString(record["printername"], printerpattern.split(",")) : |
---|
| 336 | printer = StoragePrinter(self, record["printername"]) |
---|
| 337 | printer.ident = record.get("id") |
---|
[1582] | 338 | printer.PricePerJob = record.get("priceperjob") or 0.0 |
---|
| 339 | printer.PricePerPage = record.get("priceperpage") or 0.0 |
---|
[1777] | 340 | printer.Description = unicode((record.get("description") or ""), "UTF-8").encode(self.tool.getCharset()) |
---|
[1327] | 341 | printer.Exists = 1 |
---|
| 342 | printers.append(printer) |
---|
| 343 | self.cacheEntry("PRINTERS", printer.Name, printer) |
---|
| 344 | return printers |
---|
| 345 | |
---|
| 346 | def getPrinterUsersAndQuotas(self, printer, names=["*"]) : |
---|
| 347 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
| 348 | usersandquotas = [] |
---|
| 349 | result = self.doSearch("SELECT users.id as uid,username,balance,lifetimepaid,limitby,email,userpquota.id,lifepagecounter,pagecounter,softlimit,hardlimit,datelimit FROM users JOIN userpquota ON users.id=userpquota.userid AND printerid=%s ORDER BY username ASC" % self.doQuote(printer.ident)) |
---|
| 350 | if result : |
---|
| 351 | for record in result : |
---|
| 352 | if self.tool.matchString(record.get("username"), names) : |
---|
| 353 | user = StorageUser(self, record.get("username")) |
---|
| 354 | user.ident = record.get("uid") |
---|
| 355 | user.LimitBy = record.get("limitby") |
---|
| 356 | user.AccountBalance = record.get("balance") |
---|
| 357 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
| 358 | user.Email = record.get("email") |
---|
| 359 | user.Exists = 1 |
---|
| 360 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 361 | userpquota.ident = record.get("id") |
---|
| 362 | userpquota.PageCounter = record.get("pagecounter") |
---|
| 363 | userpquota.LifePageCounter = record.get("lifepagecounter") |
---|
| 364 | userpquota.SoftLimit = record.get("softlimit") |
---|
| 365 | userpquota.HardLimit = record.get("hardlimit") |
---|
| 366 | userpquota.DateLimit = record.get("datelimit") |
---|
| 367 | userpquota.Exists = 1 |
---|
| 368 | usersandquotas.append((user, userpquota)) |
---|
| 369 | self.cacheEntry("USERS", user.Name, user) |
---|
| 370 | self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) |
---|
| 371 | return usersandquotas |
---|
| 372 | |
---|
| 373 | def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : |
---|
| 374 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
| 375 | groupsandquotas = [] |
---|
| 376 | result = self.doSearch("SELECT groupname FROM groups JOIN grouppquota ON groups.id=grouppquota.groupid AND printerid=%s ORDER BY groupname ASC" % self.doQuote(printer.ident)) |
---|
| 377 | if result : |
---|
| 378 | for record in result : |
---|
| 379 | if self.tool.matchString(record.get("groupname"), names) : |
---|
| 380 | group = self.getGroup(record.get("groupname")) |
---|
| 381 | grouppquota = self.getGroupPQuota(group, printer) |
---|
| 382 | groupsandquotas.append((group, grouppquota)) |
---|
| 383 | return groupsandquotas |
---|
| 384 | |
---|
| 385 | def addPrinter(self, printername) : |
---|
| 386 | """Adds a printer to the quota storage, returns it.""" |
---|
| 387 | self.doModify("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername)) |
---|
| 388 | return self.getPrinter(printername) |
---|
| 389 | |
---|
| 390 | def addUser(self, user) : |
---|
| 391 | """Adds a user to the quota storage, returns its id.""" |
---|
[1711] | 392 | self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email) VALUES (%s, %s, %s, %s, %s)" % (self.doQuote(user.Name), self.doQuote(user.LimitBy or 'quota'), self.doQuote(user.AccountBalance or 0.0), self.doQuote(user.LifeTimePaid or 0.0), self.doQuote(user.Email))) |
---|
[1327] | 393 | return self.getUser(user.Name) |
---|
| 394 | |
---|
| 395 | def addGroup(self, group) : |
---|
| 396 | """Adds a group to the quota storage, returns its id.""" |
---|
[1711] | 397 | self.doModify("INSERT INTO groups (groupname, limitby) VALUES (%s, %s)" % (self.doQuote(group.Name), self.doQuote(group.LimitBy or "quota"))) |
---|
[1327] | 398 | return self.getGroup(group.Name) |
---|
| 399 | |
---|
| 400 | def addUserToGroup(self, user, group) : |
---|
| 401 | """Adds an user to a group.""" |
---|
| 402 | result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
| 403 | try : |
---|
| 404 | mexists = int(result[0].get("mexists")) |
---|
| 405 | except (IndexError, TypeError) : |
---|
| 406 | mexists = 0 |
---|
| 407 | if not mexists : |
---|
| 408 | self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
| 409 | |
---|
| 410 | def addUserPQuota(self, user, printer) : |
---|
| 411 | """Initializes a user print quota on a printer.""" |
---|
| 412 | self.doModify("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
| 413 | return self.getUserPQuota(user, printer) |
---|
| 414 | |
---|
| 415 | def addGroupPQuota(self, group, printer) : |
---|
| 416 | """Initializes a group print quota on a printer.""" |
---|
| 417 | self.doModify("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
| 418 | return self.getGroupPQuota(group, printer) |
---|
| 419 | |
---|
| 420 | def writePrinterPrices(self, printer) : |
---|
| 421 | """Write the printer's prices back into the storage.""" |
---|
| 422 | self.doModify("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(printer.PricePerPage), self.doQuote(printer.PricePerJob), self.doQuote(printer.ident))) |
---|
| 423 | |
---|
[1582] | 424 | def writePrinterDescription(self, printer) : |
---|
| 425 | """Write the printer's description back into the storage.""" |
---|
[1761] | 426 | description = printer.Description |
---|
| 427 | if description is not None : |
---|
[1777] | 428 | description = unicode(printer.Description, self.tool.getCharset()).encode("UTF-8"), |
---|
[1761] | 429 | self.doModify("UPDATE printers SET description=%s WHERE id=%s" % (self.doQuote(description), self.doQuote(printer.ident))) |
---|
[1582] | 430 | |
---|
[1327] | 431 | def writeUserLimitBy(self, user, limitby) : |
---|
| 432 | """Sets the user's limiting factor.""" |
---|
| 433 | self.doModify("UPDATE users SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(user.ident))) |
---|
| 434 | |
---|
| 435 | def writeGroupLimitBy(self, group, limitby) : |
---|
| 436 | """Sets the group's limiting factor.""" |
---|
| 437 | self.doModify("UPDATE groups SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(group.ident))) |
---|
| 438 | |
---|
| 439 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
| 440 | """Sets the date limit permanently for a user print quota.""" |
---|
| 441 | self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident))) |
---|
| 442 | |
---|
| 443 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
| 444 | """Sets the date limit permanently for a group print quota.""" |
---|
| 445 | self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident))) |
---|
| 446 | |
---|
| 447 | def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) : |
---|
| 448 | """Increase page counters for a user print quota.""" |
---|
| 449 | self.doModify("UPDATE userpquota SET pagecounter=pagecounter+%s,lifepagecounter=lifepagecounter+%s WHERE id=%s" % (self.doQuote(nbpages), self.doQuote(nbpages), self.doQuote(userpquota.ident))) |
---|
| 450 | |
---|
| 451 | def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) : |
---|
| 452 | """Sets the new page counters permanently for a user print quota.""" |
---|
| 453 | self.doModify("UPDATE userpquota SET pagecounter=%s,lifepagecounter=%s WHERE id=%s" % (self.doQuote(newpagecounter), self.doQuote(newlifepagecounter), self.doQuote(userpquota.ident))) |
---|
| 454 | |
---|
| 455 | def decreaseUserAccountBalance(self, user, amount) : |
---|
| 456 | """Decreases user's account balance from an amount.""" |
---|
| 457 | self.doModify("UPDATE users SET balance=balance-%s WHERE id=%s" % (self.doQuote(amount), self.doQuote(user.ident))) |
---|
| 458 | |
---|
| 459 | def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) : |
---|
| 460 | """Sets the new account balance and eventually new lifetime paid.""" |
---|
| 461 | if newlifetimepaid is not None : |
---|
| 462 | self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident))) |
---|
| 463 | else : |
---|
| 464 | self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident))) |
---|
| 465 | |
---|
[1522] | 466 | def writeNewPayment(self, user, amount) : |
---|
| 467 | """Adds a new payment to the payments history.""" |
---|
| 468 | self.doModify("INSERT INTO payments (userid, amount) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(amount))) |
---|
| 469 | |
---|
[1327] | 470 | def writeLastJobSize(self, lastjob, jobsize, jobprice) : |
---|
| 471 | """Sets the last job's size permanently.""" |
---|
| 472 | self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident))) |
---|
| 473 | |
---|
[1761] | 474 | def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None) : |
---|
[1327] | 475 | """Adds a job in a printer's history.""" |
---|
[1761] | 476 | if filename is not None : |
---|
[1777] | 477 | filename = unicode(filename, self.tool.getCharset()).encode("UTF-8") |
---|
[1761] | 478 | if title is not None : |
---|
[1777] | 479 | title = unicode(title, self.tool.getCharset()).encode("UTF-8") |
---|
[1761] | 480 | if options is not None : |
---|
[1777] | 481 | options = unicode(options, self.tool.getCharset()).encode("UTF-8") |
---|
[1327] | 482 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
| 483 | if jobsize is not None : |
---|
[1520] | 484 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, hostname, jobsizebytes) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes))) |
---|
[1327] | 485 | else : |
---|
[1520] | 486 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options, hostname, jobsizebytes) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes))) |
---|
[1327] | 487 | else : |
---|
| 488 | # here we explicitly want to reset jobsize to NULL if needed |
---|
[1520] | 489 | self.doModify("UPDATE jobhistory SET userid=%s, jobid=%s, pagecounter=%s, action=%s, jobsize=%s, jobprice=%s, filename=%s, title=%s, copies=%s, options=%s, hostname=%s, jobsizebytes=%s, jobdate=now() WHERE id=%s" % (self.doQuote(user.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(printer.LastJob.ident))) |
---|
[1327] | 490 | |
---|
| 491 | def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) : |
---|
| 492 | """Sets soft and hard limits for a user quota.""" |
---|
| 493 | self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userpquota.ident))) |
---|
| 494 | |
---|
| 495 | def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) : |
---|
| 496 | """Sets soft and hard limits for a group quota on a specific printer.""" |
---|
| 497 | self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(grouppquota.ident))) |
---|
| 498 | |
---|
| 499 | def writePrinterToGroup(self, pgroup, printer) : |
---|
| 500 | """Puts a printer into a printer group.""" |
---|
| 501 | children = [] |
---|
| 502 | result = self.doSearch("SELECT printerid FROM printergroupsmembers WHERE groupid=%s" % self.doQuote(pgroup.ident)) |
---|
| 503 | if result : |
---|
| 504 | for record in result : |
---|
| 505 | children.append(record.get("printerid")) # TODO : put this into the database integrity rules |
---|
| 506 | if printer.ident not in children : |
---|
| 507 | self.doModify("INSERT INTO printergroupsmembers (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
| 508 | |
---|
[1332] | 509 | def removePrinterFromGroup(self, pgroup, printer) : |
---|
| 510 | """Removes a printer from a printer group.""" |
---|
| 511 | self.doModify("DELETE FROM printergroupsmembers WHERE groupid=%s AND printerid=%s" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
| 512 | |
---|
[1502] | 513 | def retrieveHistory(self, user=None, printer=None, datelimit=None, hostname=None, limit=100) : |
---|
[1327] | 514 | """Retrieves all print jobs for user on printer (or all) before date, limited to first 100 results.""" |
---|
| 515 | query = "SELECT jobhistory.*,username,printername FROM jobhistory,users,printers WHERE users.id=userid AND printers.id=printerid" |
---|
| 516 | where = [] |
---|
| 517 | if (user is not None) and user.Exists : |
---|
| 518 | where.append("userid=%s" % self.doQuote(user.ident)) |
---|
| 519 | if (printer is not None) and printer.Exists : |
---|
| 520 | where.append("printerid=%s" % self.doQuote(printer.ident)) |
---|
[1502] | 521 | if hostname is not None : |
---|
| 522 | where.append("hostname=%s" % self.doQuote(hostname)) |
---|
[1327] | 523 | if datelimit is not None : |
---|
| 524 | where.append("jobdate<=%s" % self.doQuote(datelimit)) |
---|
| 525 | if where : |
---|
| 526 | query += " AND %s" % " AND ".join(where) |
---|
| 527 | query += " ORDER BY id DESC" |
---|
| 528 | if limit : |
---|
| 529 | query += " LIMIT %s" % self.doQuote(int(limit)) |
---|
| 530 | jobs = [] |
---|
| 531 | result = self.doSearch(query) |
---|
| 532 | if result : |
---|
| 533 | for fields in result : |
---|
| 534 | job = StorageJob(self) |
---|
| 535 | job.ident = fields.get("id") |
---|
| 536 | job.JobId = fields.get("jobid") |
---|
| 537 | job.PrinterPageCounter = fields.get("pagecounter") |
---|
| 538 | job.JobSize = fields.get("jobsize") |
---|
| 539 | job.JobPrice = fields.get("jobprice") |
---|
| 540 | job.JobAction = fields.get("action") |
---|
[1777] | 541 | job.JobFileName = unicode((fields.get("filename") or ""), "UTF-8").encode(self.tool.getCharset()) |
---|
| 542 | job.JobTitle = unicode((fields.get("title") or ""), "UTF-8").encode(self.tool.getCharset()) |
---|
[1327] | 543 | job.JobCopies = fields.get("copies") |
---|
[1777] | 544 | job.JobOptions = unicode((fields.get("options") or ""), "UTF-8").encode(self.tool.getCharset()) |
---|
[1327] | 545 | job.JobDate = fields.get("jobdate") |
---|
[1502] | 546 | job.JobHostName = fields.get("hostname") |
---|
[1520] | 547 | job.JobSizeBytes = fields.get("jobsizebytes") |
---|
[1358] | 548 | job.UserName = fields.get("username") |
---|
| 549 | job.PrinterName = fields.get("printername") |
---|
[1327] | 550 | job.Exists = 1 |
---|
| 551 | jobs.append(job) |
---|
| 552 | return jobs |
---|
| 553 | |
---|
| 554 | def deleteUser(self, user) : |
---|
| 555 | """Completely deletes an user from the Quota Storage.""" |
---|
| 556 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
| 557 | # TODO : we can't reassign the last job to the previous one, because next user would be |
---|
| 558 | # TODO : incorrectly charged (overcharged). |
---|
| 559 | for q in [ |
---|
[1531] | 560 | "DELETE FROM payments WHERE userid=%s" % self.doQuote(user.ident), |
---|
[1327] | 561 | "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 562 | "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 563 | "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 564 | "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident), |
---|
| 565 | ] : |
---|
| 566 | self.doModify(q) |
---|
| 567 | |
---|
| 568 | def deleteGroup(self, group) : |
---|
| 569 | """Completely deletes a group from the Quota Storage.""" |
---|
| 570 | for q in [ |
---|
| 571 | "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident), |
---|
| 572 | "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident), |
---|
| 573 | "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident), |
---|
| 574 | ] : |
---|
| 575 | self.doModify(q) |
---|
[1330] | 576 | |
---|
| 577 | def deletePrinter(self, printer) : |
---|
| 578 | """Completely deletes a printer from the Quota Storage.""" |
---|
| 579 | for q in [ |
---|
| 580 | "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)), |
---|
| 581 | "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 582 | "DELETE FROM grouppquota WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 583 | "DELETE FROM userpquota WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 584 | "DELETE FROM printers WHERE id=%s" % self.doQuote(printer.ident), |
---|
| 585 | ] : |
---|
| 586 | self.doModify(q) |
---|
[1327] | 587 | |
---|