Changeset 3056

Show
Ignore:
Timestamp:
11/13/06 23:24:01 (17 years ago)
Author:
jerome
Message:

The code to refund jobs is there and works (at least with PostgreSQL).
Only the pkrefund command line tool (and CGI script ?) is missing.

Location:
pykota/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/cgi-bin/printquota.cgi

    r3055 r3056  
    210210                    billingcode = None 
    211211                self.report = ["<h2>%s</h2>" % _("History")]     
    212                 history = self.storage.retrieveHistory(user, printer, hostname, billingcode, end=datelimit) 
     212                history = self.storage.retrieveHistory(user=user, printer=printer, hostname=hostname, billingcode=billingcode, end=datelimit) 
    213213                if not history : 
    214214                    self.report.append("<h3>%s</h3>" % _("Empty")) 
  • pykota/trunk/pykota/storage.py

    r3050 r3056  
    2323# 
    2424 
     25"""This module is the database abstraction layer for PyKota.""" 
     26 
    2527import os 
    2628import imp 
     
    3638    __str__ = __repr__ 
    3739         
     40         
    3841class StorageObject : 
    3942    """Object present in the database.""" 
     
    5760            getattr(self.parent, "save%s" % self.__class__.__name__[7:])(self) 
    5861            self.isDirty = False 
     62             
    5963         
    6064class StorageUser(StorageObject) :         
     
    124128        self.isDirty = False             
    125129         
     130    def refund(self, amount) : 
     131        """Refunds a number of credits to an user.""" 
     132        self.consumeAccountBalance(-amount) 
     133         
     134         
    126135class StorageGroup(StorageObject) :         
    127136    """User class.""" 
     
    161170        self.Exists = False 
    162171        self.isDirty = False             
     172         
    163173         
    164174class StoragePrinter(StorageObject) : 
     
    236246        self.Exists = False 
    237247        self.isDirty = False             
     248         
    238249         
    239250class StorageUserPQuota(StorageObject) : 
     
    360371        self.isDirty = False 
    361372         
     373    def refund(self, nbpages) :     
     374        """Refunds a number of pages to an user on a particular printer.""" 
     375        self.parent.increaseUserPQuotaPagesCounters(self, -nbpages) 
     376        self.PageCounter = int(self.PageCounter or 0) - nbpages 
     377        self.LifePageCounter = int(self.LifePageCounter or 0) - nbpages 
     378         
     379         
    362380class StorageGroupPQuota(StorageObject) : 
    363381    """Group Print Quota class.""" 
     
    427445        self.Exists = False 
    428446        self.isDirty = False 
     447         
    429448         
    430449class StorageJob(StorageObject) : 
     
    462481        else : 
    463482            raise AttributeError, name 
     483             
     484    def refund(self) :         
     485        """Refund a particular print job.""" 
     486        if (not self.JobSize) or (self.JobAction in ("DENY", "REFUND")) : 
     487            return 
     488        self.parent.beginTransaction() 
     489        try : 
     490            if self.JobBillingCode : 
     491                bcode = self.parent.getBillingCode(self.JobBillingCode) 
     492                bcode.refund(self.JobSize, self.JobPrice) 
     493                 
     494            if self.User.Exists : 
     495                self.User.refund(self.JobPrice) 
     496                if self.Printer.Exists :     
     497                    upq = self.parent.getUserPQuota(self.User, self.Printer)     
     498                    if upq.Exists : 
     499                        upq.refund(self.JobSize) 
     500            self.parent.refundJob(self.ident) 
     501        except :         
     502            self.parent.rollbackTransaction() 
     503            raise 
     504        else :     
     505            self.parent.commitTransaction() 
     506         
    464507         
    465508class StorageLastJob(StorageJob) : 
     
    469512        self.PrinterName = printer.Name # not needed 
    470513        self.Printer = printer 
     514         
    471515         
    472516class StorageBillingCode(StorageObject) : 
     
    494538        """Consumes some pages and credits for this billing code.""" 
    495539        if pages : 
    496            self.parent.consumeBillingCode(self, pages, price) 
    497            self.PageCounter += pages 
    498            self.Balance -= price 
     540            self.parent.consumeBillingCode(self, pages, price) 
     541            self.PageCounter += pages 
     542            self.Balance -= price 
     543            
     544    def refund(self, pages, price) : 
     545        """Refunds a particular billing code.""" 
     546        self.consume(-pages, -price) 
     547         
    499548         
    500549class BaseStorage : 
  • pykota/trunk/pykota/storages/ldapstorage.py

    r2953 r3056  
    13031303            self.doModify(pgroup.ident, fields)          
    13041304             
    1305     def retrieveHistory(self, user=None, printer=None, hostname=None, billingcode=None, limit=100, start=None, end=None) : 
     1305    def retrieveHistory(self, user=None, printer=None, hostname=None, billingcode=None, jobid=None, limit=100, start=None, end=None) : 
    13061306        """Retrieves all print jobs for user on printer (or all) between start and end date, limited to first 100 results.""" 
    13071307        precond = "(objectClass=pykotaJob)" 
     
    13151315        if billingcode is not None : 
    13161316            where.append("(pykotaBillingCode=%s)" % self.userCharsetToDatabase(billingcode)) 
     1317        if jobid is not None : 
     1318            where.append("(pykotaJobId=%s)" % jobid) # TODO : jobid is text, so self.userCharsetToDatabase(jobid) but do all of them as well. 
    13171319        if where :     
    13181320            where = "(&%s)" % "".join([precond] + where) 
     
    17781780        return self.doModify(bcode.ident, fields)          
    17791781 
     1782    def refundJob(self, jobident) :    
     1783        """Marks a job as refunded in the history.""" 
     1784        dn = "cn=%s,%s" % (ident, self.info["jobbase"]) 
     1785        fields = { 
     1786                     "pykotaAction" : "REFUND", 
     1787                 }     
     1788        self.doModify(dn, fields)          
     1789         
    17801790    def storageUserFromRecord(self, username, record) : 
    17811791        """Returns a StorageUser instance from a database record.""" 
  • pykota/trunk/pykota/storages/sql.py

    r2949 r3056  
    688688        self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote(bcode.ident))) 
    689689        
     690    def refundJob(self, jobident) :    
     691        """Marks a job as refunded in the history.""" 
     692        self.doModify("UPDATE jobhistory SET action='REFUND' WHERE id=%s;" % self.doQuote(jobident)) 
     693         
    690694    def decreaseUserAccountBalance(self, user, amount) :     
    691695        """Decreases user's account balance from an amount.""" 
     
    763767        self.doModify("DELETE FROM printergroupsmembers WHERE groupid=%s AND printerid=%s" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) 
    764768         
    765     def retrieveHistory(self, user=None, printer=None, hostname=None, billingcode=None, limit=100, start=None, end=None) : 
     769    def retrieveHistory(self, user=None, printer=None, hostname=None, billingcode=None, jobid=None, limit=100, start=None, end=None) : 
    766770        """Retrieves all print jobs for user on printer (or all) between start and end date, limited to first 100 results.""" 
    767771        query = "SELECT jobhistory.*,username,printername FROM jobhistory,users,printers WHERE users.id=userid AND printers.id=printerid" 
     
    775779        if billingcode is not None :     
    776780            where.append("billingcode=%s" % self.doQuote(self.userCharsetToDatabase(billingcode))) 
     781        if jobid is not None :     
     782            where.append("jobid=%s" % self.doQuote(jobid)) # TODO : jobid is text, so self.userCharsetToDatabase(jobid) but do all of them as well. 
    777783        if start is not None :     
    778784            where.append("jobdate>=%s" % self.doQuote(start))