Anonymous avatar Anonymous committed c4b4dcc

Updated ActiveMapper to support order_by parameters on all relationships.
Thanks to Charles Duffy for this patch!

Comments (0)

Files changed (2)

lib/sqlalchemy/ext/activemapper.py

 #
 class relationship(object):
     def __init__(self, classname, colname=None, backref=None, private=False,
-                 lazy=True, uselist=True, secondary=None):
+                 lazy=True, uselist=True, secondary=None, order_by=False):
         self.classname = classname
         self.colname   = colname
         self.backref   = backref
         self.lazy      = lazy
         self.uselist   = uselist
         self.secondary = secondary
+        self.order_by  = order_by
 
 class one_to_many(relationship):
     def __init__(self, classname, colname=None, backref=None, private=False,
-                 lazy=True):
+                 lazy=True, order_by=False):
         relationship.__init__(self, classname, colname, backref, private, 
-                              lazy, uselist=True)
+                              lazy, uselist=True, order_by=order_by)
 
 class one_to_one(relationship):
     def __init__(self, classname, colname=None, backref=None, private=False,
-                 lazy=True):
+                 lazy=True, order_by=False):
         if backref is not None:
             backref = create_backref(backref, uselist=False)
         relationship.__init__(self, classname, colname, backref, private, 
-                              lazy, uselist=False)
+                              lazy, uselist=False, order_by=order_by)
 
 class many_to_many(relationship):
-    def __init__(self, classname, secondary, backref=None, lazy=True):
+    def __init__(self, classname, secondary, backref=None, lazy=True,
+                 order_by=False):
         relationship.__init__(self, classname, None, backref, False, lazy,
-                              uselist=True, secondary=secondary)
+                              uselist=True, secondary=secondary,
+                              order_by=order_by)
 
 
 # 
         relations = {}
         for propname, reldesc in klass.relations.items():
             relclass = ActiveMapperMeta.classes[reldesc.classname]
+            if isinstance(reldesc.order_by, str):
+                reldesc.order_by = [ reldesc.order_by ]
+            if isinstance(reldesc.order_by, list):
+                for itemno in range(len(reldesc.order_by)):
+                    if isinstance(reldesc.order_by[itemno], str):
+                        reldesc.order_by[itemno] = getattr(relclass.c, reldesc.order_by[itemno])
             relations[propname] = relation(relclass.mapper,
                                            secondary=reldesc.secondary,
                                            backref=reldesc.backref, 
                                            private=reldesc.private, 
                                            lazy=reldesc.lazy, 
-                                           uselist=reldesc.uselist)
+                                           uselist=reldesc.uselist,
+                                           order_by=reldesc.order_by)
         
         class_mapper(klass).add_properties(relations)
         if klass in __deferred_classes__: 

test/ext/activemapper.py

                 cell_phone  = column(String)
                 work_phone  = column(String)
                 prefs_id    = column(Integer, foreign_key=ForeignKey('preferences.id'))
-                addresses   = one_to_many('Address', colname='person_id', backref='person')
+                addresses   = one_to_many('Address', colname='person_id', backref='person', order_by=['state', 'city', 'postal_code'])
                 preferences = one_to_one('Preferences', colname='pref_id', backref='person')
 
             def __str__(self):
     def tearDown(self):
         for t in activemapper.metadata.table_iterator(reverse=True):
             t.delete().execute()
-        #people = Person.select()
-        #for person in people: person.delete()
-        
-        #addresses = Address.select()
-        #for address in addresses: address.delete()
-        
-        #preferences = Preferences.select()
-        #for preference in preferences: preference.delete()
-        
-        #objectstore.flush()
-        #objectstore.clear()
     
     def create_person_one(self):
         # create a person
     # go ahead and setup the database connection, and create the tables
     
     # launch the unit tests
-    unittest.main()
+    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.