Show
Ignore:
Timestamp:
02/23/06 11:58:05 (18 years ago)
Author:
jerome
Message:

Now manage print quota entries the way it should be done (c) (tm) !

Location:
pykota/trunk/pykota/storages
Files:
2 modified

Legend:

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

    r2735 r2749  
    191191                self.tool.logdebug("%s" % entry) 
    192192                self.database.add_s(dn, entry) 
     193            except ldap.ALREADY_EXISTS, msg :         
     194                raise PyKotaStorageError 
    193195            except ldap.LDAPError, msg : 
    194196                message = (_("Problem adding LDAP entry (%s, %s)") % (dn, str(fields))) + " : %s" % str(msg) 
     
    454456            result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s)(pykotaPrinterName=%s))" % \ 
    455457                                      (self.userCharsetToDatabase(user.Name), self.userCharsetToDatabase(printer.Name)), \ 
    456                                       ["pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount"], \ 
     458                                      ["pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount", "pykotaMaxJobSize"], \ 
    457459                                      base=base) 
    458460            if result : 
     
    480482                    else :     
    481483                        userpquota.DateLimit = userpquota.DateLimit[0] 
     484                userpquota.MaxJobSize = fields.get("pykotaMaxJobSize") 
     485                if userpquota.MaxJobSize is not None : 
     486                    if userpquota.MaxJobSize[0].upper() == "NONE" : 
     487                        userpquota.MaxJobSize = None 
     488                    else :     
     489                        userpquota.MaxJobSize = int(userpquota.MaxJobSize[0]) 
    482490                userpquota.Exists = 1 
    483491        return userpquota 
     
    493501            result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s)(pykotaPrinterName=%s))" % \ 
    494502                                      (self.userCharsetToDatabase(group.Name), self.userCharsetToDatabase(printer.Name)), \ 
    495                                       ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], \ 
     503                                      ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaMaxJobSize"], \ 
    496504                                      base=base) 
    497505            if result : 
     
    516524                    else :     
    517525                        grouppquota.DateLimit = grouppquota.DateLimit[0] 
     526                grouppquota.MaxJobSize = fields.get("pykotaMaxJobSize") 
     527                if grouppquota.MaxJobSize is not None : 
     528                    if grouppquota.MaxJobSize[0].upper() == "NONE" : 
     529                        grouppquota.MaxJobSize = None 
     530                    else :     
     531                        grouppquota.MaxJobSize = int(grouppquota.MaxJobSize[0]) 
    518532                grouppquota.PageCounter = 0 
    519533                grouppquota.LifePageCounter = 0 
     
    10181032        raise "Not Implemented !" # TODO !!! 
    10191033                 
    1020     def addUserPQuota(self, user, printer) : 
     1034    def addUserPQuota(self, upq) : 
    10211035        """Initializes a user print quota on a printer.""" 
     1036        # first check if an entry already exists 
     1037        oldentry = self.getUserPQuota(upq.User, upq.Printer) 
     1038        if oldentry.Exists : 
     1039            return oldentry # we return the existing entry 
    10221040        uuid = self.genUUID() 
    1023         uname = self.userCharsetToDatabase(user.Name) 
    1024         pname = self.userCharsetToDatabase(printer.Name) 
     1041        uname = self.userCharsetToDatabase(upq.User.Name) 
     1042        pname = self.userCharsetToDatabase(upq.Printer.Name) 
    10251043        fields = { "cn" : uuid, 
    10261044                   "objectClass" : ["pykotaObject", "pykotaUserPQuota"], 
    10271045                   "pykotaUserName" : uname, 
    10281046                   "pykotaPrinterName" : pname, 
    1029                    "pykotaDateLimit" : "None", 
    1030                    "pykotaPageCounter" : "0", 
    1031                    "pykotaLifePageCounter" : "0", 
    1032                    "pykotaWarnCount" : "0", 
     1047                   "pykotaSoftLimit" : str(upq.SoftLimit), 
     1048                   "pykotaHardLimit" : str(upq.HardLimit), 
     1049                   "pykotaDateLimit" : str(upq.DateLimit), 
     1050                   "pykotaPageCounter" : str(upq.PageCounter or 0), 
     1051                   "pykotaLifePageCounter" : str(upq.LifePageCounter or 0), 
     1052                   "pykotaWarnCount" : str(upq.WarnCount or 0), 
     1053                   "pykotaMaxJobSize" : str(upq.MaxJobSize or 0), 
    10331054                 }  
    10341055        if self.info["userquotabase"].lower() == "user" : 
    1035             dn = "cn=%s,%s" % (uuid, user.ident) 
     1056            dn = "cn=%s,%s" % (uuid, upq.User.ident) 
    10361057        else :     
    10371058            dn = "cn=%s,%s" % (uuid, self.info["userquotabase"]) 
    10381059        self.doAdd(dn, fields) 
    1039         return self.getUserPQuota(user, printer) 
    1040          
    1041     def addGroupPQuota(self, group, printer) : 
     1060        upq.isDirty = False 
     1061        return None # the entry created doesn't need further modification 
     1062         
     1063    def addGroupPQuota(self, gpq) : 
    10421064        """Initializes a group print quota on a printer.""" 
     1065        oldentry = self.getGroupPQuota(gpq.Group, gpq.Printer) 
     1066        if oldentry.Exists : 
     1067            return oldentry # we return the existing entry 
    10431068        uuid = self.genUUID() 
    1044         gname = self.userCharsetToDatabase(group.Name) 
    1045         pname = self.userCharsetToDatabase(printer.Name) 
     1069        gname = self.userCharsetToDatabase(gpq.Group.Name) 
     1070        pname = self.userCharsetToDatabase(gpq.Printer.Name) 
    10461071        fields = { "cn" : uuid, 
    10471072                   "objectClass" : ["pykotaObject", "pykotaGroupPQuota"], 
     
    10511076                 }  
    10521077        if self.info["groupquotabase"].lower() == "group" : 
    1053             dn = "cn=%s,%s" % (uuid, group.ident) 
     1078            dn = "cn=%s,%s" % (uuid, gpq.Group.ident) 
    10541079        else :     
    10551080            dn = "cn=%s,%s" % (uuid, self.info["groupquotabase"]) 
    10561081        self.doAdd(dn, fields) 
    1057         return self.getGroupPQuota(group, printer) 
     1082        gpq.isDirty = False 
     1083        return None # the entry created doesn't need further modification 
    10581084         
    10591085    def savePrinter(self, printer) :     
     
    12041230                   "pykotaHardLimit" : str(userpquota.HardLimit), 
    12051231                   "pykotaDateLimit" : str(userpquota.DateLimit), 
    1206                    "pykotaWarnCount" : str(userpquota.WarnCount), 
    1207                    "pykotaPageCounter" : str(userpquota.PageCounter), 
    1208                    "pykotaLifePageCounter" : str(userpquota.LifePageCounter), 
     1232                   "pykotaWarnCount" : str(userpquota.WarnCount or 0), 
     1233                   "pykotaPageCounter" : str(userpquota.PageCounter or 0), 
     1234                   "pykotaLifePageCounter" : str(userpquota.LifePageCounter or 0), 
     1235                   "pykotaMaxJobSize" : str(userpquota.MaxJobSize or 0), 
    12091236                 } 
    12101237        self.doModify(userpquota.ident, fields) 
     
    12301257                   "pykotaHardLimit" : str(grouppquota.HardLimit), 
    12311258                   "pykotaDateLimit" : str(grouppquota.DateLimit), 
     1259                   "pykotaMaxJobSize" : str(userpquota.MaxJobSize or 0), 
    12321260                 } 
    12331261        self.doModify(grouppquota.ident, fields) 
     
    14271455                self.doDelete(group.ident) 
    14281456                 
     1457    def deleteManyUserPQuotas(self, printers, users) :         
     1458        """Deletes many user print quota entries.""" 
     1459        # TODO : grab all with a single (possibly VERY huge) filter if possible (might depend on the LDAP server !) 
     1460        for printer in printers : 
     1461            for user in users : 
     1462                upq = self.getUserPQuota(user, printer) 
     1463                if upq.Exists : 
     1464                    upq.delete() 
     1465             
     1466    def deleteManyGroupPQuotas(self, printers, groups) : 
     1467        """Deletes many group print quota entries.""" 
     1468        # TODO : grab all with a single (possibly VERY huge) filter if possible (might depend on the LDAP server !) 
     1469        for printer in printers : 
     1470            for group in groups : 
     1471                gpq = self.getGroupPQuota(group, printer) 
     1472                if gpq.Exists : 
     1473                    gpq.delete() 
     1474                 
    14291475    def deleteUserPQuota(self, upquota) :     
    14301476        """Completely deletes an user print quota entry from the database.""" 
  • pykota/trunk/pykota/storages/sql.py

    r2743 r2749  
    644644                       (self.doQuote(group.ident), self.doQuote(user.ident))) 
    645645             
    646     def addUserPQuota(self, user, printer) : 
     646    def addUserPQuota(self, upq) : 
    647647        """Initializes a user print quota on a printer.""" 
    648         self.doModify("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident))) 
    649         return self.getUserPQuota(user, printer) 
    650          
    651     def addGroupPQuota(self, group, printer) : 
     648        try : 
     649            self.doModify("INSERT INTO userpquota (userid, printerid, softlimit, hardlimit, warncount, datelimit, pagecounter, lifepagecounter, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)" \ 
     650                              % (self.doQuote(upq.User.ident), \ 
     651                                 self.doQuote(upq.Printer.ident), \ 
     652                                 self.doQuote(upq.SoftLimit), \ 
     653                                 self.doQuote(upq.HardLimit), \ 
     654                                 self.doQuote(upq.WarnCount), \ 
     655                                 self.doQuote(upq.DateLimit), \ 
     656                                 self.doQuote(upq.PageCounter), \ 
     657                                 self.doQuote(upq.LifePageCounter), \ 
     658                                 self.doQuote(upq.MaxJobSize))) 
     659        except PyKotaStorageError :                          
     660            # TODO : check if this is an error different from a duplicate insert 
     661            # return the existing entry which has to be modified 
     662            return self.getUserPQuota(upq.User, upq.Printer) 
     663        else :     
     664            upq.isDirty = False 
     665            return None # the entry created doesn't need further modification 
     666         
     667    def addGroupPQuota(self, gpq) : 
    652668        """Initializes a group print quota on a printer.""" 
    653         self.doModify("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(printer.ident))) 
    654         return self.getGroupPQuota(group, printer) 
     669        try : 
     670            self.doModify("INSERT INTO grouppquota (groupid, printerid, softlimit, hardlimit, datelimit, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s)" \ 
     671                              % (self.doQuote(gpq.Group.ident), \ 
     672                                 self.doQuote(gpq.Printer.ident), \ 
     673                                 self.doQuote(gpq.SoftLimit), \ 
     674                                 self.doQuote(gpq.HardLimit), \ 
     675                                 self.doQuote(gpq.DateLimit), \ 
     676                                 self.doQuote(gpq.MaxJobSize))) 
     677        except PyKotaStorageError :                          
     678            # TODO : check if this is an error different from a duplicate insert 
     679            # return the existing entry which has to be modified 
     680            return self.getGroupPQuota(gpq.Group, gpq.Printer) 
     681        else :     
     682            gpq.isDirty = False 
     683            return None # the entry created doesn't need further modification 
    655684         
    656685    def savePrinter(self, printer) :     
     
    738767    def saveUserPQuota(self, userpquota) : 
    739768        """Saves an user print quota entry.""" 
    740         self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, warncount=%s, datelimit=%s, pagecounter=%s, lifepagecounter=%s WHERE id=%s" \ 
     769        self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, warncount=%s, datelimit=%s, pagecounter=%s, lifepagecounter=%s, maxjobsize=%s WHERE id=%s" \ 
    741770                              % (self.doQuote(userpquota.SoftLimit), \ 
    742771                                 self.doQuote(userpquota.HardLimit), \ 
     
    745774                                 self.doQuote(userpquota.PageCounter), \ 
    746775                                 self.doQuote(userpquota.LifePageCounter), \ 
     776                                 self.doQuote(userpquota.MaxJobSize), \ 
    747777                                 self.doQuote(userpquota.ident))) 
    748778         
     
    843873            self.doModify(q) 
    844874             
     875    def deleteManyUserPQuotas(self, printers, users) :         
     876        """Deletes many user print quota entries.""" 
     877        printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) 
     878        userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users]) 
     879        for q in [  
     880                    "DELETE FROM jobhistory WHERE userid IN (%s) AND printerid IN (%s)" \ 
     881                                 % (userids, printerids), 
     882                    "DELETE FROM userpquota WHERE userid IN (%s) AND printerid IN (%s)" \ 
     883                                 % (userids, printerids), 
     884                  ] : 
     885            self.doModify(q) 
     886             
     887    def deleteManyGroupPQuotas(self, printers, groups) : 
     888        """Deletes many group print quota entries.""" 
     889        printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) 
     890        groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups]) 
     891        for q in [  
     892                    "DELETE FROM grouppquota WHERE groupid IN (%s) AND printerid IN (%s)" \ 
     893                                 % (groupids, printerids), 
     894                  ] : 
     895            self.doModify(q) 
     896         
    845897    def deleteUserPQuota(self, upquota) :     
    846898        """Completely deletes an user print quota entry from the database."""