Changeset 2765
- Timestamp:
- 03/01/06 00:07:05 (19 years ago)
- Location:
- pykota/trunk
- Files:
-
- 7 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/edpykota
r2762 r2765 185 185 getattr(self.storage, "deleteMany%sPQuotas" % suffix)(printers, entries) 186 186 self.display("\n") 187 self.d isplay("\r100.00%%\r \r%s\n" % _("Done."))187 self.done() 188 188 elif options["list"] : 189 189 for printer in printers : … … 305 305 percent = 100.0 * float(i) / float(nbtotal) 306 306 self.display("\r%.02f%%" % percent) 307 self.display("\r100.00%%\r \r%s\n" % _("Done.")) 308 307 self.done() 309 308 if __name__ == "__main__" : 310 309 retcode = 0 -
pykota/trunk/bin/pkbcodes
r2725 r2765 30 30 31 31 from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ 32 from pykota.storage import StorageBillingCode 32 33 33 34 __doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s … … 84 85 class PKBcodes(PyKotaTool) : 85 86 """A class for a billing codes manager.""" 87 def modifyBillingCode(self, billingcode, reset, description) : 88 """Modifies a billing code.""" 89 if reset : 90 billingcode.reset() 91 if description is not None : # NB : "" is allowed ! 92 billingcode.setDescription(description) 93 86 94 def main(self, names, options) : 87 95 """Manage billing codes.""" … … 89 97 raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) 90 98 99 if not options["add"] : 100 if not options["list"] : 101 self.display(_("Extracting datas...")) 102 if not names : # NB : can't happen for --delete because it's catched earlier 103 names = ["*"] 104 billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) 105 if not billingcodes : 106 raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) 107 108 reset = options["reset"] 109 description = options["description"] 110 if description : 111 description = options["description"].strip() 112 91 113 if options["delete"] : 92 self.display("%s...\n" % _("Deletion")) 93 todelete = self.storage.getMatchingBillingCodes(",".join(names)) 94 nbtotal = len(todelete) 114 self.display("\n%s..." % _("Deletion")) 115 self.storage.deleteManyBillingCodes(billingcodes) 116 self.display("\n") 117 elif options["add"] : 118 self.display("%s...\n" % _("Creation")) 119 nbtotal = len(names) 95 120 for i in range(nbtotal) : 96 entry = todelete[i] 97 if entry.Exists : 98 entry.delete() 121 bname = names[i] 122 billingcode = StorageBillingCode(self.storage, bname) 123 self.modifyBillingCode(billingcode, reset, description) 124 oldbillingcode = self.storage.addBillingCode(billingcode) 125 if oldbillingcode is not None : 126 if options["skipexisting"] : 127 self.printInfo(_("Billing code [%s] already exists, skipping.") % bname) 128 else : 129 self.printInfo(_("Billing code [%s] already exists, will be modified.") % bname) 130 self.modifyBillingCode(oldbillingcode, reset, description) 131 oldbillingcode.save() 99 132 percent = 100.0 * float(i) / float(nbtotal) 100 133 self.display("\r%.02f%%" % percent) 101 else : 102 if options["add"] : 103 self.display("%s...\n" % _("Creation")) 104 billingcodes = [] 105 nbtotal = len(names) 106 for i in range(nbtotal) : 107 bname = names[i] 108 billingcode = self.storage.getBillingCode(bname) 109 if billingcode.Exists : 110 if options["skipexisting"] : 111 self.printInfo(_("Billing code [%s] already exists, skipping.") % billingcode.BillingCode) 112 else : 113 self.printInfo(_("Billing code [%s] already exists, will be modified.") % billingcode.BillingCode) 114 billingcodes.append(billingcode) 115 else : 116 billingcode = self.storage.addBillingCode(bname) 117 if not billingcode.Exists : 118 raise PyKotaToolError, _("Impossible to add billingcode %s") % bname 119 else : 120 billingcodes.append(billingcode) 121 percent = 100.0 * float(i) / float(nbtotal) 122 self.display("\r%.02f%%" % percent) 123 self.display("\r100.00%%\r \r%s\n" % _("Done.")) 124 else : 125 if not names : 126 names = ["*"] 127 billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) 128 if not billingcodes : 129 raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) 130 134 else : 131 135 if options["list"] : 132 136 for billingcode in billingcodes : … … 140 144 _("credits")) 141 145 else : 142 self.display("%s...\n" % _("Modification")) 143 reset = options["reset"] 144 if reset or options["description"] : # optimize when nothing to do 145 if options["description"] : 146 description = options["description"].strip() 147 nbtotal = len(billingcodes) 148 for i in range(nbtotal) : 149 billingcode = billingcodes[i] 150 if reset : 151 billingcode.reset() 152 if description is not None : # NB : "" is allowed ! 153 billingcode.setDescription(description) 154 billingcode.save() 155 percent = 100.0 * float(i) / float(nbtotal) 156 self.display("\r%.02f%%" % percent) 157 146 self.display("\n%s...\n" % _("Modification")) 147 nbtotal = len(billingcodes) 148 for i in range(nbtotal) : 149 billingcode = billingcodes[i] 150 self.modifyBillingCode(billingcode, reset, description) 151 billingcode.save() 152 percent = 100.0 * float(i) / float(nbtotal) 153 self.display("\r%.02f%%" % percent) 154 158 155 if not options["list"] : 159 self.d isplay("\r100.00%%\r \r%s\n" % _("Done."))156 self.done() 160 157 161 158 if __name__ == "__main__" : -
pykota/trunk/bin/pkusers
r2762 r2765 226 226 percent = 100.0 * float(i) / float(nbtotal) 227 227 self.display("\r%.02f%%" % percent) 228 self.d isplay("\r100.00%%\r \r%s\n" % _("Done."))228 self.done() 229 229 else : 230 230 entries = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) … … 353 353 354 354 if not options["list"] : 355 self.d isplay("\r100.00%%\r \r%s\n" % _("Done."))355 self.done() 356 356 357 357 if __name__ == "__main__" : -
pykota/trunk/NEWS
r2762 r2765 24 24 - 1.24alpha15 : 25 25 26 - optimized pkbcodes using edpykota as the model. 27 26 28 - 'edpykota --list' and 'pkusers --list' are now authorized 27 29 to mere mortal users, but the list is restricted to informations -
pykota/trunk/pykota/storages/ldapstorage.py
r2763 r2765 1462 1462 self.doDelete(group.ident) 1463 1463 1464 def deleteManyBillingCodes FromNames(self, billingcodes) :1465 """Deletes many billing codes from their names."""1466 for bcode in self.getMatchingBillingCodes(",".join(billingcodes)):1464 def deleteManyBillingCodes(self, billingcodes) : 1465 """Deletes many billing codes.""" 1466 for bcode in billingcodes : 1467 1467 bcode.delete() 1468 1468 1469 def deleteManyUsers FromNames(self, usernames) :1470 """Deletes many users from their names."""1471 for user in self.getMatchingUsers(",".join(usernames)) :1469 def deleteManyUsers(self, users) : 1470 """Deletes many users.""" 1471 for user in users : 1472 1472 user.delete() 1473 1473 1474 def deleteManyGroups FromNames(self, groupnames) :1475 """Deletes many groups from their names."""1476 for group in self.getMatchingGroups(",".join(groupnames)) :1474 def deleteManyGroups(self, groups) : 1475 """Deletes many groups.""" 1476 for group in groups : 1477 1477 group.delete() 1478 1478 1479 def deleteManyPrinters FromNames(self, printernames) :1480 """Deletes many printers from their names."""1481 for printer in self.getMatchingPrinters(",".join(printernames)):1479 def deleteManyPrinters(self, printers) : 1480 """Deletes many printers.""" 1481 for printer in printers : 1482 1482 printer.delete() 1483 1483 … … 1701 1701 return code 1702 1702 1703 def addBillingCode(self, label) :1703 def addBillingCode(self, bcode) : 1704 1704 """Adds a billing code to the quota storage, returns it.""" 1705 oldentry = self.getBillingCode(bcode.BillingCode) 1706 if oldentry.Exists : 1707 return oldentry # we return the existing entry 1705 1708 uuid = self.genUUID() 1706 1709 dn = "cn=%s,%s" % (uuid, self.info["billingcodebase"]) 1707 1710 fields = { "objectClass" : ["pykotaObject", "pykotaBilling"], 1708 1711 "cn" : uuid, 1709 "pykotaBillingCode" : self.userCharsetToDatabase(label), 1710 "pykotaPageCounter" : "0", 1711 "pykotaBalance" : "0.0", 1712 "pykotaBillingCode" : self.userCharsetToDatabase(bcode.BillingCode), 1713 "pykotaPageCounter" : str(bcode.PageCounter or 0), 1714 "pykotaBalance" : str(bcode.Balance or 0.0), 1715 "description" : self.userCharsetToDatabase(bcode.Description or ""), 1712 1716 } 1713 1717 self.doAdd(dn, fields) 1714 return self.getBillingCode(label) 1715 1716 def saveBillingCode(self, code) : 1718 bcode.isDirty = False 1719 return None # the entry created doesn't need further modification 1720 1721 def saveBillingCode(self, bcode) : 1717 1722 """Sets the new description for a billing code.""" 1718 1723 fields = { 1719 "description" : self.userCharsetToDatabase( code.Description or ""),1720 "pykotaPageCounter" : str( code.PageCounter),1721 "pykotaBalance" : str( code.Balance),1724 "description" : self.userCharsetToDatabase(bcode.Description or ""), 1725 "pykotaPageCounter" : str(bcode.PageCounter or 0), 1726 "pykotaBalance" : str(bcode.Balance or 0.0), 1722 1727 } 1723 self.doModify( code.ident, fields)1728 self.doModify(bcode.ident, fields) 1724 1729 1725 1730 def getMatchingBillingCodes(self, billingcodepattern) : … … 1744 1749 return codes 1745 1750 1746 def consumeBillingCode(self, code, pagecounter, balance) :1751 def consumeBillingCode(self, bcode, pagecounter, balance) : 1747 1752 """Consumes from a billing code.""" 1748 1753 fields = { … … 1750 1755 "pykotaPageCounter" : { "operator" : "+", "value" : pagecounter, "convert" : int }, 1751 1756 } 1752 return self.doModify( code.ident, fields)1757 return self.doModify(bcode.ident, fields) 1753 1758 -
pykota/trunk/pykota/storages/sql.py
r2763 r2765 532 532 return self.getPrinter(printername) 533 533 534 def addBillingCode(self, label) : 535 """Adds a billing code to the quota storage, returns it.""" 536 self.doModify("INSERT INTO billingcodes (billingcode) VALUES (%s)" % self.doQuote(self.userCharsetToDatabase(label))) 537 return self.getBillingCode(label) 534 def addBillingCode(self, bcode) : 535 """Adds a billing code to the quota storage, returns the old value if it already exists.""" 536 try : 537 self.doModify("INSERT INTO billingcodes (billingcode, balance, pagecounter, description) VALUES (%s, %s, %s, %s)" \ 538 % (self.doQuote(self.userCharsetToDatabase(bcode.BillingCode)), 539 self.doQuote(bcode.Balance or 0.0), \ 540 self.doQuote(bcode.PageCounter or 0), \ 541 self.doQuote(self.userCharsetToDatabase(bcode.Description)))) 542 except PyKotaStorageError : 543 # TODO : check if this is an error different from a duplicate insert 544 # return the existing entry which has to be modified 545 return self.getBillingCode(bcode.BillingCode) 546 else : 547 bcode.isDirty = False 548 return None # the entry created doesn't need further modification 538 549 539 550 def addUser(self, user) : … … 651 662 self.doModify("UPDATE userpquota SET pagecounter=pagecounter + %s,lifepagecounter=lifepagecounter + %s WHERE id=%s" % (self.doQuote(nbpages), self.doQuote(nbpages), self.doQuote(userpquota.ident))) 652 663 653 def saveBillingCode(self, code) :664 def saveBillingCode(self, bcode) : 654 665 """Saves the billing code to the database.""" 655 666 self.doModify("UPDATE billingcodes SET balance=%s, pagecounter=%s, description=%s WHERE id=%s" \ 656 % (self.doQuote( code.Balance or 0.0), \657 self.doQuote( code.PageCounter or 0), \658 self.doQuote(self.userCharsetToDatabase( code.Description)), \659 self.doQuote( code.ident)))667 % (self.doQuote(bcode.Balance or 0.0), \ 668 self.doQuote(bcode.PageCounter or 0), \ 669 self.doQuote(self.userCharsetToDatabase(bcode.Description)), \ 670 self.doQuote(bcode.ident))) 660 671 661 def consumeBillingCode(self, code, pagecounter, balance) :672 def consumeBillingCode(self, bcode, pagecounter, balance) : 662 673 """Consumes from a billing code.""" 663 self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote( code.ident)))674 self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote(bcode.ident))) 664 675 665 676 def decreaseUserAccountBalance(self, user, amount) : … … 813 824 self.commitTransaction() 814 825 815 def deleteManyBillingCodes FromNames(self, billingcodes) :816 """Deletes many billing codes from their names."""817 code labels = ", ".join(["%s" % self.doQuote(b) for b in billingcodes])826 def deleteManyBillingCodes(self, billingcodes) : 827 """Deletes many billing codes.""" 828 codeids = ", ".join(["%s" % self.doQuote(b.ident) for b in billingcodes]) 818 829 self.deleteInTransaction([ 819 "DELETE FROM billingcodes WHERE billingcode IN (%s)" % codelabels,])820 821 def deleteManyUsers FromNames(self, usernames) :822 """Deletes many users from their names."""823 user names = ", ".join(["%s" % self.doQuote(u) for u in usernames])830 "DELETE FROM billingcodes WHERE id IN (%s)" % codeids,]) 831 832 def deleteManyUsers(self, users) : 833 """Deletes many users.""" 834 userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users]) 824 835 self.deleteInTransaction([ 825 "DELETE FROM payments WHERE userid IN ( SELECT id FROM users WHERE username IN %s)" % usernames,826 "DELETE FROM groupsmembers WHERE userid IN ( SELECT id FROM users WHERE username IN %s)" % usernames,827 "DELETE FROM jobhistory WHERE userid IN ( SELECT id FROM users WHERE username IN %s)" % usernames,828 "DELETE FROM userpquota WHERE userid IN ( SELECT id FROM users WHERE username IN %s)" % usernames,829 "DELETE FROM users WHERE username IN %s" % usernames,])830 831 def deleteMany PrintersFromNames(self, printernames) :832 """Deletes many printers from their names."""833 printernames = ", ".join(["%s" % self.doQuote(p) for p in printernames])836 "DELETE FROM payments WHERE userid IN (%s)" % userids, 837 "DELETE FROM groupsmembers WHERE userid IN (%s)" % userids, 838 "DELETE FROM jobhistory WHERE userid IN (%s)" % userids, 839 "DELETE FROM userpquota WHERE userid IN (%s)" % userids, 840 "DELETE FROM users WHERE id IN (%s)" % userids,]) 841 842 def deleteManyGroups(self, groups) : 843 """Deletes many groups.""" 844 groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups]) 834 845 self.deleteInTransaction([ 835 "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)), 836 "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident), 837 "DELETE FROM grouppquota WHERE printerid=%s" % self.doQuote(printer.ident), 838 "DELETE FROM userpquota WHERE printerid=%s" % self.doQuote(printer.ident), 839 "DELETE FROM printers WHERE id=%s" % self.doQuote(printer.ident),]) 846 "DELETE FROM groupsmembers WHERE groupid IN (%s)" % groupids, 847 "DELETE FROM grouppquota WHERE groupid IN (%s)" % groupids, 848 "DELETE FROM groups WHERE id IN (%s)" % groupids,]) 849 850 def deleteManyPrinters(self, printers) : 851 """Deletes many printers.""" 852 printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) 853 self.deleteInTransaction([ 854 "DELETE FROM printergroupsmembers WHERE groupid IN (%s) OR printerid IN (%s)" % printerids, 855 "DELETE FROM jobhistory WHERE printerid IN (%s)" % printerids, 856 "DELETE FROM grouppquota WHERE printerid IN (%s)" % printerids, 857 "DELETE FROM userpquota WHERE printerid IN (%s)" % printerids, 858 "DELETE FROM printers WHERE id IN (%s)" % printerids,]) 840 859 841 860 def deleteManyUserPQuotas(self, printers, users) : -
pykota/trunk/pykota/tool.py
r2762 r2765 201 201 sys.stdout.flush() 202 202 203 def done(self) : 204 """Displays the 'done' message.""" 205 self.display("\r100.00%%\r \r%s\n" % _("Done.")) 206 203 207 def logdebug(self, message) : 204 208 """Logs something to debug output if debug is enabled."""