Changeset 3165 for pykota/trunk/pykota
- Timestamp:
- 04/16/07 18:52:23 (17 years ago)
- Location:
- pykota/trunk/pykota
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/dumper.py
r3142 r3165 80 80 raise PyKotaCommandLineError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype 81 81 82 orderby = options["orderby"] 83 if orderby : 84 fields = [f.strip() for f in orderby.split(",")] 85 orderby = [] 86 for field in fields : 87 if field.isalpha() \ 88 or ((field[0] in ("+", "-")) and field[1:].isalpha()) : 89 orderby.append(field) 90 else : 91 self.printInfo("Skipping invalid ordering statement '%(field)s'" % locals(), "error") 92 else : 93 orderby = [] 94 82 95 extractonly = {} 83 96 if datatype == "all" : … … 147 160 retcode = self.dumpXml(allentries, neededdatatypes) 148 161 else : 149 entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly )162 entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly, orderby) 150 163 if entries : 151 164 nbentries = len(entries) -
pykota/trunk/pykota/storages/ldapstorage.py
r3142 r3165 1574 1574 self.doDelete(code.ident) 1575 1575 1576 def extractPrinters(self, extractonly={} ) :1576 def extractPrinters(self, extractonly={}, ordering=[]) : 1577 1577 """Extracts all printer records.""" 1578 1578 pname = extractonly.get("printername") … … 1588 1588 return result 1589 1589 1590 def extractUsers(self, extractonly={} ) :1590 def extractUsers(self, extractonly={}, ordering=[]) : 1591 1591 """Extracts all user records.""" 1592 1592 uname = extractonly.get("username") … … 1598 1598 return result 1599 1599 1600 def extractBillingcodes(self, extractonly={} ) :1600 def extractBillingcodes(self, extractonly={}, ordering=[]) : 1601 1601 """Extracts all billing codes records.""" 1602 1602 billingcode = extractonly.get("billingcode") … … 1608 1608 return result 1609 1609 1610 def extractGroups(self, extractonly={} ) :1610 def extractGroups(self, extractonly={}, ordering=[]) : 1611 1611 """Extracts all group records.""" 1612 1612 gname = extractonly.get("groupname") … … 1618 1618 return result 1619 1619 1620 def extractPayments(self, extractonly={} ) :1620 def extractPayments(self, extractonly={}, ordering=[]) : 1621 1621 """Extracts all payment records.""" 1622 1622 startdate = extractonly.get("start") … … 1636 1636 return result 1637 1637 1638 def extractUpquotas(self, extractonly={} ) :1638 def extractUpquotas(self, extractonly={}, ordering=[]) : 1639 1639 """Extracts all userpquota records.""" 1640 1640 pname = extractonly.get("printername") … … 1648 1648 return result 1649 1649 1650 def extractGpquotas(self, extractonly={} ) :1650 def extractGpquotas(self, extractonly={}, ordering=[]) : 1651 1651 """Extracts all grouppquota records.""" 1652 1652 pname = extractonly.get("printername") … … 1660 1660 return result 1661 1661 1662 def extractUmembers(self, extractonly={} ) :1662 def extractUmembers(self, extractonly={}, ordering=[]) : 1663 1663 """Extracts all user groups members.""" 1664 1664 gname = extractonly.get("groupname") … … 1673 1673 return result 1674 1674 1675 def extractPmembers(self, extractonly={} ) :1675 def extractPmembers(self, extractonly={}, ordering=[]) : 1676 1676 """Extracts all printer groups members.""" 1677 1677 pname = extractonly.get("printername") … … 1686 1686 return result 1687 1687 1688 def extractHistory(self, extractonly={} ) :1688 def extractHistory(self, extractonly={}, ordering=[]) : 1689 1689 """Extracts all jobhistory records.""" 1690 1690 uname = extractonly.get("username") -
pykota/trunk/pykota/storages/sql.py
r3164 r3165 153 153 return "" 154 154 155 def extractPrinters(self, extractonly={}) : 155 def createOrderBy(self, default, ordering) : 156 """Creates a suitable ORDER BY statement based on a list of fieldnames prefixed with '+' (ASC) or '-' (DESC).""" 157 statements = [] 158 if not ordering : 159 ordering = default 160 for field in ordering : 161 if field.startswith("-") : 162 statements.append("%s DESC" % field[1:]) 163 elif field.startswith("+") : 164 statements.append("%s ASC" % field[1:]) 165 else : 166 statements.append("%s ASC" % field) 167 return ", ".join(statements) 168 169 def extractPrinters(self, extractonly={}, ordering=[]) : 156 170 """Extracts all printer records.""" 157 171 thefilter = self.createFilter(extractonly) 158 172 if thefilter : 159 173 thefilter = "WHERE %s" % thefilter 160 result = self.doRawSearch("SELECT * FROM printers %s ORDER BY id ASC" % thefilter) 174 orderby = self.createOrderBy(["+id"], ordering) 175 result = self.doRawSearch("SELECT * FROM printers %(thefilter)s ORDER BY %(orderby)s" % locals()) 161 176 return self.prepareRawResult(result) 162 177 163 def extractUsers(self, extractonly={} ) :178 def extractUsers(self, extractonly={}, ordering=[]) : 164 179 """Extracts all user records.""" 165 180 thefilter = self.createFilter(extractonly) 166 181 if thefilter : 167 182 thefilter = "WHERE %s" % thefilter 168 result = self.doRawSearch("SELECT * FROM users %s ORDER BY id ASC" % thefilter) 183 orderby = self.createOrderBy(["+id"], ordering) 184 result = self.doRawSearch("SELECT * FROM users %(thefilter)s ORDER BY %(orderby)s" % locals()) 169 185 return self.prepareRawResult(result) 170 186 171 def extractBillingcodes(self, extractonly={} ) :187 def extractBillingcodes(self, extractonly={}, ordering=[]) : 172 188 """Extracts all billing codes records.""" 173 189 thefilter = self.createFilter(extractonly) 174 190 if thefilter : 175 191 thefilter = "WHERE %s" % thefilter 176 result = self.doRawSearch("SELECT * FROM billingcodes %s ORDER BY id ASC" % thefilter) 192 orderby = self.createOrderBy(["+id"], ordering) 193 result = self.doRawSearch("SELECT * FROM billingcodes %(thefilter)s ORDER BY %(orderby)s" % locals()) 177 194 return self.prepareRawResult(result) 178 195 179 def extractGroups(self, extractonly={} ) :196 def extractGroups(self, extractonly={}, ordering=[]) : 180 197 """Extracts all group records.""" 181 198 thefilter = self.createFilter(extractonly) 182 199 if thefilter : 183 200 thefilter = "WHERE %s" % thefilter 184 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) %s GROUP BY groups.id,groups.groupname,groups.limitby,groups.description ORDER BY groups.id ASC" % thefilter) 201 orderby = self.createOrderBy(["+groups.id"], ordering) 202 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()) 185 203 return self.prepareRawResult(result) 186 204 187 def extractPayments(self, extractonly={} ) :205 def extractPayments(self, extractonly={}, ordering=[]) : 188 206 """Extracts all payment records.""" 189 207 startdate = extractonly.get("start") … … 202 220 if enddate : 203 221 thefilter = "%s AND date<=%s" % (thefilter, self.doQuote(enddate)) 204 result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid %s ORDER BY payments.id ASC" % thefilter) 222 orderby = self.createOrderBy(["+payments.id"], ordering) 223 result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid %(thefilter)s ORDER BY %(orderby)s" % locals()) 205 224 return self.prepareRawResult(result) 206 225 207 def extractUpquotas(self, extractonly={} ) :226 def extractUpquotas(self, extractonly={}, ordering=[]) : 208 227 """Extracts all userpquota records.""" 209 228 thefilter = self.createFilter(extractonly) 210 229 if thefilter : 211 230 thefilter = "AND %s" % thefilter 212 result = self.doRawSearch("SELECT users.username,printers.printername,userpquota.* FROM users,printers,userpquota WHERE users.id=userpquota.userid AND printers.id=userpquota.printerid %s ORDER BY userpquota.id ASC" % thefilter) 231 orderby = self.createOrderBy(["+userpquota.id"], ordering) 232 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()) 213 233 return self.prepareRawResult(result) 214 234 215 def extractGpquotas(self, extractonly={} ) :235 def extractGpquotas(self, extractonly={}, ordering=[]) : 216 236 """Extracts all grouppquota records.""" 217 237 thefilter = self.createFilter(extractonly) 218 238 if thefilter : 219 239 thefilter = "AND %s" % thefilter 220 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) %s GROUP BY grouppquota.id,grouppquota.groupid,grouppquota.printerid,grouppquota.softlimit,grouppquota.hardlimit,grouppquota.datelimit,grouppquota.maxjobsize,groups.groupname,printers.printername ORDER BY grouppquota.id ASC" % thefilter) 240 orderby = self.createOrderBy(["+grouppquota.id"], ordering) 241 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()) 221 242 return self.prepareRawResult(result) 222 243 223 def extractUmembers(self, extractonly={} ) :244 def extractUmembers(self, extractonly={}, ordering=[]) : 224 245 """Extracts all user groups members.""" 225 246 thefilter = self.createFilter(extractonly) 226 247 if thefilter : 227 248 thefilter = "AND %s" % thefilter 228 result = self.doRawSearch("SELECT groups.groupname, users.username, groupsmembers.* FROM groups,users,groupsmembers WHERE users.id=groupsmembers.userid AND groups.id=groupsmembers.groupid %s ORDER BY groupsmembers.groupid ASC, groupsmembers.userid ASC" % thefilter) 249 orderby = self.createOrderBy(["+groupsmembers.groupid", "+groupsmembers.userid"], ordering) 250 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()) 229 251 return self.prepareRawResult(result) 230 252 231 def extractPmembers(self, extractonly={} ) :253 def extractPmembers(self, extractonly={}, ordering=[]) : 232 254 """Extracts all printer groups members.""" 233 255 for (k, v) in extractonly.items() : … … 241 263 if thefilter : 242 264 thefilter = "AND %s" % thefilter 243 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 %s ORDER BY printergroupsmembers.groupid ASC, printergroupsmembers.printerid ASC" % thefilter) 265 orderby = self.createOrderBy(["+printergroupsmembers.groupid", "+printergroupsmembers.printerid"], ordering) 266 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()) 244 267 return self.prepareRawResult(result) 245 268 246 def extractHistory(self, extractonly={} ) :269 def extractHistory(self, extractonly={}, ordering=[]) : 247 270 """Extracts all jobhistory records.""" 248 271 startdate = extractonly.get("start") … … 261 284 if enddate : 262 285 thefilter = "%s AND jobdate<=%s" % (thefilter, self.doQuote(enddate)) 263 result = self.doRawSearch("SELECT users.username,printers.printername,jobhistory.* FROM users,printers,jobhistory WHERE users.id=jobhistory.userid AND printers.id=jobhistory.printerid %s ORDER BY jobhistory.id ASC" % thefilter) 286 orderby = self.createOrderBy(["+jobhistory.id"], ordering) 287 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()) 264 288 return self.prepareRawResult(result) 265 289