Add builds and build_logs tables to invirt-database for tracking build
[invirt/packages/invirt-database.git] / python / database / owner.py
index f9e0128..ac8ecfd 100755 (executable)
@@ -1,60 +1,41 @@
-MAX_MEMORY_TOTAL = 512
-MAX_MEMORY_SINGLE = 512
-MAX_DISK_TOTAL = 50
-MAX_DISK_SINGLE = 50
-MAX_VMS_TOTAL = 10
-MAX_VMS_ACTIVE = 4
+from invirt.database import record
+from invirt.database.models import session
 
-class Owner(object):
-    def __repr__(self):
-        return """<Owner %s: ram_quota_total=%s MB ram_quota_single=%s MB
-disk_quota_total=%s MB disk_quota_single=%s MB
-vms_quota_total=%s vms_quota_active=%s >""" % (self.owner_id, 
-               self.ram_quota_total if self.ram_quota_total else MAX_MEMORY_TOTAL,
-               self.ram_quota_single if self.ram_quota_single else MAX_MEMORY_SINGLE, 
-               self.disk_quota_total if self.disk_quota_total else MAX_DISK_TOTAL,
-               self.disk_quota_single if self.disk_quota_single else MAX_DISK_SINGLE, 
-               self.vms_quota_total if self.vms_quota_total else MAX_VMS_TOTAL,
-               self.vms_quota_active if self.vms_quota_active else MAX_VMS_ACTIVE)
-    def getMemoryQuotas(owner):
-        owner_info = Owner.query().filter_by(owner_id=owner).first()
-        if owner_info != None:
-            quota_total = owner_info.ram_quota_total
-            if quota_total == None:
-                quota_total = MAX_MEMORY_TOTAL
-            quota_single = owner_info.ram_quota_single
-            if quota_single == None:
-                quota_single = MAX_MEMORY_SINGLE
-        else:
-            quota_total = MAX_MEMORY_TOTAL
-            quota_single = MAX_MEMORY_SINGLE
-        return (quota_total, quota_single)
-    getMemoryQuotas = staticmethod(getMemoryQuotas)
-    def getDiskQuotas(owner):
-        owner_info = Owner.query().filter_by(owner_id=owner).first()
-        if owner_info != None:
-            quota_total = owner_info.disk_quota_total
-            if quota_total == None:
-                quota_total = MAX_DISK_TOTAL
-            quota_single = owner_info.disk_quota_single
-            if quota_single == None:
-                quota_single = MAX_DISK_SINGLE
-        else:
-            quota_total = MAX_DISK_TOTAL
-            quota_single = MAX_DISK_SINGLE
-        return (quota_total, quota_single)
-    getDiskQuotas = staticmethod(getDiskQuotas)
-    def getVMQuotas(owner):
-        owner_info = Owner.query().filter_by(owner_id=owner).first()
-        if owner_info != None:
-            quota_total = owner_info.vms_quota_total
-            if quota_total == None:
-                quota_total = MAX_VMS_TOTAL
-            quota_active = owner_info.vms_quota_active
-            if quota_active == None:
-                quota_active = MAX_VMS_ACTIVE
-        else:
-            quota_total = MAX_VMS_TOTAL
-            quota_single = MAX_VMS_ACTIVE
-        return (quota_total, quota_active)
-    getVMQuotas = staticmethod(getVMQuotas)
+class Owner(record.Record):
+    _f = {
+        'ram_quota_total': (512, 'MiB'),
+        'ram_quota_single': (512, 'MiB'),
+        'disk_quota_total': (50, 'GiB'),
+        'disk_quota_single': (50, 'GiB'),
+        'vms_quota_total': (10, ''),
+        'vms_quota_active': (4, '')
+    }
+    _default = dict([(_k,_v[0]) for _k,_v in _f.items()])
+    def _unitFormatter(unit):
+        return lambda v:'%s%s'%(v,unit)
+    _format = dict([(_k,_unitFormatter(_v[1])) for _k,_v in _f.items()])
+    _identity_field = 'owner_id'
+
+    @classmethod
+    def getMemoryQuotas(cls, owner):
+        owner_info = cls.query().get(owner)
+        if owner_info == None:
+            owner_info = cls(owner_id=owner)
+            session.expunge(owner_info)
+        return (owner_info.get('ram_quota_total'), owner_info.get('ram_quota_single'))
+
+    @classmethod
+    def getDiskQuotas(cls, owner):
+        owner_info = cls.query().get(owner)
+        if owner_info == None:
+            owner_info = cls(owner_id=owner)
+            session.expunge(owner_info)
+        return (owner_info.get('disk_quota_total'), owner_info.get('disk_quota_single'))
+
+    @classmethod
+    def getVMQuotas(cls, owner):
+        owner_info = cls.query().get(owner)
+        if owner_info == None:
+            owner_info = cls(owner_id=owner)
+            session.expunge(owner_info)
+        return (owner_info.get('vms_quota_total'), owner_info.get('vms_quota_active'))