Show
Ignore:
Timestamp:
09/27/08 22:02:37 (16 years ago)
Author:
jerome
Message:

Removed unnecessary spaces at EOL.

Files:
1 modified

Legend:

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

    r3411 r3413  
    88# the Free Software Foundation, either version 3 of the License, or 
    99# (at your option) any later version. 
    10 #  
     10# 
    1111# This program is distributed in the hope that it will be useful, 
    1212# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414# GNU General Public License for more details. 
    15 #  
     15# 
    1616# You should have received a copy of the GNU General Public License 
    1717# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     
    2626                           StorageJob, StorageLastJob, StorageUserPQuota, \ 
    2727                           StorageGroupPQuota, StorageBillingCode 
    28                             
    29 from pykota.utils import *                            
     28 
     29from pykota.utils import * 
    3030 
    3131class SQLStorage : 
     
    4242        user.Exists = True 
    4343        return user 
    44          
     44 
    4545    def storageGroupFromRecord(self, groupname, record) : 
    4646        """Returns a StorageGroup instance from a database record.""" 
     
    5353        group.Exists = True 
    5454        return group 
    55          
     55 
    5656    def storagePrinterFromRecord(self, printername, record) : 
    5757        """Returns a StoragePrinter instance from a database record.""" 
     
    6969        printer.Exists = True 
    7070        return printer 
    71          
    72     def setJobAttributesFromRecord(self, job, record) :     
     71 
     72    def setJobAttributesFromRecord(self, job, record) : 
    7373        """Sets the attributes of a job from a database record.""" 
    7474        job.ident = record.get("id") 
     
    7878        job.JobPrice = record.get("jobprice") 
    7979        job.JobAction = record.get("action") 
    80         job.JobFileName = databaseToUnicode(record.get("filename") or "")  
    81         job.JobTitle = databaseToUnicode(record.get("title") or "")  
     80        job.JobFileName = databaseToUnicode(record.get("filename") or "") 
     81        job.JobTitle = databaseToUnicode(record.get("title") or "") 
    8282        job.JobCopies = record.get("copies") 
    83         job.JobOptions = databaseToUnicode(record.get("options") or "")  
     83        job.JobOptions = databaseToUnicode(record.get("options") or "") 
    8484        job.JobDate = record.get("jobdate") 
    8585        job.JobHostName = record.get("hostname") 
     
    9595            (job.JobTitle, job.JobFileName, job.JobOptions) = (_("Hidden because of privacy concerns"),) * 3 
    9696        job.Exists = True 
    97          
     97 
    9898    def storageJobFromRecord(self, record) : 
    9999        """Returns a StorageJob instance from a database record.""" 
     
    101101        self.setJobAttributesFromRecord(job, record) 
    102102        return job 
    103          
     103 
    104104    def storageLastJobFromRecord(self, printer, record) : 
    105105        """Returns a StorageLastJob instance from a database record.""" 
     
    107107        self.setJobAttributesFromRecord(lastjob, record) 
    108108        return lastjob 
    109          
     109 
    110110    def storageUserPQuotaFromRecord(self, user, printer, record) : 
    111111        """Returns a StorageUserPQuota instance from a database record.""" 
     
    120120        userpquota.Exists = True 
    121121        return userpquota 
    122          
     122 
    123123    def storageGroupPQuotaFromRecord(self, group, printer, record) : 
    124124        """Returns a StorageGroupPQuota instance from a database record.""" 
     
    135135        grouppquota.Exists = True 
    136136        return grouppquota 
    137          
     137 
    138138    def storageBillingCodeFromRecord(self, billingcode, record) : 
    139139        """Returns a StorageBillingCode instance from a database record.""" 
     
    145145        code.Exists = True 
    146146        return code 
    147          
    148     def createFilter(self, only) :     
     147 
     148    def createFilter(self, only) : 
    149149        """Returns the appropriate SQL filter.""" 
    150150        if only : 
     
    152152            for (k, v) in only.items() : 
    153153                expressions.append("%s=%s" % (k, self.doQuote(unicodeToDatabase(v)))) 
    154             return " AND ".join(expressions)      
    155         return ""         
    156          
    157     def createOrderBy(self, default, ordering) :     
     154            return " AND ".join(expressions) 
     155        return "" 
     156 
     157    def createOrderBy(self, default, ordering) : 
    158158        """Creates a suitable ORDER BY statement based on a list of fieldnames prefixed with '+' (ASC) or '-' (DESC).""" 
    159159        statements = [] 
    160160        if not ordering : 
    161161            ordering = default 
    162         for field in ordering :     
    163             if field.startswith("-") :     
     162        for field in ordering : 
     163            if field.startswith("-") : 
    164164                statements.append("%s DESC" % field[1:]) 
    165165            elif field.startswith("+") : 
    166166                statements.append("%s ASC" % field[1:]) 
    167             else :     
     167            else : 
    168168                statements.append("%s ASC" % field) 
    169         return ", ".join(statements)     
    170          
     169        return ", ".join(statements) 
     170 
    171171    def extractPrinters(self, extractonly={}, ordering=[]) : 
    172172        """Extracts all printer records.""" 
     
    177177        result = self.doRawSearch("SELECT * FROM printers %(thefilter)s ORDER BY %(orderby)s" % locals()) 
    178178        return self.prepareRawResult(result) 
    179          
     179 
    180180    def extractUsers(self, extractonly={}, ordering=[]) : 
    181181        """Extracts all user records.""" 
     
    186186        result = self.doRawSearch("SELECT * FROM users %(thefilter)s ORDER BY %(orderby)s" % locals()) 
    187187        return self.prepareRawResult(result) 
    188          
     188 
    189189    def extractBillingcodes(self, extractonly={}, ordering=[]) : 
    190190        """Extracts all billing codes records.""" 
     
    195195        result = self.doRawSearch("SELECT * FROM billingcodes %(thefilter)s ORDER BY %(orderby)s" % locals()) 
    196196        return self.prepareRawResult(result) 
    197          
     197 
    198198    def extractGroups(self, extractonly={}, ordering=[]) : 
    199199        """Extracts all group records.""" 
     
    204204        result = self.doRawSearch("SELECT groups.*,COALESCE(SUM(balance), 0) AS balance, COALESCE(SUM(lifetimepaid), 0) as lifetimepaid FROM groups LEFT OUTER JOIN users ON users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) %(thefilter)s GROUP BY groups.id,groups.groupname,groups.limitby,groups.description ORDER BY %(orderby)s" % locals()) 
    205205        return self.prepareRawResult(result) 
    206          
     206 
    207207    def extractPayments(self, extractonly={}, ordering=[]) : 
    208208        """Extracts all payment records.""" 
     
    212212            try : 
    213213                del extractonly[limit] 
    214             except KeyError :     
     214            except KeyError : 
    215215                pass 
    216216        thefilter = self.createFilter(extractonly) 
     
    218218            thefilter = "AND %s" % thefilter 
    219219        (startdate, enddate) = self.cleanDates(startdate, enddate) 
    220         if startdate :  
     220        if startdate : 
    221221            thefilter = "%s AND date>=%s" % (thefilter, self.doQuote(startdate)) 
    222         if enddate :  
     222        if enddate : 
    223223            thefilter = "%s AND date<=%s" % (thefilter, self.doQuote(enddate)) 
    224224        orderby = self.createOrderBy(["+payments.id"], ordering) 
    225225        result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid %(thefilter)s ORDER BY %(orderby)s" % locals()) 
    226226        return self.prepareRawResult(result) 
    227          
     227 
    228228    def extractUpquotas(self, extractonly={}, ordering=[]) : 
    229229        """Extracts all userpquota records.""" 
     
    234234        result = self.doRawSearch("SELECT users.username,printers.printername,userpquota.* FROM users,printers,userpquota WHERE users.id=userpquota.userid AND printers.id=userpquota.printerid %(thefilter)s ORDER BY %(orderby)s" % locals()) 
    235235        return self.prepareRawResult(result) 
    236          
     236 
    237237    def extractGpquotas(self, extractonly={}, ordering=[]) : 
    238238        """Extracts all grouppquota records.""" 
     
    243243        result = self.doRawSearch("SELECT groups.groupname,printers.printername,grouppquota.*,coalesce(sum(pagecounter), 0) AS pagecounter,coalesce(sum(lifepagecounter), 0) AS lifepagecounter FROM groups,printers,grouppquota,userpquota WHERE groups.id=grouppquota.groupid AND printers.id=grouppquota.printerid AND userpquota.printerid=grouppquota.printerid AND userpquota.userid IN (SELECT userid FROM groupsmembers WHERE groupsmembers.groupid=grouppquota.groupid) %(thefilter)s GROUP BY grouppquota.id,grouppquota.groupid,grouppquota.printerid,grouppquota.softlimit,grouppquota.hardlimit,grouppquota.datelimit,grouppquota.maxjobsize,groups.groupname,printers.printername ORDER BY %(orderby)s" % locals()) 
    244244        return self.prepareRawResult(result) 
    245          
     245 
    246246    def extractUmembers(self, extractonly={}, ordering=[]) : 
    247247        """Extracts all user groups members.""" 
     
    252252        result = self.doRawSearch("SELECT groups.groupname, users.username, groupsmembers.* FROM groups,users,groupsmembers WHERE users.id=groupsmembers.userid AND groups.id=groupsmembers.groupid %(thefilter)s ORDER BY %(orderby)s" % locals()) 
    253253        return self.prepareRawResult(result) 
    254          
     254 
    255255    def extractPmembers(self, extractonly={}, ordering=[]) : 
    256256        """Extracts all printer groups members.""" 
     
    268268        result = self.doRawSearch("SELECT p1.printername as pgroupname, p2.printername as printername, printergroupsmembers.* FROM printers p1, printers p2, printergroupsmembers WHERE p1.id=printergroupsmembers.groupid AND p2.id=printergroupsmembers.printerid %(thefilter)s ORDER BY %(orderby)s" % locals()) 
    269269        return self.prepareRawResult(result) 
    270          
     270 
    271271    def extractHistory(self, extractonly={}, ordering=[]) : 
    272272        """Extracts all jobhistory records.""" 
     
    276276            try : 
    277277                del extractonly[limit] 
    278             except KeyError :     
     278            except KeyError : 
    279279                pass 
    280280        thefilter = self.createFilter(extractonly) 
     
    282282            thefilter = "AND %s" % thefilter 
    283283        (startdate, enddate) = self.cleanDates(startdate, enddate) 
    284         if startdate :  
     284        if startdate : 
    285285            thefilter = "%s AND jobdate>=%s" % (thefilter, self.doQuote(startdate)) 
    286         if enddate :  
     286        if enddate : 
    287287            thefilter = "%s AND jobdate<=%s" % (thefilter, self.doQuote(enddate)) 
    288288        orderby = self.createOrderBy(["+jobhistory.id"], ordering) 
    289289        result = self.doRawSearch("SELECT users.username,printers.printername,jobhistory.* FROM users,printers,jobhistory WHERE users.id=jobhistory.userid AND printers.id=jobhistory.printerid %(thefilter)s ORDER BY %(orderby)s" % locals()) 
    290290        return self.prepareRawResult(result) 
    291              
     291 
    292292    def filterNames(self, records, attribute, patterns=None) : 
    293293        """Returns a list of 'attribute' from a list of records. 
    294          
     294 
    295295           Logs any missing attribute. 
    296         """    
     296        """ 
    297297        result = [] 
    298298        for record in records : 
     
    307307                    if self.tool.matchString(attrval, patterns) : 
    308308                        result.append(attrval) 
    309                 else :     
     309                else : 
    310310                    result.append(attrval) 
    311         return result    
    312                  
    313     def getAllBillingCodes(self, billingcode=None) :     
     311        return result 
     312 
     313    def getAllBillingCodes(self, billingcode=None) : 
    314314        """Extracts all billing codes or only the billing codes matching the optional parameter.""" 
    315315        result = self.doSearch("SELECT billingcode FROM billingcodes") 
    316316        if result : 
    317317            return self.filterNames(result, "billingcode", billingcode) 
    318         else :     
     318        else : 
    319319            return [] 
    320          
    321     def getAllPrintersNames(self, printername=None) :     
     320 
     321    def getAllPrintersNames(self, printername=None) : 
    322322        """Extracts all printer names or only the printers' names matching the optional parameter.""" 
    323323        result = self.doSearch("SELECT printername FROM printers") 
    324324        if result : 
    325325            return self.filterNames(result, "printername", printername) 
    326         else :     
     326        else : 
    327327            return [] 
    328      
    329     def getAllUsersNames(self, username=None) :     
     328 
     329    def getAllUsersNames(self, username=None) : 
    330330        """Extracts all user names.""" 
    331331        result = self.doSearch("SELECT username FROM users") 
    332332        if result : 
    333333            return self.filterNames(result, "username", username) 
    334         else :     
     334        else : 
    335335            return [] 
    336          
    337     def getAllGroupsNames(self, groupname=None) :     
     336 
     337    def getAllGroupsNames(self, groupname=None) : 
    338338        """Extracts all group names.""" 
    339339        result = self.doSearch("SELECT groupname FROM groups") 
     
    342342        else : 
    343343            return [] 
    344          
     344 
    345345    def getUserNbJobsFromHistory(self, user) : 
    346346        """Returns the number of jobs the user has in history.""" 
     
    349349            return result[0]["count"] 
    350350        return 0 
    351          
    352     def getUserFromBackend(self, username) :     
     351 
     352    def getUserFromBackend(self, username) : 
    353353        """Extracts user information given its name.""" 
    354354        result = self.doSearch("SELECT * FROM users WHERE username=%s"\ 
     
    356356        if result : 
    357357            return self.storageUserFromRecord(username, result[0]) 
    358         else :     
     358        else : 
    359359            return StorageUser(self, username) 
    360         
    361     def getGroupFromBackend(self, groupname) :     
     360 
     361    def getGroupFromBackend(self, groupname) : 
    362362        """Extracts group information given its name.""" 
    363363        result = self.doSearch("SELECT groups.*,COALESCE(SUM(balance), 0.0) AS balance, COALESCE(SUM(lifetimepaid), 0.0) AS lifetimepaid FROM groups LEFT OUTER JOIN users ON users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) WHERE groupname=%s GROUP BY groups.id,groups.groupname,groups.limitby,groups.description" \ 
     
    365365        if result : 
    366366            return self.storageGroupFromRecord(groupname, result[0]) 
    367         else :     
     367        else : 
    368368            return StorageGroup(self, groupname) 
    369         
    370     def getPrinterFromBackend(self, printername) :         
     369 
     370    def getPrinterFromBackend(self, printername) : 
    371371        """Extracts printer information given its name.""" 
    372372        result = self.doSearch("SELECT * FROM printers WHERE printername=%s" \ 
     
    374374        if result : 
    375375            return self.storagePrinterFromRecord(printername, result[0]) 
    376         else :     
     376        else : 
    377377            return StoragePrinter(self, printername) 
    378          
    379     def getBillingCodeFromBackend(self, label) :         
     378 
     379    def getBillingCodeFromBackend(self, label) : 
    380380        """Extracts a billing code information given its name.""" 
    381381        result = self.doSearch("SELECT * FROM billingcodes WHERE billingcode=%s" \ 
     
    383383        if result : 
    384384            return self.storageBillingCodeFromRecord(label, result[0]) 
    385         else :     
     385        else : 
    386386            return StorageBillingCode(self, label) 
    387          
    388     def getUserPQuotaFromBackend(self, user, printer) :         
     387 
     388    def getUserPQuotaFromBackend(self, user, printer) : 
    389389        """Extracts a user print quota.""" 
    390390        if printer.Exists and user.Exists : 
     
    394394                return self.storageUserPQuotaFromRecord(user, printer, result[0]) 
    395395        return StorageUserPQuota(self, user, printer) 
    396          
    397     def getGroupPQuotaFromBackend(self, group, printer) :         
     396 
     397    def getGroupPQuotaFromBackend(self, group, printer) : 
    398398        """Extracts a group print quota.""" 
    399399        if printer.Exists and group.Exists : 
     
    403403                return self.storageGroupPQuotaFromRecord(group, printer, result[0]) 
    404404        return StorageGroupPQuota(self, group, printer) 
    405          
    406     def getPrinterLastJobFromBackend(self, printer) :         
     405 
     406    def getPrinterLastJobFromBackend(self, printer) : 
    407407        """Extracts a printer's last job information.""" 
    408408        result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobprice, filename, title, copies, options, hostname, jobdate, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice FROM jobhistory, users WHERE userid=users.id AND jobhistory.id IN (SELECT max(id) FROM jobhistory WHERE printerid=%s)" % self.doQuote(printer.ident)) 
    409409        if result : 
    410410            return self.storageLastJobFromRecord(printer, result[0]) 
    411         else :     
     411        else : 
    412412            return StorageLastJob(self, printer) 
    413              
    414     def getGroupMembersFromBackend(self, group) :         
     413 
     414    def getGroupMembersFromBackend(self, group) : 
    415415        """Returns the group's members list.""" 
    416416        groupmembers = [] 
     
    422422                groupmembers.append(user) 
    423423                self.cacheEntry("USERS", user.Name, user) 
    424         return groupmembers         
    425          
    426     def getUserGroupsFromBackend(self, user) :         
     424        return groupmembers 
     425 
     426    def getUserGroupsFromBackend(self, user) : 
    427427        """Returns the user's groups list.""" 
    428428        groups = [] 
     
    431431            for record in result : 
    432432                groups.append(self.getGroup(databaseToUnicode(record.get("groupname")))) 
    433         return groups         
    434          
    435     def getParentPrintersFromBackend(self, printer) :     
     433        return groups 
     434 
     435    def getParentPrintersFromBackend(self, printer) : 
    436436        """Get all the printer groups this printer is a member of.""" 
    437437        pgroups = [] 
     
    444444                        pgroups.append(parentprinter) 
    445445        return pgroups 
    446          
     446 
    447447    def getMatchingPrinters(self, printerpattern) : 
    448448        """Returns the list of all printers for which name matches a certain pattern.""" 
     
    456456            try : 
    457457                patdict = {}.fromkeys(patterns) 
    458             except AttributeError :     
     458            except AttributeError : 
    459459                # Python v2.2 or earlier 
    460460                patdict = {} 
     
    467467                    printers.append(printer) 
    468468                    self.cacheEntry("PRINTERS", printer.Name, printer) 
    469         return printers         
    470          
     469        return printers 
     470 
    471471    def getMatchingUsers(self, userpattern) : 
    472472        """Returns the list of all users for which name matches a certain pattern.""" 
     
    480480            try : 
    481481                patdict = {}.fromkeys(patterns) 
    482             except AttributeError :     
     482            except AttributeError : 
    483483                # Python v2.2 or earlier 
    484484                patdict = {} 
     
    491491                    users.append(user) 
    492492                    self.cacheEntry("USERS", user.Name, user) 
    493         return users         
    494          
     493        return users 
     494 
    495495    def getMatchingGroups(self, grouppattern) : 
    496496        """Returns the list of all groups for which name matches a certain pattern.""" 
     
    504504            try : 
    505505                patdict = {}.fromkeys(patterns) 
    506             except AttributeError :     
     506            except AttributeError : 
    507507                # Python v2.2 or earlier 
    508508                patdict = {} 
     
    515515                    groups.append(group) 
    516516                    self.cacheEntry("GROUPS", group.Name, group) 
    517         return groups         
    518          
     517        return groups 
     518 
    519519    def getMatchingBillingCodes(self, billingcodepattern) : 
    520520        """Returns the list of all billing codes for which the label matches a certain pattern.""" 
     
    525525            try : 
    526526                patdict = {}.fromkeys(patterns) 
    527             except AttributeError :     
     527            except AttributeError : 
    528528                # Python v2.2 or earlier 
    529529                patdict = {} 
     
    536536                    codes.append(code) 
    537537                    self.cacheEntry("BILLINGCODES", code.BillingCode, code) 
    538         return codes         
    539          
    540     def getPrinterUsersAndQuotas(self, printer, names=["*"]) :         
     538        return codes 
     539 
     540    def getPrinterUsersAndQuotas(self, printer, names=["*"]) : 
    541541        """Returns the list of users who uses a given printer, along with their quotas.""" 
    542542        usersandquotas = [] 
     
    552552                    self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) 
    553553        return usersandquotas 
    554                  
    555     def getPrinterGroupsAndQuotas(self, printer, names=["*"]) :         
     554 
     555    def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : 
    556556        """Returns the list of groups which uses a given printer, along with their quotas.""" 
    557557        groupsandquotas = [] 
     
    565565                    groupsandquotas.append((group, grouppquota)) 
    566566        return groupsandquotas 
    567          
    568     def addPrinter(self, printer) :         
     567 
     568    def addPrinter(self, printer) : 
    569569        """Adds a printer to the quota storage, returns the old value if it already exists.""" 
    570570        oldentry = self.getPrinter(printer.Name) 
     
    580580        printer.isDirty = False 
    581581        return None # the entry created doesn't need further modification 
    582          
     582 
    583583    def addBillingCode(self, bcode) : 
    584584        """Adds a billing code to the quota storage, returns the old value if it already exists.""" 
     
    587587            return oldentry 
    588588        self.doModify("INSERT INTO billingcodes (billingcode, balance, pagecounter, description) VALUES (%s, %s, %s, %s)" \ 
    589                            % (self.doQuote(unicodeToDatabase(bcode.BillingCode)),  
     589                           % (self.doQuote(unicodeToDatabase(bcode.BillingCode)), 
    590590                              self.doQuote(bcode.Balance or 0.0), \ 
    591591                              self.doQuote(bcode.PageCounter or 0), \ 
     
    593593        bcode.isDirty = False 
    594594        return None # the entry created doesn't need further modification 
    595          
    596     def addUser(self, user) :         
     595 
     596    def addUser(self, user) : 
    597597        """Adds a user to the quota storage, returns the old value if it already exists.""" 
    598598        oldentry = self.getUser(user.Name) 
     
    613613        user.isDirty = False 
    614614        return None # the entry created doesn't need further modification 
    615          
    616     def addGroup(self, group) :         
     615 
     616    def addGroup(self, group) : 
    617617        """Adds a group to the quota storage, returns the old value if it already exists.""" 
    618618        oldentry = self.getGroup(group.Name) 
     
    626626        return None # the entry created doesn't need further modification 
    627627 
    628     def addUserToGroup(self, user, group) :     
     628    def addUserToGroup(self, user, group) : 
    629629        """Adds an user to a group.""" 
    630630        result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident))) 
    631631        try : 
    632632            mexists = int(result[0].get("mexists")) 
    633         except (IndexError, TypeError) :     
     633        except (IndexError, TypeError) : 
    634634            mexists = 0 
    635         if not mexists :     
     635        if not mexists : 
    636636            self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident))) 
    637              
    638     def delUserFromGroup(self, user, group) :     
     637 
     638    def delUserFromGroup(self, user, group) : 
    639639        """Removes an user from a group.""" 
    640640        self.doModify("DELETE FROM groupsmembers WHERE groupid=%s AND userid=%s" % \ 
    641641                       (self.doQuote(group.ident), self.doQuote(user.ident))) 
    642              
     642 
    643643    def addUserPQuota(self, upq) : 
    644644        """Initializes a user print quota on a printer.""" 
     
    658658        upq.isDirty = False 
    659659        return None # the entry created doesn't need further modification 
    660          
     660 
    661661    def addGroupPQuota(self, gpq) : 
    662662        """Initializes a group print quota on a printer.""" 
     
    673673        gpq.isDirty = False 
    674674        return None # the entry created doesn't need further modification 
    675          
    676     def savePrinter(self, printer) :     
     675 
     676    def savePrinter(self, printer) : 
    677677        """Saves the printer to the database in a single operation.""" 
    678678        self.doModify("UPDATE printers SET passthrough=%s, maxjobsize=%s, description=%s, priceperpage=%s, priceperjob=%s WHERE id=%s" \ 
     
    683683                                 self.doQuote(printer.PricePerJob or 0.0), \ 
    684684                                 self.doQuote(printer.ident))) 
    685                                   
    686     def saveUser(self, user) :         
     685 
     686    def saveUser(self, user) : 
    687687        """Saves the user to the database in a single operation.""" 
    688688        self.doModify("UPDATE users SET limitby=%s, balance=%s, lifetimepaid=%s, email=%s, overcharge=%s, description=%s WHERE id=%s" \ 
     
    694694                                  self.doQuote(unicodeToDatabase(user.Description)), \ 
    695695                                  self.doQuote(user.ident))) 
    696                                    
    697     def saveGroup(self, group) :         
     696 
     697    def saveGroup(self, group) : 
    698698        """Saves the group to the database in a single operation.""" 
    699699        self.doModify("UPDATE groups SET limitby=%s, description=%s WHERE id=%s" \ 
     
    701701                                  self.doQuote(unicodeToDatabase(group.Description)), \ 
    702702                                  self.doQuote(group.ident))) 
    703          
    704     def writeUserPQuotaDateLimit(self, userpquota, datelimit) :     
     703 
     704    def writeUserPQuotaDateLimit(self, userpquota, datelimit) : 
    705705        """Sets the date limit permanently for a user print quota.""" 
    706706        self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident))) 
    707              
    708     def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) :     
     707 
     708    def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : 
    709709        """Sets the date limit permanently for a group print quota.""" 
    710710        self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident))) 
    711          
    712     def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) :     
     711 
     712    def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) : 
    713713        """Increase page counters for a user print quota.""" 
    714714        self.doModify("UPDATE userpquota SET pagecounter=pagecounter + %s,lifepagecounter=lifepagecounter + %s WHERE id=%s" % (self.doQuote(nbpages), self.doQuote(nbpages), self.doQuote(userpquota.ident))) 
    715         
    716     def saveBillingCode(self, bcode) :     
     715 
     716    def saveBillingCode(self, bcode) : 
    717717        """Saves the billing code to the database.""" 
    718718        self.doModify("UPDATE billingcodes SET balance=%s, pagecounter=%s, description=%s WHERE id=%s" \ 
     
    721721                               self.doQuote(unicodeToDatabase(bcode.Description)), \ 
    722722                               self.doQuote(bcode.ident))) 
    723         
     723 
    724724    def consumeBillingCode(self, bcode, pagecounter, balance) : 
    725725        """Consumes from a billing code.""" 
    726726        self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote(bcode.ident))) 
    727         
    728     def refundJob(self, jobident) :    
     727 
     728    def refundJob(self, jobident) : 
    729729        """Marks a job as refunded in the history.""" 
    730730        self.doModify("UPDATE jobhistory SET action='REFUND' WHERE id=%s;" % self.doQuote(jobident)) 
    731          
    732     def decreaseUserAccountBalance(self, user, amount) :     
     731 
     732    def decreaseUserAccountBalance(self, user, amount) : 
    733733        """Decreases user's account balance from an amount.""" 
    734734        self.doModify("UPDATE users SET balance=balance - %s WHERE id=%s" % (self.doQuote(amount), self.doQuote(user.ident))) 
    735         
     735 
    736736    def writeNewPayment(self, user, amount, comment="") : 
    737737        """Adds a new payment to the payments history.""" 
    738738        if user.ident is not None : 
    739739            self.doModify("INSERT INTO payments (userid, amount, description) VALUES (%s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(amount), self.doQuote(unicodeToDatabase(comment)))) 
    740         else :     
     740        else : 
    741741            self.doModify("INSERT INTO payments (userid, amount, description) VALUES ((SELECT id FROM users WHERE username=%s), %s, %s)" % (self.doQuote(unicodeToDatabase(user.Name)), self.doQuote(amount), self.doQuote(unicodeToDatabase(comment)))) 
    742          
    743     def writeLastJobSize(self, lastjob, jobsize, jobprice) :         
     742 
     743    def writeLastJobSize(self, lastjob, jobsize, jobprice) : 
    744744        """Sets the last job's size permanently.""" 
    745745        self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident))) 
    746          
     746 
    747747    def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None, jobmd5sum=None, jobpages=None, jobbilling=None, precomputedsize=None, precomputedprice=None) : 
    748748        """Adds a job in a printer's history.""" 
    749         if self.privacy :     
     749        if self.privacy : 
    750750            # For legal reasons, we want to hide the title, filename and options 
    751751            title = filename = options = "hidden" 
     
    757757            if jobsize is not None : 
    758758                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))) 
    759             else :     
     759            else : 
    760760                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))) 
    761         else :         
     761        else : 
    762762            # here we explicitly want to reset jobsize to NULL if needed 
    763763            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))) 
    764              
     764 
    765765    def saveUserPQuota(self, userpquota) : 
    766766        """Saves an user print quota entry.""" 
     
    774774                                 self.doQuote(userpquota.MaxJobSize), \ 
    775775                                 self.doQuote(userpquota.ident))) 
    776          
     776 
    777777    def writeUserPQuotaWarnCount(self, userpquota, warncount) : 
    778778        """Sets the warn counter value for a user quota.""" 
    779779        self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" % (self.doQuote(warncount), self.doQuote(userpquota.ident))) 
    780          
     780 
    781781    def increaseUserPQuotaWarnCount(self, userpquota) : 
    782782        """Increases the warn counter value for a user quota.""" 
    783783        self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" % self.doQuote(userpquota.ident)) 
    784          
     784 
    785785    def saveGroupPQuota(self, grouppquota) : 
    786786        """Saves a group print quota entry.""" 
     
    798798            for record in result : 
    799799                children.append(record.get("printerid")) # TODO : put this into the database integrity rules 
    800         if printer.ident not in children :         
     800        if printer.ident not in children : 
    801801            self.doModify("INSERT INTO printergroupsmembers (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) 
    802          
     802 
    803803    def removePrinterFromGroup(self, pgroup, printer) : 
    804804        """Removes a printer from a printer group.""" 
    805805        self.doModify("DELETE FROM printergroupsmembers WHERE groupid=%s AND printerid=%s" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) 
    806          
     806 
    807807    def retrieveHistory(self, user=None, printer=None, hostname=None, billingcode=None, jobid=None, limit=100, start=None, end=None) : 
    808808        """Retrieves all print jobs for user on printer (or all) between start and end date, limited to first 100 results.""" 
     
    813813        if printer is not None : # printer.ident is None anyway if printer doesn't exist 
    814814            where.append("printerid=%s" % self.doQuote(printer.ident)) 
    815         if hostname is not None :     
     815        if hostname is not None : 
    816816            where.append("hostname=%s" % self.doQuote(hostname)) 
    817         if billingcode is not None :     
     817        if billingcode is not None : 
    818818            where.append("billingcode=%s" % self.doQuote(unicodeToDatabase(billingcode))) 
    819         if jobid is not None :     
     819        if jobid is not None : 
    820820            where.append("jobid=%s" % self.doQuote(jobid)) # TODO : jobid is text, so unicodeToDatabase(jobid) but do all of them as well. 
    821         if start is not None :     
     821        if start is not None : 
    822822            where.append("jobdate>=%s" % self.doQuote(start)) 
    823         if end is not None :     
     823        if end is not None : 
    824824            where.append("jobdate<=%s" % self.doQuote(end)) 
    825         if where :     
     825        if where : 
    826826            query += " AND %s" % " AND ".join(where) 
    827827        query += " ORDER BY jobhistory.id DESC" 
     
    829829            # TODO : LIMIT is not supported under DB2. 
    830830            # TODO : so we must use something like " FETCH FIRST %s ROWS ONLY" % self.doQuote(int(limit)) 
    831             query += " LIMIT %s" % self.doQuote(int(limit))  
    832         jobs = []     
    833         result = self.doSearch(query)     
     831            query += " LIMIT %s" % self.doQuote(int(limit)) 
     832        jobs = [] 
     833        result = self.doSearch(query) 
    834834        if result : 
    835835            for fields in result : 
     
    837837                jobs.append(job) 
    838838        return jobs 
    839          
    840     def deleteUser(self, user) :     
     839 
     840    def deleteUser(self, user) : 
    841841        """Completely deletes an user from the database.""" 
    842842        # TODO : What should we do if we delete the last person who used a given printer ? 
    843843        # TODO : we can't reassign the last job to the previous one, because next user would be 
    844844        # TODO : incorrectly charged (overcharged). 
    845         for q in [  
     845        for q in [ 
    846846                    "DELETE FROM payments WHERE userid=%s" % self.doQuote(user.ident), 
    847847                    "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident), 
     
    851851                  ] : 
    852852            self.doModify(q) 
    853              
    854     def multipleQueriesInTransaction(self, queries) :         
     853 
     854    def multipleQueriesInTransaction(self, queries) : 
    855855        """Does many modifications in a single transaction.""" 
    856856        self.beginTransaction() 
     
    858858            for q in queries : 
    859859                self.doModify(q) 
    860         except :     
     860        except : 
    861861            self.rollbackTransaction() 
    862862            raise 
    863         else :     
     863        else : 
    864864            self.commitTransaction() 
    865              
    866     def deleteManyBillingCodes(self, billingcodes) :         
     865 
     866    def deleteManyBillingCodes(self, billingcodes) : 
    867867        """Deletes many billing codes.""" 
    868868        codeids = ", ".join(["%s" % self.doQuote(b.ident) for b in billingcodes]) 
    869869        if codeids : 
    870             self.multipleQueriesInTransaction([  
     870            self.multipleQueriesInTransaction([ 
    871871                    "DELETE FROM billingcodes WHERE id IN (%s)" % codeids,]) 
    872              
    873     def deleteManyUsers(self, users) :         
     872 
     873    def deleteManyUsers(self, users) : 
    874874        """Deletes many users.""" 
    875875        userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users]) 
    876876        if userids : 
    877             self.multipleQueriesInTransaction([  
     877            self.multipleQueriesInTransaction([ 
    878878                    "DELETE FROM payments WHERE userid IN (%s)" % userids, 
    879879                    "DELETE FROM groupsmembers WHERE userid IN (%s)" % userids, 
     
    881881                    "DELETE FROM userpquota WHERE userid IN (%s)" % userids, 
    882882                    "DELETE FROM users WHERE id IN (%s)" % userids,]) 
    883                      
    884     def deleteManyGroups(self, groups) :         
     883 
     884    def deleteManyGroups(self, groups) : 
    885885        """Deletes many groups.""" 
    886886        groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups]) 
    887887        if groupids : 
    888             self.multipleQueriesInTransaction([  
     888            self.multipleQueriesInTransaction([ 
    889889                    "DELETE FROM groupsmembers WHERE groupid IN (%s)" % groupids, 
    890890                    "DELETE FROM grouppquota WHERE groupid IN (%s)" % groupids, 
    891891                    "DELETE FROM groups WHERE id IN (%s)" % groupids,]) 
    892          
     892 
    893893    def deleteManyPrinters(self, printers) : 
    894894        """Deletes many printers.""" 
    895895        printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) 
    896896        if printerids : 
    897             self.multipleQueriesInTransaction([  
     897            self.multipleQueriesInTransaction([ 
    898898                    "DELETE FROM printergroupsmembers WHERE groupid IN (%s) OR printerid IN (%s)" % (printerids, printerids), 
    899899                    "DELETE FROM jobhistory WHERE printerid IN (%s)" % printerids, 
     
    901901                    "DELETE FROM userpquota WHERE printerid IN (%s)" % printerids, 
    902902                    "DELETE FROM printers WHERE id IN (%s)" % printerids,]) 
    903          
    904     def deleteManyUserPQuotas(self, printers, users) :         
     903 
     904    def deleteManyUserPQuotas(self, printers, users) : 
    905905        """Deletes many user print quota entries.""" 
    906906        printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) 
    907907        userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users]) 
    908908        if userids and printerids : 
    909             self.multipleQueriesInTransaction([  
     909            self.multipleQueriesInTransaction([ 
    910910                    "DELETE FROM jobhistory WHERE userid IN (%s) AND printerid IN (%s)" \ 
    911911                                 % (userids, printerids), 
    912912                    "DELETE FROM userpquota WHERE userid IN (%s) AND printerid IN (%s)" \ 
    913913                                 % (userids, printerids),]) 
    914              
     914 
    915915    def deleteManyGroupPQuotas(self, printers, groups) : 
    916916        """Deletes many group print quota entries.""" 
     
    918918        groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups]) 
    919919        if groupids and printerids : 
    920             self.multipleQueriesInTransaction([  
     920            self.multipleQueriesInTransaction([ 
    921921                    "DELETE FROM grouppquota WHERE groupid IN (%s) AND printerid IN (%s)" \ 
    922922                                 % (groupids, printerids),]) 
    923          
    924     def deleteUserPQuota(self, upquota) :     
     923 
     924    def deleteUserPQuota(self, upquota) : 
    925925        """Completely deletes an user print quota entry from the database.""" 
    926         for q in [  
     926        for q in [ 
    927927                    "DELETE FROM jobhistory WHERE userid=%s AND printerid=%s" \ 
    928928                                 % (self.doQuote(upquota.User.ident), self.doQuote(upquota.Printer.ident)), 
     
    930930                  ] : 
    931931            self.doModify(q) 
    932          
    933     def deleteGroupPQuota(self, gpquota) :     
     932 
     933    def deleteGroupPQuota(self, gpquota) : 
    934934        """Completely deletes a group print quota entry from the database.""" 
    935         for q in [  
     935        for q in [ 
    936936                    "DELETE FROM grouppquota WHERE id=%s" % self.doQuote(gpquota.ident), 
    937937                  ] : 
    938938            self.doModify(q) 
    939          
    940     def deleteGroup(self, group) :     
     939 
     940    def deleteGroup(self, group) : 
    941941        """Completely deletes a group from the database.""" 
    942942        for q in [ 
     
    944944                   "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident), 
    945945                   "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident), 
    946                  ] :   
     946                 ] : 
    947947            self.doModify(q) 
    948              
    949     def deletePrinter(self, printer) :     
     948 
     949    def deletePrinter(self, printer) : 
    950950        """Completely deletes a printer from the database.""" 
    951         for q in [  
     951        for q in [ 
    952952                    "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)), 
    953953                    "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident), 
     
    957957                  ] : 
    958958            self.doModify(q) 
    959              
    960     def deleteBillingCode(self, code) :     
     959 
     960    def deleteBillingCode(self, code) : 
    961961        """Completely deletes a billing code from the database.""" 
    962962        for q in [ 
    963963                   "DELETE FROM billingcodes WHERE id=%s" % self.doQuote(code.ident), 
    964                  ] :   
     964                 ] : 
    965965            self.doModify(q) 
    966          
     966