Author: Alex
Date: 2010-06-22 18:05:44 -0500 (Tue, 22 Jun 2010)
New Revision: 13389
Modified:
django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
Log:
[soc2010/query-refactor] Implemented __regex and __iregex.
Modified:
django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
===================================================================
--- django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
2010-06-22 21:19:39 UTC (rev 13388)
+++ django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
2010-06-22 23:05:44 UTC (rev 13389)
@@ -1,3 +1,5 @@
+import re
+
from pymongo import ASCENDING, DESCENDING
from django.db.models.sql.datastructures import FullResultSet, EmptyResultSet
@@ -11,6 +13,8 @@
"isnull": lambda params, value_annotation, negated: {"$ne": None} if
value_annotation == negated else None,
"gt": lambda params, value_annotation, negated: {"$gt": params[0]},
"in": lambda params, value_annotation, negated: {"$in": params},
+ "regex": lambda params, value_annotation, negated:
re.compile(params[0]),
+ "iregex": lambda params, value_annotations, negated:
re.compile(params[0], re.I)
}
def __init__(self, query, connection, using):
@@ -54,7 +58,9 @@
return column, self.LOOKUP_TYPES[lookup_type](params,
value_annotation, negated)
def negate(self, k, v):
- if isinstance(v, dict):
+ # Regex lookups are of the form {"field": re.compile("pattern") and
+ # need to be negated with $not, not $ne.
+ if isinstance(v, dict) or isinstance(v, re._pattern_type):
return {k: {"$not": v}}
return {k: {"$ne": v}}
Modified:
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
===================================================================
---
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
2010-06-22 21:19:39 UTC (rev 13388)
+++
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
2010-06-22 23:05:44 UTC (rev 13389)
@@ -284,3 +284,31 @@
],
lambda g: g.name,
)
+
+ def test_regex(self):
+ q = Group.objects.create(name="Queen")
+ e = Group.objects.create(name="The E Street Band")
+ b = Group.objects.create(name="The Beatles")
+
+ self.assertQuerysetEqual(
+ Group.objects.filter(name__regex="^The"), [
+ "The E Street Band",
+ "The Beatles",
+ ],
+ lambda g: g.name
+ )
+
+ self.assertQuerysetEqual(
+ Group.objects.filter(name__iregex="^the"), [
+ "The E Street Band",
+ "The Beatles",
+ ],
+ lambda g: g.name
+ )
+
+ self.assertQuerysetEqual(
+ Group.objects.exclude(name__regex="^The"), [
+ "Queen",
+ ],
+ lambda g: g.name,
+ )
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.