Implement _id proxy on sql objects (on User for now)
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 25 Dec 2011 19:03:11 +0000 (20:03 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Wed, 28 Dec 2011 21:26:00 +0000 (22:26 +0100)
So that the old code can access the primary key still as
"._id".

Quite simple Python Descriptor thing.
Very generic.

mediagoblin/db/sql/models.py

index b87ff3aacfcf585ff1a46c4b2a7edaa32191822f..68b078a5ea4db7dd6584b06d22d7537c6c42053c 100644 (file)
@@ -11,6 +11,18 @@ from mediagoblin.db.sql.base import GMGTableBase
 Base = declarative_base(cls=GMGTableBase)
 
 
+class SimpleFieldAlias(object):
+    """An alias for any field"""
+    def __init__(self, fieldname):
+        self.fieldname = fieldname
+
+    def __get__(self, instance, cls):
+        return getattr(instance, self.fieldname)
+
+    def __set__(self, instance, val):
+        setattr(instance, self.fieldname, val)
+
+
 class User(Base):
     __tablename__ = "users"
 
@@ -32,6 +44,8 @@ class User(Base):
     ## TODO
     # plugin data would be in a separate model
 
+    _id = SimpleFieldAlias("id")
+
 
 class MediaEntry(Base):
     __tablename__ = "media_entries"