Add search level one() method
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Sat, 24 Dec 2011 18:08:20 +0000 (19:08 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Sat, 24 Dec 2011 18:11:12 +0000 (19:11 +0100)
And create a _fix_query_dict which converts '_id' to 'id'.

mediagoblin/db/sql/base.py

index b8d5cc965dd2441e64d8deab00b4063ec2dd5ce3..38b043345db604093373a49e069e62f58ab18e6b 100644 (file)
@@ -4,13 +4,26 @@ from sqlalchemy.orm import scoped_session, sessionmaker
 Session = scoped_session(sessionmaker())
 
 
+def _fix_query_dict(query_dict):
+    if '_id' in query_dict:
+        query_dict['id'] = query_dict.pop('_id')
+
+
 class GMGTableBase(object):
     query = Session.query_property()
 
     @classmethod
     def find(cls, query_dict={}):
+        _fix_query_dict(query_dict)
         return cls.query.filter_by(**query_dict)
 
     @classmethod
     def find_one(cls, query_dict={}):
+        _fix_query_dict(query_dict)
         return cls.query.filter_by(**query_dict).first()
+
+    @classmethod
+    def one(cls, query_dict):
+        retval = cls.find_one(query_dict)
+        assert retval is not None
+        return retval