Author: Alex
Date: 2010-06-21 13:38:24 -0500 (Mon, 21 Jun 2010)
New Revision: 13370

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 order_by, also fixed a typo in "The 
Beatles", sorry.

Modified: 
django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py
===================================================================
--- django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py   
2010-06-21 18:23:34 UTC (rev 13369)
+++ django/branches/soc2010/query-refactor/django/contrib/mongodb/compiler.py   
2010-06-21 18:38:24 UTC (rev 13370)
@@ -1,3 +1,5 @@
+from pymongo import ASCENDING, DESCENDING
+
 from django.db.models.sql.datastructures import FullResultSet
 
 
@@ -62,10 +64,15 @@
         assert not self.query.extra
         assert not self.query.having
         assert self.query.high_mark is None
-        assert not self.query.order_by
         
         filters = self.get_filters(self.query.where)
-        return 
self.connection.db[self.query.model._meta.db_table].find(filters)
+        cursor = 
self.connection.db[self.query.model._meta.db_table].find(filters)
+        if self.query.order_by:
+            cursor = cursor.sort([
+                (ordering.lstrip("-"), DESCENDING if ordering.startswith("-") 
else ASCENDING)
+                for ordering in self.query.order_by
+            ])
+        return cursor
     
     def results_iter(self):
         query = self.build_query()

Modified: 
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py
===================================================================
--- 
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py   
    2010-06-21 18:23:34 UTC (rev 13369)
+++ 
django/branches/soc2010/query-refactor/tests/regressiontests/mongodb/tests.py   
    2010-06-21 18:38:24 UTC (rev 13370)
@@ -63,15 +63,39 @@
         Artist.objects.create(name="Brian May")
         self.assertTrue(Artist.objects.filter(name="Brian May").exists())
     
+    def test_orderby(self):
+        Group.objects.create(name="Queen", year_formed=1971)
+        Group.objects.create(name="The E Street Band", year_formed=1972)
+        Group.objects.create(name="The Beatles", year_formed=1960)
+        
+        self.assertQuerysetEqual(
+            Group.objects.order_by("year_formed"), [
+                "The Beatles",
+                "Queen",
+                "The E Street Band",
+            ],
+            lambda g: g.name
+        )
+        
+        self.assertQuerysetEqual(
+            Group.objects.order_by("-year_formed"), [
+                "The E Street Band",
+                "Queen",
+                "The Beatles",
+            ],
+            lambda g: g.name,
+        )
+        
+    
     def test_not_equals(self):
         q = Group.objects.create(name="Queen", year_formed=1971)
         e = Group.objects.create(name="The E Street Band", year_formed=1972)
-        b = Group.objects.create(name="The Beetles")
+        b = Group.objects.create(name="The Beatles")
         
         self.assertQuerysetEqual(
             Group.objects.exclude(year_formed=1972), [
                 "Queen",
-                "The Beetles",
+                "The Beatles",
             ],
             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.

Reply via email to