114 | | def addPrinter(self, printername) : |
115 | | """Adds a printer to the quota storage.""" |
116 | | self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) |
117 | | |
118 | | def getPrinterUsers(self, printername) : |
| 117 | def getPrinterId(self, printername) : |
| 118 | """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 | |
| 125 | def getUserId(self, username) : |
| 126 | """Returns a userid given a username.""" |
| 127 | result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) |
| 128 | try : |
| 129 | return self.doParseResult(result)[0]["id"] |
| 130 | except TypeError : # Not found |
| 131 | return |
| 132 | |
| 133 | def getGroupId(self, groupname) : |
| 134 | """Returns a groupid given a grupname.""" |
| 135 | result = self.doQuery("SELECT id FROM groups WHERE groupname=%s;" % self.doQuote(groupname)) |
| 136 | try : |
| 137 | return self.doParseResult(result)[0]["id"] |
| 138 | except TypeError : # Not found |
| 139 | return |
| 140 | |
| 141 | def getJobHistoryId(self, jobid, userid, printerid) : |
| 142 | """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))) |
| 144 | try : |
| 145 | return self.doParseResult(result)[0]["id"] |
| 146 | except TypeError : # Not found |
| 147 | return |
| 148 | |
| 149 | def getPrinterUsers(self, printerid) : |
134 | | return [record["groupname"] for record in result] |
135 | | |
136 | | def getUserId(self, username) : |
137 | | """Returns a userid given a username.""" |
138 | | result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) |
139 | | try : |
140 | | return self.doParseResult(result)[0]["id"] |
141 | | except TypeError : # Not found |
142 | | return |
143 | | |
144 | | def getPrinterId(self, printername) : |
145 | | """Returns a printerid given a printername.""" |
146 | | result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
147 | | try : |
148 | | return self.doParseResult(result)[0]["id"] |
| 165 | return [(record["id"], record["groupname"]) for record in result] |
| 166 | |
| 167 | def addPrinter(self, printername) : |
| 168 | """Adds a printer to the quota storage, returns its id.""" |
| 169 | self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) |
| 170 | return self.getPrinterId(printername) |
| 171 | |
| 172 | def addUser(self, username) : |
| 173 | """Adds a user to the quota storage, returns its id.""" |
| 174 | self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) |
| 175 | return self.getUserId(username) |
| 176 | |
| 177 | def addGroup(self, groupname) : |
| 178 | """Adds a group to the quota storage, returns its id.""" |
| 179 | self.doQuery("INSERT INTO groups (groupname) VALUES (%s);" % self.doQuote(groupname)) |
| 180 | return self.getGroupId(groupname) |
| 181 | |
| 182 | def addUserPQuota(self, username, printerid) : |
| 183 | """Initializes a user print quota on a printer, adds the user to the quota storage if needed.""" |
| 184 | userid = self.getUserId(username) |
| 185 | if userid is None : |
| 186 | userid = self.addUser(username) |
| 187 | self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) |
| 188 | return (userid, printerid) |
| 189 | |
| 190 | def addGroupPQuota(self, groupname, printerid) : |
| 191 | """Initializes a group print quota on a printer, adds the group to the quota storage if needed.""" |
| 192 | groupid = self.getGroupId(groupname) |
| 193 | if groupid is None : |
| 194 | groupid = self.addUser(groupname) |
| 195 | self.doQuery("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s);" % (self.doQuote(groupid), self.doQuote(printerid))) |
| 196 | return (groupid, printerid) |
| 197 | |
| 198 | def setUserPQuota(self, userid, printerid, softlimit, hardlimit) : |
| 199 | """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))) |
| 201 | |
| 202 | def resetUserPQuota(self, userid, printerid) : |
| 203 | """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))) |
| 205 | |
| 206 | def updateUserPQuota(self, userid, printerid, pagecount) : |
| 207 | """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))) |
| 209 | |
| 210 | def getUserPQuota(self, userid, printerid) : |
| 211 | """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))) |
| 213 | try : |
| 214 | return self.doParseResult(result)[0] |
151 | | |
152 | | def getPrinterPageCounter(self, printername) : |
153 | | """Returns the last page counter value for a printer given its name.""" |
154 | | result = self.doQuery("SELECT pagecounter, lastjobid, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
| 217 | |
| 218 | def setUserDateLimit(self, userid, printerid, datelimit) : |
| 219 | """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))) |
| 221 | |
| 222 | def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) : |
| 223 | """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))) |
| 225 | return self.getJobHistoryId(jobid, userid, printerid) # in case jobid is not sufficient |
| 226 | |
| 227 | def updateJobSizeInHistory(self, historyid, jobsize) : |
| 228 | """Updates a job size in the history given the history line's id.""" |
| 229 | self.doQuery("UPDATE jobhistory SET jobsize=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(historyid))) |
| 230 | |
| 231 | def getPrinterPageCounter(self, printerid) : |
| 232 | """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)) |
160 | | def updatePrinterPageCounter(self, printername, username, pagecount, jobid) : |
161 | | """Updates the last page counter information for a printer given its name, last username, pagecount and jobid.""" |
162 | | return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s, lastjobid=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(jobid), self.doQuote(printername))) |
163 | | |
164 | | def addUserPQuota(self, username, printername) : |
165 | | """Initializes a user print quota on a printer, adds the printer and the user to the quota storage if needed.""" |
166 | | (userid, printerid) = self.getUPIds(username, printername) |
167 | | if printerid is None : |
168 | | self.addPrinter(printername) # should we still add it ? |
169 | | if userid is None : |
170 | | self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) |
171 | | (userid, printerid) = self.getUPIds(username, printername) |
172 | | if (userid is not None) and (printerid is not None) : |
173 | | return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) |
174 | | |
175 | | def getUPIds(self, username, printername) : |
176 | | """Returns a tuple (userid, printerid) given a username and a printername.""" |
177 | | return (self.getUserId(username), self.getPrinterId(printername)) |
178 | | |
179 | | def getUserPQuota(self, username, printername) : |
180 | | """Returns the Print Quota information for a given (username, printername).""" |
181 | | (userid, printerid) = self.getUPIds(username, printername) |
182 | | if (userid is not None) and (printerid is not None) : |
183 | | result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) |
184 | | try : |
185 | | return self.doParseResult(result)[0] |
186 | | except TypeError : # Not found |
187 | | pass |
188 | | |
189 | | def setUserPQuota(self, username, printername, softlimit, hardlimit) : |
190 | | """Sets soft and hard limits for a user quota on a specific printer given (username, printername).""" |
191 | | (userid, printerid) = self.getUPIds(username, printername) |
192 | | if (userid is not None) and (printerid is not None) : |
193 | | 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))) |
194 | | |
195 | | def resetUserPQuota(self, username, printername) : |
196 | | """Resets the page counter to zero. Life time page counter is kept unchanged.""" |
197 | | (userid, printerid) = self.getUPIds(username, printername) |
198 | | if (userid is not None) and (printerid is not None) : |
199 | | self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) |
200 | | |
201 | | def setDateLimit(self, username, printername, datelimit) : |
202 | | """Sets the limit date for a soft limit to become an hard one given (username, printername).""" |
203 | | (userid, printerid) = self.getUPIds(username, printername) |
204 | | if (userid is not None) and (printerid is not None) : |
205 | | 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))) |
206 | | |
207 | | def updateUserPQuota(self, username, printername, pagecount) : |
208 | | """Updates the used user Quota information given (username, printername) and a job size in pages.""" |
209 | | (userid, printerid) = self.getUPIds(username, printername) |
210 | | if (userid is not None) and (printerid is not None) : |
211 | | 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))) |
212 | | |