Ticket #1465: trunk-regex_field_lookup.diff
File trunk-regex_field_lookup.diff, 5.0 KB (added by , 19 years ago) |
---|
-
contrib/admin/views/main.py
219 219 break 220 220 lookup_params['order_by'] = ((order_type == 'desc' and '-' or '') + lookup_order_field,) 221 221 if lookup_opts.admin.search_fields and query: 222 complex_queries = []223 for bit in query.split():222 # regular expression 223 if len(query) > 2 and query[0] == '/' and query[-1] == '/': 224 224 or_queries = [] 225 225 for field_name in lookup_opts.admin.search_fields: 226 or_queries.append(meta.Q(**{'%s__icontains' % field_name: bit})) 227 complex_queries.append(reduce(operator.or_, or_queries)) 228 lookup_params['complex'] = reduce(operator.and_, complex_queries) 226 or_queries.append(meta.Q(**{'%s__iregex' % field_name: query[1:-1]})) 227 lookup_params['complex'] = reduce(operator.or_, or_queries) 228 else: 229 complex_queries = [] 230 for bit in query.split(): 231 or_queries = [] 232 for field_name in lookup_opts.admin.search_fields: 233 or_queries.append(meta.Q(**{'%s__icontains' % field_name: bit})) 234 complex_queries.append(reduce(operator.or_, or_queries)) 235 lookup_params['complex'] = reduce(operator.and_, complex_queries) 229 236 if opts.one_to_one_field: 230 237 lookup_params.update(opts.one_to_one_field.rel.limit_choices_to) 231 238 self.lookup_params = lookup_params -
core/db/backends/postgresql.py
172 172 'iexact': 'ILIKE %s', 173 173 'contains': 'LIKE %s', 174 174 'icontains': 'ILIKE %s', 175 'regex': '~ %s', 176 'iregex': '~* %s', 175 177 'ne': '!= %s', 176 178 'gt': '> %s', 177 179 'gte': '>= %s', -
core/db/backends/sqlite3.py
36 36 # register extract and date_trun functions 37 37 self.connection.create_function("django_extract", 2, _sqlite_extract) 38 38 self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) 39 self.connection.create_function("regexp", 2, _sqlite_regexp) 39 40 cursor = self.connection.cursor(factory=SQLiteCursorWrapper) 40 41 cursor.row_factory = utf8rowFactory 41 42 if DEBUG: … … 137 138 def get_indexes(cursor, table_name): 138 139 raise NotImplementedError 139 140 141 def _sqlite_regexp(re_pattern, re_string): 142 import re 143 try: 144 return bool(re.search(re_pattern, re_string, re.I)) 145 except: 146 return False 147 140 148 # Operators and fields ######################################################## 141 149 142 150 # SQLite requires LIKE statements to include an ESCAPE clause if the value … … 147 155 'iexact': "LIKE %s ESCAPE '\\'", 148 156 'contains': "LIKE %s ESCAPE '\\'", 149 157 'icontains': "LIKE %s ESCAPE '\\'", 158 'regex': "REGEXP %s", 159 'iregex': "REGEXP %s", 150 160 'ne': '!= %s', 151 161 'gt': '> %s', 152 162 'gte': '>= %s', -
core/db/backends/mysql.py
156 156 'iexact': 'LIKE %s', 157 157 'contains': 'LIKE BINARY %s', 158 158 'icontains': 'LIKE %s', 159 'regex': 'REGEXP %s', 160 'iregex': 'REGEXP %s', 159 161 'ne': '!= %s', 160 162 'gt': '> %s', 161 163 'gte': '>= %s', -
core/meta/__init__.py
1349 1349 return "%s = %%s" % db.get_date_extract_sql(lookup_type, table_prefix + field_name) 1350 1350 elif lookup_type == 'isnull': 1351 1351 return "%s%s IS %sNULL" % (table_prefix, field_name, (not value and 'NOT ' or '')) 1352 elif lookup_type in ('regex', 'iregex'): 1353 raise NotImplementedError 1352 1354 raise TypeError, "Got invalid lookup_type: %s" % repr(lookup_type) 1353 1355 1354 1356 def function_get_object(opts, klass, does_not_exist_exception, **kwargs): -
core/meta/fields.py
175 175 176 176 def get_db_prep_lookup(self, lookup_type, value): 177 177 "Returns field's value prepared for database lookup." 178 if lookup_type in ('exact', ' gt', 'gte', 'lt', 'lte', 'ne', 'year', 'month', 'day'):178 if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'ne', 'year', 'month', 'day'): 179 179 return [value] 180 180 elif lookup_type in ('range', 'in'): 181 181 return value