]> git.decadent.org.uk Git - dak.git/blobdiff - daklib/dbconn.py
Add the ORMObject.get() class method.
[dak.git] / daklib / dbconn.py
index 5e047847e0b3d36054b174461228371bd08c48c9..837ce8e47b39dfe270d9a70b0f9a2eeed8395a35 100755 (executable)
@@ -262,10 +262,31 @@ class ORMObject(object):
         validation fails.
         '''
         for property in self.not_null_constraints():
+            # TODO: It is a bit awkward that the mapper configuration allow
+            # directly setting the numeric _id columns. We should get rid of it
+            # in the long run.
+            if hasattr(self, property + '_id') and \
+                getattr(self, property + '_id') is not None:
+                continue
             if not hasattr(self, property) or getattr(self, property) is None:
                 raise DBUpdateError(self.validation_message % \
                     (property, str(self)))
 
+    @classmethod
+    @session_wrapper
+    def get(cls, primary_key,  session = None):
+        '''
+        This is a support function that allows getting an object by its primary
+        key.
+
+        Architecture.get(3[, session])
+
+        instead of the more verbose
+
+        session.query(Architecture).get(3)
+        '''
+        return session.query(cls).get(primary_key)
+
 __all__.append('ORMObject')
 
 ################################################################################
@@ -2239,8 +2260,8 @@ class DBSource(ORMObject):
             'install_date']
 
     def not_null_constraints(self):
-        return ['source', 'version', 'maintainer', 'changedby', \
-            'poolfile', 'install_date']
+        return ['source', 'version', 'install_date', 'maintainer', \
+            'changedby', 'poolfile', 'install_date']
 
 __all__.append('DBSource')