Changeset 2707

Show
Ignore:
Timestamp:
02/18/06 11:56:29 (18 years ago)
Author:
jerome
Message:

Improved user modification speed by around 15%.

Location:
pykota/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkusers

    r2706 r2707  
    308308                for i in range(nbtotal) :         
    309309                    entry = entries[i] 
    310                     if description is not None : # NB : "" is allowed ! 
    311                         entry.setDescription(description) 
    312                     if limitby :     
    313                         entry.setLimitBy(limitby) 
     310                     
     311                    # We need a transaction because we may have to create a record 
     312                    # in the payments' table at the same time we modify the user's balance. 
     313                    self.storage.beginTransaction() 
     314                    try : 
     315                        if description is not None : # NB : "" is allowed ! 
     316                            entry.setDescription(description) 
     317                        if limitby :     
     318                            entry.setLimitBy(limitby) 
     319                             
     320                        if suffix == "User" :     
     321                            if overcharge is not None : # NB : 0 is allowed !      
     322                                entry.setOverChargeFactor(overcharge) 
    314323                         
    315                     if suffix == "User" :     
    316                         if overcharge is not None : # NB : 0 is allowed !      
    317                             entry.setOverChargeFactor(overcharge) 
    318                              
    319                     entry.save()     
    320                      
    321                     if suffix == "User" : 
    322                         if balance : 
    323                             if balance.startswith("+") or balance.startswith("-") : 
    324                                 newbalance = float(entry.AccountBalance or 0.0) + balancevalue 
    325                                 newlifetimepaid = float(entry.LifeTimePaid or 0.0) + balancevalue 
    326                                 entry.setAccountBalance(newbalance, newlifetimepaid, comment) 
    327                             else : 
    328                                 diff = balancevalue - float(entry.AccountBalance or 0.0) 
    329                                 newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff 
    330                                 entry.setAccountBalance(balancevalue, newlifetimepaid, comment) 
    331                                          
    332                         for ugroup in usersgroups : 
    333                             if options["remove"] : 
    334                                 ugroup.delUserFromGroup(entry) 
    335                             else : 
    336                                 ugroup.addUserToGroup(entry) 
    337                                  
     324                        if suffix == "User" : 
     325                            if balance : 
     326                                if balance.startswith("+") or balance.startswith("-") : 
     327                                    newbalance = float(entry.AccountBalance or 0.0) + balancevalue 
     328                                    newlifetimepaid = float(entry.LifeTimePaid or 0.0) + balancevalue 
     329                                    entry.setAccountBalance(newbalance, newlifetimepaid, comment) 
     330                                else : 
     331                                    diff = balancevalue - float(entry.AccountBalance or 0.0) 
     332                                    newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff 
     333                                    entry.setAccountBalance(balancevalue, newlifetimepaid, comment) 
     334                                             
     335                            for ugroup in usersgroups : 
     336                                if options["remove"] : 
     337                                    ugroup.delUserFromGroup(entry) 
     338                                else : 
     339                                    ugroup.addUserToGroup(entry) 
     340                        entry.save()     
     341                    except : 
     342                        self.storage.rollbackTransaction() 
     343                        raise 
     344                    else :     
     345                        self.storage.commitTransaction() 
     346 
    338347                    percent = 100.0 * float(i) / float(nbtotal) 
    339348                    self.display("\r%.02f%%" % percent) 
  • pykota/trunk/pykota/storage.py

    r2706 r2707  
    7676        """Sets the user's account balance in case he pays more money.""" 
    7777        diff = float(lifetimepaid or 0.0) - float(self.LifeTimePaid or 0.0) 
    78         self.parent.beginTransaction() 
    79         try : 
    80             self.parent.writeUserAccountBalance(self, balance, lifetimepaid) 
    81             self.parent.writeNewPayment(self, diff, comment) 
    82         except PyKotaStorageError, msg :     
    83             self.parent.rollbackTransaction() 
    84             raise PyKotaStorageError, msg 
    85         else :     
    86             self.parent.commitTransaction() 
    87             self.AccountBalance = balance 
    88             self.LifeTimePaid = lifetimepaid 
     78        self.AccountBalance = balance 
     79        self.LifeTimePaid = lifetimepaid 
     80        self.parent.writeNewPayment(self, diff, comment) 
     81        self.isDirty = True 
    8982         
    9083    def setLimitBy(self, limitby) :     
  • pykota/trunk/pykota/storages/ldapstorage.py

    r2706 r2707  
    10481048        self.doModify(user.ident, newfields) 
    10491049         
     1050        newfields = { "pykotaBalance" : str(user.AccountBalance or 0.0), 
     1051                      "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0),  
     1052                    } 
     1053        self.doModify(user.idbalance, newfields) 
     1054         
    10501055    def saveGroup(self, group) : 
    10511056        """Saves the group to the database in a single operation.""" 
     
    10961101        return self.doModify(user.idbalance, fields, flushcache=1)          
    10971102        
    1098     def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) :     
    1099         """Sets the new account balance and eventually new lifetime paid.""" 
    1100         fields = { 
    1101                    "pykotaBalance" : str(newbalance), 
    1102                  } 
    1103         if newlifetimepaid is not None : 
    1104             fields.update({ "pykotaLifeTimePaid" : str(newlifetimepaid) }) 
    1105         return self.doModify(user.idbalance, fields)          
    1106              
    11071103    def writeNewPayment(self, user, amount, comment="") : 
    11081104        """Adds a new payment to the payments history.""" 
  • pykota/trunk/pykota/storages/sql.py

    r2706 r2707  
    572572    def saveUser(self, user) :         
    573573        """Saves the user to the database in a single operation.""" 
    574         self.doModify("UPDATE users SET limitby=%s, email=%s, overcharge=%s, description=%s WHERE id=%s" \ 
     574        self.doModify("UPDATE users SET limitby=%s, balance=%s, lifetimepaid=%s, email=%s, overcharge=%s, description=%s WHERE id=%s" \ 
    575575                               % (self.doQuote(user.LimitBy or 'quota'), \ 
     576                                  self.doQuote(user.AccountBalance or 0.0), \ 
     577                                  self.doQuote(user.LifeTimePaid or 0.0), \ 
    576578                                  self.doQuote(user.Email), \ 
    577579                                  self.doQuote(user.OverCharge), \ 
     
    618620        self.doModify("UPDATE users SET balance=balance - %s WHERE id=%s" % (self.doQuote(amount), self.doQuote(user.ident))) 
    619621        
    620     def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) :     
    621         """Sets the new account balance and eventually new lifetime paid.""" 
    622         if newlifetimepaid is not None : 
    623             self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident))) 
    624         else :     
    625             self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident))) 
    626              
    627622    def writeNewPayment(self, user, amount, comment="") : 
    628623        """Adds a new payment to the payments history."""