| 89 | class StorageObject : |
| 90 | """Object present in the Quota Storage.""" |
| 91 | def __init__(self, parent) : |
| 92 | "Initialize minimal data.""" |
| 93 | self.parent = parent |
| 94 | self.ident = None |
| 95 | self.Exists = 0 |
| 96 | |
| 97 | class StorageUser(StorageObject) : |
| 98 | """User class.""" |
| 99 | def __init__(self, parent, name) : |
| 100 | StorageObject.__init__(self, parent) |
| 101 | self.Name = name |
| 102 | self.LimitBy = None |
| 103 | self.AccountBalance = None |
| 104 | self.LifeTimePaid = None |
| 105 | |
| 106 | def consumeAccountBalance(self, amount) : |
| 107 | """Consumes an amount of money from the user's account balance.""" |
| 108 | newbalance = float(self.AccountBalance or 0.0) - amount |
| 109 | self.parent.writeUserAccountBalance(self, newbalance) |
| 110 | self.AccountBalance = newbalance |
| 111 | |
| 112 | def setAccountBalance(self, balance, lifetimepaid) : |
| 113 | """Sets the user's account balance in case he pays more money.""" |
| 114 | |
| 115 | def setLimitBy(self, limitby) : |
| 116 | """Sets the user's limiting factor.""" |
| 117 | limitby = limitby.lower() |
| 118 | if limitby in ["quota", "balance"] : |
| 119 | self.parent.writeUserLimitBy(self, limitby) |
| 120 | self.LimitBy = limitby |
| 121 | |
| 122 | def delete(self) : |
| 123 | """Deletes an user from the Quota Storage.""" |
| 124 | self.parent.beginTransaction() |
| 125 | try : |
| 126 | self.parent.deleteUser(self) |
| 127 | except PyKotaStorageError, msg : |
| 128 | self.parent.rollbackTransaction() |
| 129 | raise PyKotaStorageError, msg |
| 130 | else : |
| 131 | self.parent.commitTransaction() |
| 132 | |
| 133 | class StorageGroup(StorageObject) : |
| 134 | """User class.""" |
| 135 | def __init__(self, parent, name) : |
| 136 | StorageObject.__init__(self, parent) |
| 137 | self.Name = name |
| 138 | self.LimitBy = None |
| 139 | self.AccountBalance = None |
| 140 | self.LifeTimePaid = None |
| 141 | |
| 142 | def setLimitBy(self, limitby) : |
| 143 | """Sets the user's limiting factor.""" |
| 144 | limitby = limitby.lower() |
| 145 | if limitby in ["quota", "balance"] : |
| 146 | self.parent.writeGroupLimitBy(self, limitby) |
| 147 | self.LimitBy = limitby |
| 148 | |
| 149 | def delete(self) : |
| 150 | """Deletes a group from the Quota Storage.""" |
| 151 | self.parent.beginTransaction() |
| 152 | try : |
| 153 | self.parent.deleteGroup(self) |
| 154 | except PyKotaStorageError, msg : |
| 155 | self.parent.rollbackTransaction() |
| 156 | raise PyKotaStorageError, msg |
| 157 | else : |
| 158 | self.parent.commitTransaction() |
| 159 | |
| 160 | class StoragePrinter(StorageObject) : |
| 161 | """Printer class.""" |
| 162 | def __init__(self, parent, name) : |
| 163 | StorageObject.__init__(self, parent) |
| 164 | self.Name = name |
| 165 | self.PricePerPage = None |
| 166 | self.PricePerJob = None |
| 167 | self.LastJob = None |
| 168 | |
| 169 | def addJobToHistory(self, jobid, user, pagecounter, action, jobsize=None) : |
| 170 | """Adds a job to the printer's history.""" |
| 171 | self.parent.writeJobNew(self, user, jobid, pagecounter, action, jobsize) |
| 172 | # TODO : update LastJob object ? Probably not needed. |
| 173 | |
| 174 | def setPrices(self, priceperpage = None, priceperjob = None) : |
| 175 | """Sets the printer's prices.""" |
| 176 | if priceperpage is None : |
| 177 | priceperpage = self.PricePerPage |
| 178 | else : |
| 179 | self.PricePerPage = float(priceperpage) |
| 180 | if priceperjob is None : |
| 181 | priceperjob = self.PricePerJob |
| 182 | else : |
| 183 | self.PricePerJob = float(priceperjob) |
| 184 | self.parent.writePrinterPrices(self) |
| 185 | |
| 186 | class StorageUserPQuota(StorageObject) : |
| 187 | """User Print Quota class.""" |
| 188 | def __init__(self, parent, user, printer) : |
| 189 | StorageObject.__init__(self, parent) |
| 190 | self.User = user |
| 191 | self.Printer = printer |
| 192 | self.PageCounter = None |
| 193 | self.LifePageCounter = None |
| 194 | self.SoftLimit = None |
| 195 | self.HardLimit = None |
| 196 | self.DateLimit = None |
| 197 | |
| 198 | def setDateLimit(self, datelimit) : |
| 199 | """Sets the date limit for this quota.""" |
| 200 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
| 201 | self.parent.writeUserPQuotaDateLimit(self, date) |
| 202 | self.DateLimit = date |
| 203 | |
| 204 | def setLimits(self, softlimit, hardlimit) : |
| 205 | """Sets the soft and hard limit for this quota.""" |
| 206 | self.parent.writeUserPQuotaLimits(self, softlimit, hardlimit) |
| 207 | self.SoftLimit = softlimit |
| 208 | self.HardLimit = hardlimit |
| 209 | |
| 210 | def reset(self) : |
| 211 | """Resets page counter to 0.""" |
| 212 | self.parent.writeUserPQuotaPagesCounters(self, 0, int(self.LifePageCounter or 0)) |
| 213 | self.PageCounter = 0 |
| 214 | |
| 215 | def increasePagesUsage(self, nbpages) : |
| 216 | """Increase the value of used pages and money.""" |
| 217 | if nbpages : |
| 218 | jobprice = (float(self.Printer.PricePerPage or 0.0) * nbpages) + float(self.Printer.PricePerJob or 0.0) |
| 219 | newpagecounter = int(self.PageCounter or 0) + nbpages |
| 220 | newlifepagecounter = int(self.LifePageCounter or 0) + nbpages |
| 221 | self.parent.beginTransaction() |
| 222 | try : |
| 223 | if jobprice : # optimization : don't access the database if unneeded. |
| 224 | self.User.consumeAccountBalance(jobprice) |
| 225 | self.parent.writeUserPQuotaPagesCounters(self, newpagecounter, newlifepagecounter) |
| 226 | except PyKotaStorageError, msg : |
| 227 | self.parent.rollbackTransaction() |
| 228 | raise PyKotaStorageError, msg |
| 229 | else : |
| 230 | self.parent.commitTransaction() |
| 231 | self.PageCounter = newpagecounter |
| 232 | self.LifePageCounter = newlifepagecounter |
| 233 | |
| 234 | class StorageGroupPQuota(StorageObject) : |
| 235 | """Group Print Quota class.""" |
| 236 | def __init__(self, parent, group, printer) : |
| 237 | StorageObject.__init__(self, parent) |
| 238 | self.Group = group |
| 239 | self.Printer = printer |
| 240 | self.PageCounter = None |
| 241 | self.LifePageCounter = None |
| 242 | self.SoftLimit = None |
| 243 | self.HardLimit = None |
| 244 | self.DateLimit = None |
| 245 | |
| 246 | def setDateLimit(self, datelimit) : |
| 247 | """Sets the date limit for this quota.""" |
| 248 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
| 249 | self.parent.writeGroupPQuotaDateLimit(self, date) |
| 250 | self.DateLimit = date |
| 251 | |
| 252 | def setLimits(self, softlimit, hardlimit) : |
| 253 | """Sets the soft and hard limit for this quota.""" |
| 254 | self.parent.writeGroupPQuotaLimits(self, softlimit, hardlimit) |
| 255 | self.SoftLimit = softlimit |
| 256 | self.HardLimit = hardlimit |
| 257 | |
| 258 | class StorageLastJob(StorageObject) : |
| 259 | """Printer's Last Job class.""" |
| 260 | def __init__(self, parent, printer) : |
| 261 | StorageObject.__init__(self, parent) |
| 262 | self.Printer = printer |
| 263 | self.JobId = None |
| 264 | self.User = None |
| 265 | self.PrinterPageCounter = None |
| 266 | self.JobSize = None |
| 267 | self.JobAction = None |
| 268 | self.JobDate = None |
| 269 | |
| 270 | def setSize(self, jobsize) : |
| 271 | """Sets the last job's size.""" |
| 272 | self.parent.writeLastJobSize(self, jobsize) |
| 273 | self.JobSize = jobsize |
| 274 | |