From: Greg Price Date: Fri, 27 Feb 2009 04:09:28 +0000 (-0500) Subject: small fixes to Record X-Git-Tag: 0.1.8~7 X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-database.git/commitdiff_plain/4688abd0023864267ae3ccd69b84d14c58dd8237 small fixes to Record svn path=/trunk/packages/invirt-database/; revision=2191 --- diff --git a/debian/changelog b/debian/changelog index 21d5a6a..c96b33a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,14 @@ +invirt-database (0.1.8) unstable; urgency=low + + [Peter Iannucci] + * Added Record superclass for models, handling __repr__ consistently. + + [Greg Price] + * use self.c rather than self.__dict__ for SQLAlchemy fields + * make Record._ignore, Owner.get* classmethods + + -- Greg Price Thu, 26 Feb 2009 22:38:02 -0500 + invirt-database (0.1.7) unstable; urgency=low * Disk quotas are measured in gibibytes. diff --git a/python/database/owner.py b/python/database/owner.py index adb176c..43e7ca4 100755 --- a/python/database/owner.py +++ b/python/database/owner.py @@ -15,26 +15,23 @@ class Owner(NullableRecord): _format = dict([(_k,_unitFormatter(_v[1])) for _k,_v in _f.items()]) _identity_field = 'owner_id' - def getMemoryQuotas(owner): - owner_info = Owner.query().filter_by(owner_id=owner).first() + @classmethod + def getMemoryQuotas(cls, owner): + owner_info = cls.query().filter_by(owner_id=owner).first() if owner_info == None: - owner_info = Owner(owner_id=owner) + owner_info = cls(owner_id=owner) return (owner_info.get('ram_quota_total'), owner_info.get('ram_quota_single')) - getMemoryQuotas = staticmethod(getMemoryQuotas) - def getDiskQuotas(owner): - owner_info = Owner.query().filter_by(owner_id=owner).first() + @classmethod + def getDiskQuotas(cls, owner): + owner_info = cls.query().filter_by(owner_id=owner).first() if owner_info == None: - owner_info = Owner(owner_id=owner) + owner_info = cls(owner_id=owner) return (owner_info.get('disk_quota_total'), owner_info.get('disk_quota_single')) - getDiskQuotas = staticmethod(getDiskQuotas) - def getVMQuotas(owner): - owner_info = Owner.query().filter_by(owner_id=owner).first() + @classmethod + def getVMQuotas(cls, owner): + owner_info = cls.query().filter_by(owner_id=owner).first() if owner_info == None: - owner_info = Owner(owner_id=owner) + owner_info = cls(owner_id=owner) return (owner_info.get('vms_quota_total'), owner_info.get('vms_quota_active')) - getVMQuotas = staticmethod(getVMQuotas) - - def _ignore(self): - return super(Owner, self)._ignore() + ['getMemoryQuotas', 'getDiskQuotas', 'getVMQuotas'] diff --git a/python/database/record.py b/python/database/record.py index b1b46bb..4962322 100755 --- a/python/database/record.py +++ b/python/database/record.py @@ -1,10 +1,12 @@ class Record(object): _identity_field = None + def get(self, field): try: return self.__getattribute__(field) except: return None + def _formatField(self, field): v = self.get(field) if callable(v): @@ -16,10 +18,16 @@ class Record(object): return '[%d x %s]'%(len(v), type(v[0])) else: return repr(v) + + @classmethod + def _ignore(cls): + return [cls._identity_field] + def _fields(self): ignore = self._ignore() - keys = sorted(self.__class__.__dict__.keys()) - return [(k,self._formatField(k)) for k in keys if k[0]!="_" and k not in ignore] + keys = sorted(self.c.keys()) + return [(k,self._formatField(k)) for k in keys if k not in ignore] + def __repr__(self): classname = self.__class__.__name__ @@ -34,8 +42,6 @@ class Record(object): payload = ": "+payload return "<%s%s%s>" % (classname, identity, payload) - def _ignore(self): - return [self._identity_field, 'c', 'query', 'get'] class FormattableRecord(Record): _format = {}