Changeset 3531

Show
Ignore:
Timestamp:
04/17/10 23:08:15 (14 years ago)
Author:
jerome
Message:

Fix which fixes 56 for good. References 60.

Location:
pykota/trunk/pykota
Files:
3 modified

Legend:

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

    r3528 r3531  
    3838        """Opens the SQLite database connection.""" 
    3939        BaseStorage.__init__(self, pykotatool) 
    40  
     40        self.doModify = self.doQuery 
    4141        self.tool.logdebug("Trying to open database (dbname=%s)..." % repr(dbname)) 
    4242        self.database = sqlite.connect(dbname, isolation_level=None) 
    4343        self.cursor = self.database.cursor() 
    4444        self.closed = False 
     45        try : 
     46            self.doQuery("PRAGMA foreign_keys = True;") 
     47        except PyKotaStorageError : 
     48            pass 
    4549        self.tool.logdebug("Database opened (dbname=%s)" % repr(dbname)) 
    4650 
     
    6872        self.tool.logdebug("Transaction aborted.") 
    6973 
    70     def doRawSearch(self, query) : 
    71         """Does a raw search query.""" 
     74    def doQuery(self, query) : 
     75        """Executes an SQL query.""" 
    7276        query = query.strip() 
    7377        if not query.endswith(';') : 
     
    7781            self.cursor.execute(query) 
    7882        except self.database.Error, msg : 
     83            self.tool.logdebug("Query failed : %s" % repr(msg)) 
    7984            raise PyKotaStorageError, repr(msg) 
    80         else : 
    81             result = self.cursor.fetchall() 
    82             return result 
     85 
     86    def doRawSearch(self, query) : 
     87        """Executes a raw search query.""" 
     88        self.doQuery(query) 
     89        result = self.cursor.fetchall() 
     90        return result 
    8391 
    8492    def doSearch(self, query) : 
     
    101109                rows.append(rowdict) 
    102110            return rows 
    103  
    104     def doModify(self, query) : 
    105         """Does a (possibly multiple) modify query.""" 
    106         query = query.strip() 
    107         if not query.endswith(';') : 
    108             query += ';' 
    109         self.querydebug("QUERY : %s" % query) 
    110         try : 
    111             self.cursor.execute(query) 
    112         except self.database.Error, msg : 
    113             self.tool.logdebug("Query failed : %s" % repr(msg)) 
    114             raise PyKotaStorageError, repr(msg) 
    115111 
    116112    def doQuote(self, field) : 
  • pykota/trunk/pykota/storages/sql.py

    r3524 r3531  
    3636        user = StorageUser(self, username) 
    3737        user.ident = record.get("uid", record.get("userid", record.get("id"))) 
    38         user.LimitBy = record.get("limitby") or "quota" 
     38        user.LimitBy = databaseToUnicode(record.get("limitby") or "quota") 
    3939        user.AccountBalance = record.get("balance") 
    4040        user.LifeTimePaid = record.get("lifetimepaid") 
    41         user.Email = record.get("email") 
     41        user.Email = databaseToUnicode(record.get("email")) 
    4242        user.Description = databaseToUnicode(record.get("description")) 
    4343        user.OverCharge = record.get("overcharge", 1.0) 
     
    4949        group = StorageGroup(self, groupname) 
    5050        group.ident = record.get("id") 
    51         group.LimitBy = record.get("limitby") or "quota" 
     51        group.LimitBy = databaseToUnicode(record.get("limitby") or "quota") 
    5252        group.AccountBalance = record.get("balance") 
    5353        group.LifeTimePaid = record.get("lifetimepaid") 
     
    8585        job.JobOptions = databaseToUnicode(record.get("options") or "") 
    8686        job.JobDate = record.get("jobdate") 
    87         job.JobHostName = record.get("hostname") 
     87        job.JobHostName = databaseToUnicode(record.get("hostname")) 
    8888        job.JobSizeBytes = record.get("jobsizebytes") 
    8989        job.JobMD5Sum = record.get("md5sum") 
     
    587587        self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \ 
    588588                                     (self.doQuote(unicodeToDatabase(user.Name)), \ 
    589                                       self.doQuote(user.LimitBy or 'quota'), \ 
     589                                      self.doQuote(unicodeToDatabase(user.LimitBy or 'quota')), \ 
    590590                                      self.doQuote(user.AccountBalance or 0.0), \ 
    591591                                      self.doQuote(user.LifeTimePaid or 0.0), \ 
    592                                       self.doQuote(user.Email), \ 
     592                                      self.doQuote(unicodeToDatabase(user.Email)), \ 
    593593                                      self.doQuote(user.OverCharge), \ 
    594594                                      self.doQuote(unicodeToDatabase(user.Description)))) 
     
    607607        self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \ 
    608608                              (self.doQuote(unicodeToDatabase(group.Name)), \ 
    609                                self.doQuote(group.LimitBy or "quota"), \ 
     609                               self.doQuote(unicodeToDatabase(group.LimitBy or "quota")), \ 
    610610                               self.doQuote(unicodeToDatabase(group.Description)))) 
    611611        group.isDirty = False 
     
    673673        """Saves the user to the database in a single operation.""" 
    674674        self.doModify("UPDATE users SET limitby=%s, balance=%s, lifetimepaid=%s, email=%s, overcharge=%s, description=%s WHERE id=%s" \ 
    675                                % (self.doQuote(user.LimitBy or 'quota'), \ 
     675                               % (self.doQuote(unicodeToDatabase(user.LimitBy or 'quota')), \ 
    676676                                  self.doQuote(user.AccountBalance or 0.0), \ 
    677677                                  self.doQuote(user.LifeTimePaid or 0.0), \ 
    678                                   self.doQuote(user.Email), \ 
     678                                  self.doQuote(unicodeToDatabase(user.Email)), \ 
    679679                                  self.doQuote(user.OverCharge), \ 
    680680                                  self.doQuote(unicodeToDatabase(user.Description)), \ 
     
    684684        """Saves the group to the database in a single operation.""" 
    685685        self.doModify("UPDATE groups SET limitby=%s, description=%s WHERE id=%s" \ 
    686                                % (self.doQuote(group.LimitBy or 'quota'), \ 
     686                               % (self.doQuote(unicodeToDatabase(group.LimitBy or 'quota')), \ 
    687687                                  self.doQuote(unicodeToDatabase(group.Description)), \ 
    688688                                  self.doQuote(group.ident))) 
     
    736736            # For legal reasons, we want to hide the title, filename and options 
    737737            title = filename = options = "hidden" 
    738         filename = unicodeToDatabase(filename) 
    739         title = unicodeToDatabase(title) 
    740         options = unicodeToDatabase(options) 
    741         jobbilling = unicodeToDatabase(jobbilling) 
    742738        if (not self.disablehistory) or (not printer.LastJob.Exists) : 
    743739            if jobsize is not None : 
    744                 self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, hostname, jobsizebytes, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(jobmd5sum), self.doQuote(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice))) 
     740                self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, hostname, jobsizebytes, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" \ 
     741                                  % (self.doQuote(user.ident), 
     742                                     self.doQuote(printer.ident), 
     743                                     self.doQuote(jobid), 
     744                                     self.doQuote(pagecounter), 
     745                                     self.doQuote(action), 
     746                                     self.doQuote(jobsize), 
     747                                     self.doQuote(jobprice), 
     748                                     self.doQuote(unicodeToDatabase(filename)), 
     749                                     self.doQuote(unicodeToDatabase(title)), 
     750                                     self.doQuote(copies), 
     751                                     self.doQuote(unicodeToDatabase(options)), 
     752                                     self.doQuote(unicodeToDatabase(clienthost)), 
     753                                     self.doQuote(jobsizebytes), 
     754                                     self.doQuote(jobmd5sum), 
     755                                     self.doQuote(jobpages), 
     756                                     self.doQuote(unicodeToDatabase(jobbilling)), 
     757                                     self.doQuote(precomputedsize), 
     758                                     self.doQuote(precomputedprice))) 
    745759            else : 
    746                 self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options, hostname, jobsizebytes, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(jobmd5sum), self.doQuote(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice))) 
     760                self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options, hostname, jobsizebytes, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" \ 
     761                                  % (self.doQuote(user.ident), 
     762                                     self.doQuote(printer.ident), 
     763                                     self.doQuote(jobid), 
     764                                     self.doQuote(pagecounter), 
     765                                     self.doQuote(action), 
     766                                     self.doQuote(unicodeToDatabase(filename)), 
     767                                     self.doQuote(unicodeToDatabase(title)), 
     768                                     self.doQuote(copies), 
     769                                     self.doQuote(unicodeToDatabase(options)), 
     770                                     self.doQuote(unicodeToDatabase(clienthost)), 
     771                                     self.doQuote(jobsizebytes), 
     772                                     self.doQuote(jobmd5sum), 
     773                                     self.doQuote(jobpages), 
     774                                     self.doQuote(unicodeToDatabase(jobbilling)), 
     775                                     self.doQuote(precomputedsize), 
     776                                     self.doQuote(precomputedprice))) 
    747777        else : 
    748778            # here we explicitly want to reset jobsize to NULL if needed 
    749             self.doModify("UPDATE jobhistory SET userid=%s, jobid=%s, pagecounter=%s, action=%s, jobsize=%s, jobprice=%s, filename=%s, title=%s, copies=%s, options=%s, hostname=%s, jobsizebytes=%s, md5sum=%s, pages=%s, billingcode=%s, precomputedjobsize=%s, precomputedjobprice=%s, jobdate=now() WHERE id=%s" % (self.doQuote(user.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(jobmd5sum), self.doQuote(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice), self.doQuote(printer.LastJob.ident))) 
     779            self.doModify("UPDATE jobhistory SET userid=%s, jobid=%s, pagecounter=%s, action=%s, jobsize=%s, jobprice=%s, filename=%s, title=%s, copies=%s, options=%s, hostname=%s, jobsizebytes=%s, md5sum=%s, pages=%s, billingcode=%s, precomputedjobsize=%s, precomputedjobprice=%s, jobdate=now() WHERE id=%s" \ 
     780                              % (self.doQuote(user.ident), 
     781                                 self.doQuote(jobid), 
     782                                 self.doQuote(pagecounter), 
     783                                 self.doQuote(action), 
     784                                 self.doQuote(jobsize), 
     785                                 self.doQuote(jobprice), 
     786                                 self.doQuote(unicodeToDatabase(filename)), 
     787                                 self.doQuote(unicodeToDatabase(title)), 
     788                                 self.doQuote(copies), 
     789                                 self.doQuote(unicodeToDatabase(options)), 
     790                                 self.doQuote(unicodeToDatabase(clienthost)), 
     791                                 self.doQuote(jobsizebytes), 
     792                                 self.doQuote(jobmd5sum), 
     793                                 self.doQuote(jobpages), 
     794                                 self.doQuote(unicodeToDatabase(jobbilling)), 
     795                                 self.doQuote(precomputedsize), 
     796                                 self.doQuote(precomputedprice), 
     797                                 self.doQuote(printer.LastJob.ident))) 
    750798 
    751799    def saveUserPQuota(self, userpquota) : 
    752800        """Saves an user print quota entry.""" 
    753801        self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, warncount=%s, datelimit=%s, pagecounter=%s, lifepagecounter=%s, maxjobsize=%s WHERE id=%s" \ 
    754                               % (self.doQuote(userpquota.SoftLimit), \ 
    755                                  self.doQuote(userpquota.HardLimit), \ 
    756                                  self.doQuote(userpquota.WarnCount or 0), \ 
    757                                  self.doQuote(userpquota.DateLimit), \ 
    758                                  self.doQuote(userpquota.PageCounter or 0), \ 
    759                                  self.doQuote(userpquota.LifePageCounter or 0), \ 
    760                                  self.doQuote(userpquota.MaxJobSize), \ 
     802                              % (self.doQuote(userpquota.SoftLimit), 
     803                                 self.doQuote(userpquota.HardLimit), 
     804                                 self.doQuote(userpquota.WarnCount or 0), 
     805                                 self.doQuote(userpquota.DateLimit), 
     806                                 self.doQuote(userpquota.PageCounter or 0), 
     807                                 self.doQuote(userpquota.LifePageCounter or 0), 
     808                                 self.doQuote(userpquota.MaxJobSize), 
    761809                                 self.doQuote(userpquota.ident))) 
    762810 
    763811    def writeUserPQuotaWarnCount(self, userpquota, warncount) : 
    764812        """Sets the warn counter value for a user quota.""" 
    765         self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" % (self.doQuote(warncount), self.doQuote(userpquota.ident))) 
     813        self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" \ 
     814                          % (self.doQuote(warncount), 
     815                             self.doQuote(userpquota.ident))) 
    766816 
    767817    def increaseUserPQuotaWarnCount(self, userpquota) : 
    768818        """Increases the warn counter value for a user quota.""" 
    769         self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" % self.doQuote(userpquota.ident)) 
     819        self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" \ 
     820                          % self.doQuote(userpquota.ident)) 
    770821 
    771822    def saveGroupPQuota(self, grouppquota) : 
    772823        """Saves a group print quota entry.""" 
    773824        self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=%s WHERE id=%s" \ 
    774                               % (self.doQuote(grouppquota.SoftLimit), \ 
    775                                  self.doQuote(grouppquota.HardLimit), \ 
    776                                  self.doQuote(grouppquota.DateLimit), \ 
     825                              % (self.doQuote(grouppquota.SoftLimit), 
     826                                 self.doQuote(grouppquota.HardLimit), 
     827                                 self.doQuote(grouppquota.DateLimit), 
    777828                                 self.doQuote(grouppquota.ident))) 
    778829 
     
    800851            where.append("printerid=%s" % self.doQuote(printer.ident)) 
    801852        if hostname is not None : 
    802             where.append("hostname=%s" % self.doQuote(hostname)) 
     853            where.append("hostname=%s" % self.doQuote(unicodeToDatabase(hostname))) 
    803854        if billingcode is not None : 
    804855            where.append("billingcode=%s" % self.doQuote(unicodeToDatabase(billingcode))) 
  • pykota/trunk/pykota/version.py

    r3508 r3531  
    2424import time 
    2525 
    26 __version__ = "1.27alpha8_unofficial" 
     26__version__ = "1.27alpha9_unofficial" 
    2727 
    2828__doc__ = "PyKota : a complete Printing Quota Solution for CUPS."