root / pykota / trunk / pykota / storage.py @ 2340

Revision 2340, 30.5 kB (checked in by jerome, 19 years ago)

More work on billing codes.
Some typos fixed in Docstrings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[695]1# PyKota
[1144]2# -*- coding: ISO-8859-15 -*-
[695]3#
[952]4# PyKota : Print Quotas for CUPS and LPRng
[695]5#
[1257]6# (c) 2003-2004 Jerome Alet <alet@librelogiciel.com>
[873]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.
[695]11#
[873]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
[2302]19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[695]20#
21# $Id$
22#
[2066]23#
[695]24
[2266]25from mx import DateTime
26
[695]27class PyKotaStorageError(Exception):
28    """An exception for Quota Storage related stuff."""
29    def __init__(self, message = ""):
30        self.message = message
31        Exception.__init__(self, message)
32    def __repr__(self):
33        return self.message
34    __str__ = __repr__
[759]35       
[1041]36class StorageObject :
37    """Object present in the Quota Storage."""
38    def __init__(self, parent) :
39        "Initialize minimal data."""
40        self.parent = parent
41        self.ident = None
42        self.Exists = 0
43       
44class StorageUser(StorageObject) :       
45    """User class."""
46    def __init__(self, parent, name) :
47        StorageObject.__init__(self, parent)
48        self.Name = name
49        self.LimitBy = None
50        self.AccountBalance = None
51        self.LifeTimePaid = None
[1067]52        self.Email = None
[2054]53        self.OverCharge = 1.0
[1522]54        self.Payments = [] # TODO : maybe handle this smartly for SQL, for now just don't retrieve them
[1041]55       
56    def consumeAccountBalance(self, amount) :     
57        """Consumes an amount of money from the user's account balance."""
[1269]58        self.parent.decreaseUserAccountBalance(self, amount)
59        self.AccountBalance = float(self.AccountBalance or 0.0) - amount
[1041]60       
61    def setAccountBalance(self, balance, lifetimepaid) :   
62        """Sets the user's account balance in case he pays more money."""
[1522]63        diff = float(lifetimepaid or 0.0) - float(self.LifeTimePaid or 0.0)
64        self.parent.beginTransaction()
65        try :
66            self.parent.writeUserAccountBalance(self, balance, lifetimepaid)
67            self.parent.writeNewPayment(self, diff)
68        except PyKotaStorageError, msg :   
69            self.parent.rollbackTransaction()
70            raise PyKotaStorageError, msg
71        else :   
72            self.parent.commitTransaction()
73            self.AccountBalance = balance
74            self.LifeTimePaid = lifetimepaid
[1041]75       
76    def setLimitBy(self, limitby) :   
77        """Sets the user's limiting factor."""
[1062]78        try :
79            limitby = limitby.lower()
80        except AttributeError :   
81            limitby = "quota"
[1418]82        if limitby in ["quota", "balance", "quota-then-balance", "balance-then-quota"] :
[1041]83            self.parent.writeUserLimitBy(self, limitby)
84            self.LimitBy = limitby
85       
[2054]86    def setOverChargeFactor(self, factor) :   
87        """Sets the user's overcharging coefficient."""
88        self.parent.writeUserOverCharge(self, factor)
89        self.OverCharge = factor
90       
[1041]91    def delete(self) :   
92        """Deletes an user from the Quota Storage."""
93        self.parent.beginTransaction()
94        try :
95            self.parent.deleteUser(self)
96        except PyKotaStorageError, msg :   
97            self.parent.rollbackTransaction()
98            raise PyKotaStorageError, msg
99        else :   
100            self.parent.commitTransaction()
[2340]101            self.Exists = 0
[1041]102       
103class StorageGroup(StorageObject) :       
104    """User class."""
105    def __init__(self, parent, name) :
106        StorageObject.__init__(self, parent)
107        self.Name = name
108        self.LimitBy = None
109        self.AccountBalance = None
110        self.LifeTimePaid = None
111       
112    def setLimitBy(self, limitby) :   
113        """Sets the user's limiting factor."""
[1062]114        try :
115            limitby = limitby.lower()
116        except AttributeError :   
117            limitby = "quota"
[1041]118        if limitby in ["quota", "balance"] :
119            self.parent.writeGroupLimitBy(self, limitby)
120            self.LimitBy = limitby
121       
122    def delete(self) :   
123        """Deletes a group from the Quota Storage."""
124        self.parent.beginTransaction()
125        try :
126            self.parent.deleteGroup(self)
127        except PyKotaStorageError, msg :   
128            self.parent.rollbackTransaction()
129            raise PyKotaStorageError, msg
130        else :   
131            self.parent.commitTransaction()
[2340]132            self.Exists = 0
[1041]133       
134class StoragePrinter(StorageObject) :
135    """Printer class."""
136    def __init__(self, parent, name) :
137        StorageObject.__init__(self, parent)
138        self.Name = name
139        self.PricePerPage = None
140        self.PricePerJob = None
[1582]141        self.Description = None
[2054]142        self.Coefficients = None
[1041]143       
[1358]144    def __getattr__(self, name) :   
145        """Delays data retrieval until it's really needed."""
146        if name == "LastJob" : 
147            self.LastJob = self.parent.getPrinterLastJob(self)
148            return self.LastJob
149        else :
150            raise AttributeError, name
151           
[2217]152    def addJobToHistory(self, jobid, user, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None, jobmd5sum=None, jobpages=None, jobbilling=None) :
[1041]153        """Adds a job to the printer's history."""
[2217]154        self.parent.writeJobNew(self, user, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, clienthost, jobsizebytes, jobmd5sum, jobpages, jobbilling)
[1041]155        # TODO : update LastJob object ? Probably not needed.
156       
[1258]157    def addPrinterToGroup(self, printer) :   
158        """Adds a printer to a printer group."""
[1259]159        if (printer not in self.parent.getParentPrinters(self)) and (printer.ident != self.ident) :
[1258]160            self.parent.writePrinterToGroup(self, printer)
[1269]161            # TODO : reset cached value for printer parents, or add new parent to cached value
[1332]162           
163    def delPrinterFromGroup(self, printer) :   
164        """Deletes a printer from a printer group."""
[1333]165        self.parent.removePrinterFromGroup(self, printer)
166        # TODO : reset cached value for printer parents, or add new parent to cached value
[1258]167       
[1041]168    def setPrices(self, priceperpage = None, priceperjob = None) :   
169        """Sets the printer's prices."""
170        if priceperpage is None :
[1711]171            priceperpage = self.PricePerPage or 0.0
[1041]172        else :   
173            self.PricePerPage = float(priceperpage)
174        if priceperjob is None :   
[1711]175            priceperjob = self.PricePerJob or 0.0
[1041]176        else :   
177            self.PricePerJob = float(priceperjob)
178        self.parent.writePrinterPrices(self)
179       
[2340]180    def setDescription(self, description=None) :
181        """Sets the printer's description."""
[1582]182        if description is None :
183            description = self.Description
184        else :   
185            self.Description = str(description)
186        self.parent.writePrinterDescription(self)
187       
[1330]188    def delete(self) :   
189        """Deletes a printer from the Quota Storage."""
190        self.parent.beginTransaction()
191        try :
192            self.parent.deletePrinter(self)
193        except PyKotaStorageError, msg :   
194            self.parent.rollbackTransaction()
195            raise PyKotaStorageError, msg
196        else :   
197            self.parent.commitTransaction()
[2340]198            self.Exists = 0   
[1330]199       
[1041]200class StorageUserPQuota(StorageObject) :
201    """User Print Quota class."""
202    def __init__(self, parent, user, printer) :
203        StorageObject.__init__(self, parent)
204        self.User = user
205        self.Printer = printer
206        self.PageCounter = None
207        self.LifePageCounter = None
208        self.SoftLimit = None
209        self.HardLimit = None
210        self.DateLimit = None
[2054]211        self.WarnCount = None
[1041]212       
[1358]213    def __getattr__(self, name) :   
214        """Delays data retrieval until it's really needed."""
215        if name == "ParentPrintersUserPQuota" : 
216            self.ParentPrintersUserPQuota = (self.User.Exists and self.Printer.Exists and self.parent.getParentPrintersUserPQuota(self)) or []
217            return self.ParentPrintersUserPQuota
218        else :
219            raise AttributeError, name
220       
[1041]221    def setDateLimit(self, datelimit) :   
222        """Sets the date limit for this quota."""
223        date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)
224        self.parent.writeUserPQuotaDateLimit(self, date)
225        self.DateLimit = date
226       
227    def setLimits(self, softlimit, hardlimit) :   
228        """Sets the soft and hard limit for this quota."""
229        self.parent.writeUserPQuotaLimits(self, softlimit, hardlimit)
230        self.SoftLimit = softlimit
231        self.HardLimit = hardlimit
[1367]232        self.DateLimit = None
[2054]233        self.WarnCount = 0
[1041]234       
[1967]235    def setUsage(self, used) :
236        """Sets the PageCounter and LifePageCounter to used, or if used is + or - prefixed, changes the values of {Life,}PageCounter by that amount."""
237        vused = int(used)
238        if used.startswith("+") or used.startswith("-") :
[2030]239            self.parent.beginTransaction()
240            try :
241                self.parent.increaseUserPQuotaPagesCounters(self, vused)
242                self.parent.writeUserPQuotaDateLimit(self, None)
[2054]243                self.parent.writeUserPQuotaWarnCount(self, 0)
[2030]244            except PyKotaStorageError, msg :   
245                self.parent.rollbackTransaction()
246                raise PyKotaStorageError, msg
247            else :
248                self.parent.commitTransaction()
249            self.PageCounter += vused
250            self.LifePageCounter += vused
[1967]251        else :
[2030]252            self.parent.writeUserPQuotaPagesCounters(self, vused, vused)
253            self.PageCounter = self.LifePageCounter = vused
254        self.DateLimit = None
[2054]255        self.WarnCount = 0
[1967]256
[2066]257    def incDenyBannerCounter(self) :
258        """Increment the deny banner counter for this user quota."""
[2054]259        self.parent.increaseUserPQuotaWarnCount(self)
260        self.WarnCount = (self.WarnCount or 0) + 1
261       
[2066]262    def resetDenyBannerCounter(self) :
263        """Resets the deny banner counter for this user quota."""
264        self.parent.writeUserPQuotaWarnCount(self, 0)
265        self.WarnCount = 0
266       
[1041]267    def reset(self) :   
268        """Resets page counter to 0."""
269        self.parent.writeUserPQuotaPagesCounters(self, 0, int(self.LifePageCounter or 0))
270        self.PageCounter = 0
[2030]271        self.DateLimit = None
[1041]272       
[1755]273    def hardreset(self) :   
274        """Resets actual and life time page counters to 0."""
275        self.parent.writeUserPQuotaPagesCounters(self, 0, 0)
276        self.PageCounter = self.LifePageCounter = 0
[2030]277        self.DateLimit = None
[1755]278       
[1285]279    def computeJobPrice(self, jobsize) :   
280        """Computes the job price as the sum of all parent printers' prices + current printer's ones."""
281        totalprice = 0.0   
282        if jobsize :
[2054]283            if self.User.OverCharge != 0.0 :    # optimization, but TODO : beware of rounding errors
284                for upq in [ self ] + self.ParentPrintersUserPQuota :
285                    price = (float(upq.Printer.PricePerPage or 0.0) * jobsize) + float(upq.Printer.PricePerJob or 0.0)
286                    totalprice += price
287        if self.User.OverCharge != 1.0 : # TODO : beware of rounding errors
288            overcharged = totalprice * self.User.OverCharge       
289            self.parent.tool.printInfo("Overcharging %s by a factor of %s ===> User %s will be charged for %s units." % (totalprice, self.User.OverCharge, self.User.Name, overcharged))
290            return overcharged
291        else :   
292            return totalprice
[1285]293           
294    def increasePagesUsage(self, jobsize) :
[1041]295        """Increase the value of used pages and money."""
[1285]296        jobprice = self.computeJobPrice(jobsize)
297        if jobsize :
298            self.parent.beginTransaction()
299            try :
[1363]300                if jobprice :
301                    self.User.consumeAccountBalance(jobprice)
[1240]302                for upq in [ self ] + self.ParentPrintersUserPQuota :
[1285]303                    self.parent.increaseUserPQuotaPagesCounters(upq, jobsize)
304                    upq.PageCounter = int(upq.PageCounter or 0) + jobsize
305                    upq.LifePageCounter = int(upq.LifePageCounter or 0) + jobsize
[2340]306                # TODO : consume from the billing code as well
[1285]307            except PyKotaStorageError, msg :   
308                self.parent.rollbackTransaction()
309                raise PyKotaStorageError, msg
310            else :   
311                self.parent.commitTransaction()
312        return jobprice
[1041]313       
314class StorageGroupPQuota(StorageObject) :
315    """Group Print Quota class."""
316    def __init__(self, parent, group, printer) :
317        StorageObject.__init__(self, parent)
318        self.Group = group
319        self.Printer = printer
320        self.PageCounter = None
321        self.LifePageCounter = None
322        self.SoftLimit = None
323        self.HardLimit = None
324        self.DateLimit = None
325       
[1365]326    def __getattr__(self, name) :   
327        """Delays data retrieval until it's really needed."""
328        if name == "ParentPrintersGroupPQuota" : 
329            self.ParentPrintersGroupPQuota = (self.Group.Exists and self.Printer.Exists and self.parent.getParentPrintersGroupPQuota(self)) or []
330            return self.ParentPrintersGroupPQuota
331        else :
332            raise AttributeError, name
333       
[2030]334    def reset(self) :   
335        """Resets page counter to 0."""
336        self.parent.beginTransaction()
337        try :
338            for user in self.parent.getGroupMembers(self.Group) :
339                uq = self.parent.getUserPQuota(user, self.Printer)
340                uq.reset()
341            self.parent.writeGroupPQuotaDateLimit(self, None)
342        except PyKotaStorageError, msg :   
343            self.parent.rollbackTransaction()
344            raise PyKotaStorageError, msg
345        else :   
346            self.parent.commitTransaction()
347        self.PageCounter = 0
348        self.DateLimit = None
349       
350    def hardreset(self) :   
351        """Resets actual and life time page counters to 0."""
352        self.parent.beginTransaction()
353        try :
354            for user in self.parent.getGroupMembers(self.Group) :
355                uq = self.parent.getUserPQuota(user, self.Printer)
356                uq.hardreset()
357            self.parent.writeGroupPQuotaDateLimit(self, None)
358        except PyKotaStorageError, msg :   
359            self.parent.rollbackTransaction()
360            raise PyKotaStorageError, msg
361        else :   
362            self.parent.commitTransaction()
363        self.PageCounter = self.LifePageCounter = 0
364        self.DateLimit = None
365       
[1041]366    def setDateLimit(self, datelimit) :   
367        """Sets the date limit for this quota."""
368        date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)
369        self.parent.writeGroupPQuotaDateLimit(self, date)
370        self.DateLimit = date
371       
372    def setLimits(self, softlimit, hardlimit) :   
373        """Sets the soft and hard limit for this quota."""
374        self.parent.writeGroupPQuotaLimits(self, softlimit, hardlimit)
375        self.SoftLimit = softlimit
376        self.HardLimit = hardlimit
[1367]377        self.DateLimit = None
[1041]378       
[1274]379class StorageJob(StorageObject) :
[1692]380    """Printer's Job class."""
[1274]381    def __init__(self, parent) :
[1041]382        StorageObject.__init__(self, parent)
[1358]383        self.UserName = None
384        self.PrinterName = None
[1041]385        self.JobId = None
386        self.PrinterPageCounter = None
[1520]387        self.JobSizeBytes = None
[1041]388        self.JobSize = None
389        self.JobAction = None
390        self.JobDate = None
[1203]391        self.JobPrice = None
392        self.JobFileName = None
393        self.JobTitle = None
394        self.JobCopies = None
395        self.JobOptions = None
[1502]396        self.JobHostName = None
[2054]397        self.JobMD5Sum = None
398        self.JobPages = None
399        self.JobBillingCode = None
[1041]400       
[1358]401    def __getattr__(self, name) :   
402        """Delays data retrieval until it's really needed."""
403        if name == "User" : 
404            self.User = self.parent.getUser(self.UserName)
405            return self.User
406        elif name == "Printer" :   
407            self.Printer = self.parent.getPrinter(self.PrinterName)
408            return self.Printer
409        else :
410            raise AttributeError, name
411       
[1274]412class StorageLastJob(StorageJob) :
413    """Printer's Last Job class."""
414    def __init__(self, parent, printer) :
415        StorageJob.__init__(self, parent)
[1359]416        self.PrinterName = printer.Name # not needed
[1274]417        self.Printer = printer
418       
[2340]419class StorageBillingCode(StorageObject) :
420    """Billing code class."""
421    def __init__(self, parent, name) :
422        StorageObject.__init__(self, parent)
423        self.BillingCode = name
424        self.Description = None
425        self.PageCounter = None
426        self.Balance = None
427       
428    def delete(self) :   
429        """Deletes the billing code from the database."""
430        self.parent.deleteBillingCode(self)
431        self.Exists = 0
432       
433    def reset(self, pagecounter=0, balance=0.0) :   
434        """Resets the pagecounter and balance for this billing code."""
435        self.parent.setBillingCodeValues(self, pagecounter, balance)
436        self.PageCounter = pagecounter
437        self.Balance = balance
438       
439    def setDescription(self, description=None) :
440        """Modifies the description for this billing code."""
441        if description is None :
442            description = self.Description
443        else :   
444            self.Description = str(description)
445        self.parent.writeBillingCodeDescription(self)
446       
447    def consume(self, pages, price) :
448        """Consumes some pages and credits for this billing code."""
449        if pages :
450           self.parent.consumeBillingCode(self, pages, price)
451           self.PageCounter += pages
452           self.Balance -= price
453       
[1130]454class BaseStorage :
455    def __init__(self, pykotatool) :
[1523]456        """Opens the storage connection."""
[1130]457        self.closed = 1
458        self.tool = pykotatool
459        self.usecache = pykotatool.config.getCaching()
[1148]460        self.disablehistory = pykotatool.config.getDisableHistory()
[1875]461        self.privacy = pykotatool.config.getPrivacy()
462        if self.privacy :
463            pykotatool.logdebug("Jobs' title, filename and options will be hidden because of privacy concerns.")
[1130]464        if self.usecache :
465            self.tool.logdebug("Caching enabled.")
[1252]466            self.caches = { "USERS" : {}, "GROUPS" : {}, "PRINTERS" : {}, "USERPQUOTAS" : {}, "GROUPPQUOTAS" : {}, "JOBS" : {}, "LASTJOBS" : {} }
[1130]467       
[1240]468    def close(self) :   
469        """Must be overriden in children classes."""
470        raise RuntimeError, "BaseStorage.close() must be overriden !"
471       
[1130]472    def __del__(self) :       
473        """Ensures that the database connection is closed."""
474        self.close()
475       
476    def getFromCache(self, cachetype, key) :
477        """Tries to extract something from the cache."""
478        if self.usecache :
479            entry = self.caches[cachetype].get(key)
480            if entry is not None :
481                self.tool.logdebug("Cache hit (%s->%s)" % (cachetype, key))
482            else :   
483                self.tool.logdebug("Cache miss (%s->%s)" % (cachetype, key))
[1743]484            return entry   
[1130]485           
486    def cacheEntry(self, cachetype, key, value) :       
487        """Puts an entry in the cache."""
[1151]488        if self.usecache and getattr(value, "Exists", 0) :
[1130]489            self.caches[cachetype][key] = value
[1132]490            self.tool.logdebug("Cache store (%s->%s)" % (cachetype, key))
[1130]491           
492    def getUser(self, username) :       
493        """Returns the user from cache."""
494        user = self.getFromCache("USERS", username)
495        if user is None :
496            user = self.getUserFromBackend(username)
497            self.cacheEntry("USERS", username, user)
498        return user   
499       
500    def getGroup(self, groupname) :       
501        """Returns the group from cache."""
502        group = self.getFromCache("GROUPS", groupname)
503        if group is None :
504            group = self.getGroupFromBackend(groupname)
505            self.cacheEntry("GROUPS", groupname, group)
506        return group   
507       
508    def getPrinter(self, printername) :       
509        """Returns the printer from cache."""
510        printer = self.getFromCache("PRINTERS", printername)
511        if printer is None :
512            printer = self.getPrinterFromBackend(printername)
513            self.cacheEntry("PRINTERS", printername, printer)
514        return printer   
515       
516    def getUserPQuota(self, user, printer) :       
517        """Returns the user quota information from cache."""
518        useratprinter = "%s@%s" % (user.Name, printer.Name)
519        upquota = self.getFromCache("USERPQUOTAS", useratprinter)
520        if upquota is None :
521            upquota = self.getUserPQuotaFromBackend(user, printer)
522            self.cacheEntry("USERPQUOTAS", useratprinter, upquota)
523        return upquota   
524       
525    def getGroupPQuota(self, group, printer) :       
526        """Returns the group quota information from cache."""
527        groupatprinter = "%s@%s" % (group.Name, printer.Name)
528        gpquota = self.getFromCache("GROUPPQUOTAS", groupatprinter)
529        if gpquota is None :
530            gpquota = self.getGroupPQuotaFromBackend(group, printer)
531            self.cacheEntry("GROUPPQUOTAS", groupatprinter, gpquota)
532        return gpquota   
533       
534    def getPrinterLastJob(self, printer) :       
535        """Extracts last job information for a given printer from cache."""
536        lastjob = self.getFromCache("LASTJOBS", printer.Name)
537        if lastjob is None :
538            lastjob = self.getPrinterLastJobFromBackend(printer)
539            self.cacheEntry("LASTJOBS", printer.Name, lastjob)
540        return lastjob   
541       
[1249]542    def getParentPrinters(self, printer) :   
543        """Extracts parent printers information for a given printer from cache."""
[1252]544        if self.usecache :
545            if not hasattr(printer, "Parents") :
546                self.tool.logdebug("Cache miss (%s->Parents)" % printer.Name)
547                printer.Parents = self.getParentPrintersFromBackend(printer)
548                self.tool.logdebug("Cache store (%s->Parents)" % printer.Name)
549            else :
550                self.tool.logdebug("Cache hit (%s->Parents)" % printer.Name)
551        else :       
552            printer.Parents = self.getParentPrintersFromBackend(printer)
[1364]553        for parent in printer.Parents[:] :   
554            printer.Parents.extend(self.getParentPrinters(parent))
[1368]555        uniquedic = {}   
556        for parent in printer.Parents :
557            uniquedic[parent.Name] = parent
558        printer.Parents = uniquedic.values()   
[1252]559        return printer.Parents
[1249]560       
[1137]561    def getGroupMembers(self, group) :       
562        """Returns the group's members list from in-group cache."""
563        if self.usecache :
564            if not hasattr(group, "Members") :
565                self.tool.logdebug("Cache miss (%s->Members)" % group.Name)
566                group.Members = self.getGroupMembersFromBackend(group)
567                self.tool.logdebug("Cache store (%s->Members)" % group.Name)
568            else :
569                self.tool.logdebug("Cache hit (%s->Members)" % group.Name)
570        else :       
571            group.Members = self.getGroupMembersFromBackend(group)
572        return group.Members   
573       
574    def getUserGroups(self, user) :       
575        """Returns the user's groups list from in-user cache."""
576        if self.usecache :
577            if not hasattr(user, "Groups") :
578                self.tool.logdebug("Cache miss (%s->Groups)" % user.Name)
579                user.Groups = self.getUserGroupsFromBackend(user)
580                self.tool.logdebug("Cache store (%s->Groups)" % user.Name)
581            else :
582                self.tool.logdebug("Cache hit (%s->Groups)" % user.Name)
583        else :       
584            user.Groups = self.getUserGroupsFromBackend(user)
585        return user.Groups   
586       
[1240]587    def getParentPrintersUserPQuota(self, userpquota) :     
[1364]588        """Returns all user print quota on the printer and all its parents recursively."""
[1365]589        upquotas = [ ]
[1240]590        for printer in self.getParentPrinters(userpquota.Printer) :
[1395]591            upq = self.getUserPQuota(userpquota.User, printer)
592            if upq.Exists :
593                upquotas.append(upq)
[1240]594        return upquotas       
595       
[1365]596    def getParentPrintersGroupPQuota(self, grouppquota) :     
597        """Returns all group print quota on the printer and all its parents recursively."""
598        gpquotas = [ ]
599        for printer in self.getParentPrinters(grouppquota.Printer) :
[1395]600            gpq = self.getGroupPQuota(grouppquota.Group, printer)
601            if gpq.Exists :
602                gpquotas.append(gpq)
[1365]603        return gpquotas       
604       
[1790]605    def databaseToUserCharset(self, text) :
606        """Converts from database format (UTF-8) to user's charset."""
607        if text is not None :
608            try :
609                return unicode(text, "UTF-8").encode(self.tool.getCharset()) 
610            except UnicodeError :   
[1791]611                try :
612                    # Incorrect locale settings ?
613                    return unicode(text, "UTF-8").encode("ISO-8859-15") 
614                except UnicodeError :   
615                    pass
[1790]616        return text
617       
618    def userCharsetToDatabase(self, text) :
619        """Converts from user's charset to database format (UTF-8)."""
620        if text is not None :
621            try :
622                return unicode(text, self.tool.getCharset()).encode("UTF-8") 
623            except UnicodeError :   
[1791]624                try :
625                    # Incorrect locale settings ?
626                    return unicode(text, "ISO-8859-15").encode("UTF-8") 
627                except UnicodeError :   
628                    pass
[1790]629        return text
630       
[2266]631    def cleanDates(self, startdate, enddate) :   
632        """Clean the dates to create a correct filter."""
633        if startdate is None :
634            startdate = enddate
635        if enddate is None :   
636            enddate = startdate
637        if (startdate is None) and (enddate is None) :   
638            return (None, None)
[2268]639           
640        now = DateTime.now()   
641        nameddates = ('yesterday', 'today', 'now', 'tomorrow')
[2276]642        datedict = { "start" : startdate.lower(), "end" : enddate.lower() }   
643        for limit in datedict.keys() :
644            dateval = datedict[limit]
645            for name in nameddates :
646                if dateval.startswith(name) :
647                    try :
648                        offset = int(dateval[len(name):])
649                    except :   
650                        offset = 0
651                    dateval = dateval[:len(name)]   
[2268]652                    if limit == "start" :
653                        if dateval == "yesterday" :
[2276]654                            dateval = (now - 1 + offset).Format("%Y%m%d000000")
[2268]655                        elif dateval == "today" :
[2276]656                            dateval = (now + offset).Format("%Y%m%d000000")
[2268]657                        elif dateval == "now" :
[2276]658                            dateval = (now + offset).Format("%Y%m%d%H%M%S")
[2268]659                        else : # tomorrow
[2276]660                            dateval = (now + 1 + offset).Format("%Y%m%d000000")
[2268]661                    else :
662                        if dateval == "yesterday" :
[2276]663                            dateval = (now - 1 + offset).Format("%Y%m%d235959")
[2268]664                        elif dateval == "today" :
[2276]665                            dateval = (now + offset).Format("%Y%m%d235959")
[2268]666                        elif dateval == "now" :
[2276]667                            dateval = (now + offset).Format("%Y%m%d%H%M%S")
[2268]668                        else : # tomorrow
[2276]669                            dateval = (now + 1 + offset).Format("%Y%m%d235959")
670                    break
671                   
672            if not dateval.isdigit() :
673                dateval = None
674            else :   
[2266]675                lgdateval = len(dateval)
676                if lgdateval == 4 :
677                    if limit == "start" : 
678                        dateval = "%s0101 00:00:00" % dateval
679                    else : 
680                        dateval = "%s1231 23:59:59" % dateval
681                elif lgdateval == 6 :
682                    if limit == "start" : 
683                        dateval = "%s01 00:00:00" % dateval
684                    else : 
685                        mxdate = DateTime.ISO.ParseDateTime("%s01 00:00:00" % dateval)
686                        dateval = "%s%02i 23:59:59" % (dateval, mxdate.days_in_month)
687                elif lgdateval == 8 :
688                    if limit == "start" : 
689                        dateval = "%s 00:00:00" % dateval
690                    else : 
691                        dateval = "%s 23:59:59" % dateval
692                elif lgdateval == 10 :
693                    if limit == "start" : 
694                        dateval = "%s %s:00:00" % (dateval[:8], dateval[8:])
695                    else : 
696                        dateval = "%s %s:59:59" % (dateval[:8], dateval[8:])
697                elif lgdateval == 12 :
698                    if limit == "start" : 
699                        dateval = "%s %s:%s:00" % (dateval[:8], dateval[8:10], dateval[10:])
700                    else : 
701                        dateval = "%s %s:%s:59" % (dateval[:8], dateval[8:10], dateval[10:])
702                elif lgdateval == 14 :       
703                    dateval = "%s %s:%s:%s" % (dateval[:8], dateval[8:10], dateval[10:12], dateval[12:])
704                else :   
705                    dateval = None
706                try :   
707                    DateTime.ISO.ParseDateTime(dateval)
708                except :   
709                    dateval = None
[2276]710            datedict[limit] = dateval   
711        (start, end) = (datedict["start"], datedict["end"])
712        if start > end :
713            (start, end) = (end, start)
714        return (start, end)   
[2266]715       
[1021]716def openConnection(pykotatool) :
[695]717    """Returns a connection handle to the appropriate Quota Storage Database."""
[1021]718    backendinfo = pykotatool.config.getStorageBackend()
[800]719    backend = backendinfo["storagebackend"]
[695]720    try :
[1219]721        exec "from pykota.storages import %s as storagebackend" % backend.lower()
[695]722    except ImportError :
[773]723        raise PyKotaStorageError, _("Unsupported quota storage backend %s") % backend
[695]724    else :   
[800]725        host = backendinfo["storageserver"]
726        database = backendinfo["storagename"]
[1087]727        admin = backendinfo["storageadmin"] or backendinfo["storageuser"]
728        adminpw = backendinfo["storageadminpw"] or backendinfo["storageuserpw"]
[1240]729        return storagebackend.Storage(pykotatool, host, database, admin, adminpw)
Note: See TracBrowser for help on using the browser.