Show
Ignore:
Timestamp:
03/02/06 12:37:52 (18 years ago)
Author:
jerome
Message:

pkusers is now optimized like pkprinters, pkbcodes and edpykota.

Files:
1 modified

Legend:

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

    r2771 r2773  
    562562         
    563563    def addUser(self, user) :         
    564         """Adds a user to the quota storage, returns it.""" 
    565         self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \ 
     564        """Adds a user to the quota storage, returns the old value if it already exists.""" 
     565        try : 
     566            self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \ 
    566567                                         (self.doQuote(self.userCharsetToDatabase(user.Name)), \ 
    567568                                          self.doQuote(user.LimitBy or 'quota'), \ 
     
    571572                                          self.doQuote(user.OverCharge), \ 
    572573                                          self.doQuote(self.userCharsetToDatabase(user.Description)))) 
    573         return self.getUser(user.Name) 
     574        except PyKotaStorageError :     
     575            # TODO : check if this is an error different from a duplicate insert 
     576            # return the existing entry which has to be modified 
     577            return self.getUser(user.Name) 
     578        else :     
     579            if user.PaymentsBacklog : 
     580                for (value, comment) in user.PaymentsBacklog : 
     581                    self.writeNewPayment(user, value, comment) 
     582                user.PaymentsBacklog = [] 
     583            user.isDirty = False 
     584            return None # the entry created doesn't need further modification 
    574585         
    575586    def addGroup(self, group) :         
    576         """Adds a group to the quota storage, returns it.""" 
    577         self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \ 
    578                                           (self.doQuote(self.userCharsetToDatabase(group.Name)), \ 
    579                                            self.doQuote(group.LimitBy or "quota"), \ 
    580                                            self.doQuote(self.userCharsetToDatabase(group.Description)))) 
    581         return self.getGroup(group.Name) 
     587        """Adds a group to the quota storage, returns the old value if it already exists.""" 
     588        try : 
     589            self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \ 
     590                                  (self.doQuote(self.userCharsetToDatabase(group.Name)), \ 
     591                                   self.doQuote(group.LimitBy or "quota"), \ 
     592                                   self.doQuote(self.userCharsetToDatabase(group.Description)))) 
     593        except PyKotaStorageError :     
     594            # TODO : check if this is an error different from a duplicate insert 
     595            # return the existing entry which has to be modified 
     596            return self.getGroup(group.Name) 
     597        else :     
     598            group.isDirty = False 
     599            return None # the entry created doesn't need further modification 
    582600 
    583601    def addUserToGroup(self, user, group) :     
     
    693711    def writeNewPayment(self, user, amount, comment="") : 
    694712        """Adds a new payment to the payments history.""" 
    695         self.doModify("INSERT INTO payments (userid, amount, description) VALUES (%s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(amount), self.doQuote(self.userCharsetToDatabase(comment)))) 
     713        if user.ident is not None : 
     714            self.doModify("INSERT INTO payments (userid, amount, description) VALUES (%s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(amount), self.doQuote(self.userCharsetToDatabase(comment)))) 
     715        else :     
     716            self.doModify("INSERT INTO payments (userid, amount, description) VALUES ((SELECT id FROM users WHERE username=%s), %s, %s)" % (self.doQuote(self.userCharsetToDatabase(user.Name)), self.doQuote(amount), self.doQuote(self.userCharsetToDatabase(comment)))) 
    696717         
    697718    def writeLastJobSize(self, lastjob, jobsize, jobprice) :