Show
Ignore:
Timestamp:
07/06/05 20:11:00 (19 years ago)
Author:
jerome
Message:

The pkbcodes command line tool now works fine with the PostgreSQL
backend. The dumpykota command can now dump billing codes too.
Still no code for LDAP though.
NB : a database upgrade is necessary AGAIN !
Severity : no need to play with this until there's LDAP support.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/storages/sql.py

    r2320 r2342  
    2424 
    2525from types import StringType 
    26 from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota 
     26from pykota.storage import PyKotaStorageError, BaseStorage, StorageObject, \ 
     27                           StorageUser, StorageGroup, StoragePrinter, \ 
     28                           StorageJob, StorageLastJob, StorageUserPQuota, \ 
     29                           StorageGroupPQuota, StorageBillingCode 
    2730 
    2831class SQLStorage : 
     
    6770        return self.prepareRawResult(result) 
    6871         
     72    def extractBillingcodes(self, extractonly={}) : 
     73        """Extracts all billing codes records.""" 
     74        thefilter = self.createFilter(extractonly) 
     75        if thefilter : 
     76            thefilter = "WHERE %s" % thefilter 
     77        result = self.doRawSearch("SELECT * FROM billingcodes %s ORDER BY id ASC" % thefilter) 
     78        return self.prepareRawResult(result) 
     79         
    6980    def extractGroups(self, extractonly={}) : 
    7081        """Extracts all group records.""" 
     
    206217            printer.Exists = 1 
    207218        return printer     
     219         
     220    def getBillingCodeFromBackend(self, label) :         
     221        """Extracts a billing code information given its name.""" 
     222        code = StorageBillingCode(self, label) 
     223        result = self.doSearch("SELECT * FROM billingcodes WHERE billingcode=%s LIMIT 1" % self.doQuote(label)) 
     224        if result : 
     225            fields = result[0] 
     226            code.ident = fields.get("id") 
     227            code.BillingCode = fields.get("billingcode", label) 
     228            code.Description = self.databaseToUserCharset(fields.get("description") or "") 
     229            code.Balance = fields.get("balance") or 0.0 
     230            code.PageCounter = fields.get("pagecounter") or 0 
     231            code.Exists = 1 
     232        return code     
    208233         
    209234    def getUserPQuotaFromBackend(self, user, printer) :         
     
    329354                    self.cacheEntry("PRINTERS", printer.Name, printer) 
    330355        return printers         
     356         
     357    def getMatchingBillingCodes(self, billingcodepattern) : 
     358        """Returns the list of all billing codes for which the label matches a certain pattern.""" 
     359        codes = [] 
     360        result = self.doSearch("SELECT * FROM billingcodes") 
     361        if result : 
     362            for record in result : 
     363                if self.tool.matchString(record["billingcode"], billingcodepattern.split(",")) : 
     364                    code = StorageBillingCode(self, record["billingcode"]) 
     365                    code.ident = record.get("id") 
     366                    code.Balance = record.get("balance") or 0.0 
     367                    code.PageCounter = record.get("pagecounter") or 0 
     368                    code.Description = self.databaseToUserCharset(record.get("description") or "")  
     369                    code.Exists = 1 
     370                    codes.append(code) 
     371                    self.cacheEntry("BILLINGCODES", code.BillingCode, code) 
     372        return codes         
    331373         
    332374    def getPrinterUsersAndQuotas(self, printer, names=["*"]) :         
     
    376418        return self.getPrinter(printername) 
    377419         
     420    def addBillingCode(self, label) :         
     421        """Adds a billing code to the quota storage, returns it.""" 
     422        self.doModify("INSERT INTO billingcodes (billingcode) VALUES (%s)" % self.doQuote(label)) 
     423        return self.getBillingCode(label) 
     424         
    378425    def addUser(self, user) :         
    379         """Adds a user to the quota storage, returns its id.""" 
     426        """Adds a user to the quota storage, returns it.""" 
    380427        self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge) VALUES (%s, %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), self.doQuote(user.OverCharge))) 
    381428        return self.getUser(user.Name) 
    382429         
    383430    def addGroup(self, group) :         
    384         """Adds a group to the quota storage, returns its id.""" 
     431        """Adds a group to the quota storage, returns it.""" 
    385432        self.doModify("INSERT INTO groups (groupname, limitby) VALUES (%s, %s)" % (self.doQuote(group.Name), self.doQuote(group.LimitBy or "quota"))) 
    386433        return self.getGroup(group.Name) 
     
    442489        """Sets the new page counters permanently for a user print quota.""" 
    443490        self.doModify("UPDATE userpquota SET pagecounter=%s, lifepagecounter=%s, warncount=0, datelimit=NULL WHERE id=%s" % (self.doQuote(newpagecounter), self.doQuote(newlifepagecounter), self.doQuote(userpquota.ident))) 
     491        
     492    def writeBillingCodeDescription(self, code) : 
     493        """Sets the new description for a billing code.""" 
     494        self.doModify("UPDATE billingcodes SET description=%s WHERE id=%s" % (self.doQuote(self.userCharsetToDatabase(code.Description or "")), self.doQuote(code.ident))) 
     495        
     496    def setBillingCodeValues(self, code, newbalance, newpagecounter) :     
     497        """Sets the new page counter and balance for a billing code.""" 
     498        self.doModify("UPDATE billingcodes SET balance=%s, pagecounter=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newpagecounter), self.doQuote(code.ident))) 
     499        
     500    def consumeBillingCode(self, code, balance, pagecounter) : 
     501        """Consumes from a billing code.""" 
     502        self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote(code.ident))) 
    444503        
    445504    def decreaseUserAccountBalance(self, user, amount) :     
     
    593652                  ] : 
    594653            self.doModify(q) 
    595          
     654             
     655    def deleteBillingCode(self, code) :     
     656        """Completely deletes a billing code from the Quota Storage.""" 
     657        for q in [ 
     658                   "DELETE FROM billingcodes WHERE id=%s" % self.doQuote(code.ident), 
     659                 ] :   
     660            self.doModify(q) 
     661             
     662