RSS

(root)/packagedb/0.5.x : 621 : pkgdb/letter_paginator.py

To get this branch, use:
bzr branch /bzr/packagedb/0.5.x

« back to all changes in this revision

Viewing changes to pkgdb/letter_paginator.py

Toshio Kuratomi
2012-03-01 23:30:31
Revision ID: toshio@fedoraproject.org-20120301233031-dd454z15aajyhqfc
Fix search to escape sql wildcards and handle both * and ? shell-style globs

Show diffs side-by-side

added added

removed removed

57
57
        '''Return a list of all packages in the database.
58
58
 
59
59
           :kwarg searchwords: optional - string to restrict the list, can use
60
 
           % or * as wildcards
 
60
                * and ? as wildcards
61
61
        '''
 
62
        if searchwords:
 
63
            # Escape special chars and turn shell-style '*' and '?' wildcards
 
64
            # into sql '%' and '_' wildcards
 
65
            sql_searchwords = searchwords.replace('\\\\', '\\\\\\\\')\
 
66
                    .replace('%', '\\%').replace('_', '\\_')\
 
67
                    .replace('*', '%').replace('?', '_')
 
68
 
62
69
        server_webpath = config.get('server.webpath', '/pkgdb')
63
70
        if request.path.startswith("%s/acls/" % server_webpath):
64
71
            if request.path.startswith('%s/acls/bugs/' % server_webpath):
68
75
            else:
69
76
                mode = 'acls/name/'
70
77
                bzUrl = ''
71
 
            if searchwords != '':
72
 
                searchwords = searchwords.replace('*','%')
73
 
                if searchwords.isdigit() and int(searchwords) < 10: # 0-9
 
78
            if sql_searchwords != '':
 
79
                if sql_searchwords.isdigit() and int(sql_searchwords) < 10: # 0-9
74
80
                    #pylint:disable-msg=E1101
75
81
                    packages = Package.query.options(
76
82
                            lazyload('listings2'), lazyload('status')
78
84
                                     Package.name.like('9%')))
79
85
                    #pylint:enable-msg=E1101
80
86
                else: 
81
 
                    # sanitize for ilike:
82
 
                    searchwords = searchwords.replace('&','').replace('_','') 
83
87
                    #pylint:disable-msg=E1101
84
88
                    packages = Package.query.options(
85
89
                        lazyload('listings2'),
86
90
                        lazyload('status')).filter(
87
 
                            Package.name.ilike(searchwords)
 
91
                            Package.name.ilike(sql_searchwords, escape='\\\\')
88
92
                            ).order_by(Package.name.asc())
89
93
                    #pylint:enable-msg=E1101
90
94
            else:
100
104
        else:
101
105
            mode = 'tag/'
102
106
            bzUrl = ''
103
 
            if searchwords != '':
104
 
                searchwords = searchwords.replace('*','%') \
105
 
                              .replace('&','').replace('_','')
106
 
 
 
107
            if sql_searchwords:
107
108
                #pylint:disable-msg=E1101
108
109
                packages = session.query(Application).join('tags').filter(
109
 
                        Tag.name.ilike(searchwords)).all()
 
110
                        Tag.name.ilike(sql_searchwords, escape='\\\\')).all()
110
111
                #pylint:enable-msg=E1101
111
112
            else:
112
113
                packages = PackageBuild.query.all() #pylint:disable-msg=E1101
125
126
                        pkglisting.collection.branchname
126
127
        statusMap = dict([(statuscode, STATUS[statuscode]) for statuscode in statuses])
127
128
 
128
 
        searchwords = searchwords.replace('%','*')
129
 
 
130
129
        return dict(title=_('%(app)s -- Packages Overview %(mode)s') % {
131
130
            'app': self.app_title, 'mode': mode.strip('/')},
132
131
                       searchwords=searchwords, packages=pkg_list, mode=mode,

Loggerhead 1.18.1 is a web-based interface for Bazaar branches