Changeset 3165 for pykota/trunk

Show
Ignore:
Timestamp:
04/16/07 18:52:23 (18 years ago)
Author:
jerome
Message:

Added --orderby command line switch to dumpykota.
Doesn't work yet with the LDAP backend, since sorting will have
to be done by PyKota's code instead of relying on the database
backend itself to do the dirty work.

Location:
pykota/trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/dumpykota

    r3133 r3165  
    7878                       WARNING : existing files are truncated ! 
    7979 
     80  -O | --orderby exp   Change the ordering or result. 'exp' is a comma 
     81                       separated list of ordering statements, for example 
     82                       '--orderby +username,-printername'. Not all expression 
     83                       values are meaningful, so using this command line  
     84                       switch is not recommanded if you don't know the  
     85                       exact layout of PyKota's database schema. 
     86                        
    8087  -s | --sum           Summarize the selected datas. 
    8188                           ONLY AVAILABLE WITH --data history or payments 
     
    150157                     "output" : "-", \ 
    151158                   } 
    152         short_options = "vhd:f:o:s" 
    153         long_options = ["help", "version", "data=", "format=", "output=", "sum"] 
     159        short_options = "vhd:f:o:sO:" 
     160        long_options = ["help", "version", "data=", "format=", "output=", "sum", "orderby="] 
    154161         
    155162        # Initializes the command line tool 
     
    167174        options["output"] = options["o"] or options["output"] or defaults["output"] 
    168175        options["sum"] = options["s"] or options["sum"] 
     176        options["orderby"] = options["O"] or options["orderby"] 
    169177         
    170178        if options["help"] : 
  • pykota/trunk/pykota/dumper.py

    r3142 r3165  
    8080            raise PyKotaCommandLineError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype 
    8181                     
     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             
    8295        extractonly = {} 
    8396        if datatype == "all" :             
     
    147160            retcode = self.dumpXml(allentries, neededdatatypes) 
    148161        else :     
    149             entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly) 
     162            entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly, orderby) 
    150163            if entries : 
    151164                nbentries = len(entries) 
  • pykota/trunk/pykota/storages/ldapstorage.py

    r3142 r3165  
    15741574        self.doDelete(code.ident) 
    15751575         
    1576     def extractPrinters(self, extractonly={}) : 
     1576    def extractPrinters(self, extractonly={}, ordering=[]) : 
    15771577        """Extracts all printer records.""" 
    15781578        pname = extractonly.get("printername") 
     
    15881588            return result  
    15891589         
    1590     def extractUsers(self, extractonly={}) : 
     1590    def extractUsers(self, extractonly={}, ordering=[]) : 
    15911591        """Extracts all user records.""" 
    15921592        uname = extractonly.get("username") 
     
    15981598            return result  
    15991599         
    1600     def extractBillingcodes(self, extractonly={}) : 
     1600    def extractBillingcodes(self, extractonly={}, ordering=[]) : 
    16011601        """Extracts all billing codes records.""" 
    16021602        billingcode = extractonly.get("billingcode") 
     
    16081608            return result  
    16091609         
    1610     def extractGroups(self, extractonly={}) : 
     1610    def extractGroups(self, extractonly={}, ordering=[]) : 
    16111611        """Extracts all group records.""" 
    16121612        gname = extractonly.get("groupname") 
     
    16181618            return result  
    16191619         
    1620     def extractPayments(self, extractonly={}) : 
     1620    def extractPayments(self, extractonly={}, ordering=[]) : 
    16211621        """Extracts all payment records.""" 
    16221622        startdate = extractonly.get("start") 
     
    16361636            return result         
    16371637         
    1638     def extractUpquotas(self, extractonly={}) : 
     1638    def extractUpquotas(self, extractonly={}, ordering=[]) : 
    16391639        """Extracts all userpquota records.""" 
    16401640        pname = extractonly.get("printername") 
     
    16481648            return result 
    16491649         
    1650     def extractGpquotas(self, extractonly={}) : 
     1650    def extractGpquotas(self, extractonly={}, ordering=[]) : 
    16511651        """Extracts all grouppquota records.""" 
    16521652        pname = extractonly.get("printername") 
     
    16601660            return result 
    16611661         
    1662     def extractUmembers(self, extractonly={}) : 
     1662    def extractUmembers(self, extractonly={}, ordering=[]) : 
    16631663        """Extracts all user groups members.""" 
    16641664        gname = extractonly.get("groupname") 
     
    16731673            return result         
    16741674                 
    1675     def extractPmembers(self, extractonly={}) : 
     1675    def extractPmembers(self, extractonly={}, ordering=[]) : 
    16761676        """Extracts all printer groups members.""" 
    16771677        pname = extractonly.get("printername") 
     
    16861686            return result         
    16871687         
    1688     def extractHistory(self, extractonly={}) : 
     1688    def extractHistory(self, extractonly={}, ordering=[]) : 
    16891689        """Extracts all jobhistory records.""" 
    16901690        uname = extractonly.get("username") 
  • pykota/trunk/pykota/storages/sql.py

    r3164 r3165  
    153153        return ""         
    154154         
    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=[]) : 
    156170        """Extracts all printer records.""" 
    157171        thefilter = self.createFilter(extractonly) 
    158172        if thefilter : 
    159173            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()) 
    161176        return self.prepareRawResult(result) 
    162177         
    163     def extractUsers(self, extractonly={}) : 
     178    def extractUsers(self, extractonly={}, ordering=[]) : 
    164179        """Extracts all user records.""" 
    165180        thefilter = self.createFilter(extractonly) 
    166181        if thefilter : 
    167182            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()) 
    169185        return self.prepareRawResult(result) 
    170186         
    171     def extractBillingcodes(self, extractonly={}) : 
     187    def extractBillingcodes(self, extractonly={}, ordering=[]) : 
    172188        """Extracts all billing codes records.""" 
    173189        thefilter = self.createFilter(extractonly) 
    174190        if thefilter : 
    175191            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()) 
    177194        return self.prepareRawResult(result) 
    178195         
    179     def extractGroups(self, extractonly={}) : 
     196    def extractGroups(self, extractonly={}, ordering=[]) : 
    180197        """Extracts all group records.""" 
    181198        thefilter = self.createFilter(extractonly) 
    182199        if thefilter : 
    183200            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()) 
    185203        return self.prepareRawResult(result) 
    186204         
    187     def extractPayments(self, extractonly={}) : 
     205    def extractPayments(self, extractonly={}, ordering=[]) : 
    188206        """Extracts all payment records.""" 
    189207        startdate = extractonly.get("start") 
     
    202220        if enddate :  
    203221            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()) 
    205224        return self.prepareRawResult(result) 
    206225         
    207     def extractUpquotas(self, extractonly={}) : 
     226    def extractUpquotas(self, extractonly={}, ordering=[]) : 
    208227        """Extracts all userpquota records.""" 
    209228        thefilter = self.createFilter(extractonly) 
    210229        if thefilter : 
    211230            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()) 
    213233        return self.prepareRawResult(result) 
    214234         
    215     def extractGpquotas(self, extractonly={}) : 
     235    def extractGpquotas(self, extractonly={}, ordering=[]) : 
    216236        """Extracts all grouppquota records.""" 
    217237        thefilter = self.createFilter(extractonly) 
    218238        if thefilter : 
    219239            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()) 
    221242        return self.prepareRawResult(result) 
    222243         
    223     def extractUmembers(self, extractonly={}) : 
     244    def extractUmembers(self, extractonly={}, ordering=[]) : 
    224245        """Extracts all user groups members.""" 
    225246        thefilter = self.createFilter(extractonly) 
    226247        if thefilter : 
    227248            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()) 
    229251        return self.prepareRawResult(result) 
    230252         
    231     def extractPmembers(self, extractonly={}) : 
     253    def extractPmembers(self, extractonly={}, ordering=[]) : 
    232254        """Extracts all printer groups members.""" 
    233255        for (k, v) in extractonly.items() : 
     
    241263        if thefilter : 
    242264            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()) 
    244267        return self.prepareRawResult(result) 
    245268         
    246     def extractHistory(self, extractonly={}) : 
     269    def extractHistory(self, extractonly={}, ordering=[]) : 
    247270        """Extracts all jobhistory records.""" 
    248271        startdate = extractonly.get("start") 
     
    261284        if enddate :  
    262285            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()) 
    264288        return self.prepareRawResult(result) 
    265289             
  • pykota/trunk/TODO

    r3162 r3165  
    3030           
    3131        - --orderby and --order ASC|DESC for dumpykota (or --asc | --desc) 
     32          Now works for SQL backends, not with LDAP yet. 
    3233         
    3334        - Document the PYKOTA_HOME environment variable.