Changeset 2187 for trunk


Ignore:
Timestamp:
Feb 25, 2009, 4:00:43 PM (16 years ago)
Author:
iannucci
Message:

Added Record superclass for models, handling repr consistently.

Location:
trunk/packages/invirt-database/python/database
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-database/python/database/models.py

    r2150 r2187  
    66from sqlalchemy.ext.assignmapper import assign_mapper
    77
     8from record import *
    89from owner import Owner
    910
     
    101102       PrimaryKeyConstraint('machine_id', 'user'))
    102103
    103 class Machine(object):
    104     def __repr__(self):
    105         return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner)
     104class Machine(Record):
     105    _identity_field = 'name'
    106106
    107 class MachineAccess(object):
    108     def __repr__(self):
    109         return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user)
     107class MachineAccess(Record):
     108    pass
    110109
    111 class NIC(object):
    112     def __repr__(self):
    113         return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname)
     110class NIC(Record):
     111    pass
    114112
    115 class Disk(object):
    116     def __repr__(self):
    117         return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size)
     113class Disk(Record):
     114    pass
    118115
    119 class Type(object):
    120     def __repr__(self):
    121         return "<Type %s: %s>" % (self.type_id, self.description)
     116class Type(Record):
     117    _identity_field = 'type_id'
    122118
    123 class Mirror(object):
    124     def __repr__(self):
    125         return "<Mirror %s>" % (self.mirror_id)
     119class Mirror(Record):
     120    _identity_field = 'mirror_id'
    126121
    127 class CDROM(object):
    128     def __repr__(self):
    129         return "<CDROM %s: %s>" % (self.cdrom_id, self.description)
     122class CDROM(Record):
     123    _identity_field = 'cdrom_id'
    130124
    131 class Autoinstall(object):
    132     def __repr__(self):
    133         return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id)
     125class Autoinstall(Record):
     126    _identity_field = 'autoinstall_id'
    134127
    135128session.mapper(Machine, machine_table,
  • trunk/packages/invirt-database/python/database/owner.py

    r2158 r2187  
    1 MAX_MEMORY_TOTAL = 512
    2 MAX_MEMORY_SINGLE = 512
    3 MAX_DISK_TOTAL = 50
    4 MAX_DISK_SINGLE = 50
    5 MAX_VMS_TOTAL = 10
    6 MAX_VMS_ACTIVE = 4
     1from record import NullableRecord
    72
    8 class Owner(object):
    9     def __repr__(self):
    10         a = self.ram_quota_total
    11         b = self.ram_quota_single
    12         c = self.disk_quota_total
    13         d = self.disk_quota_single
    14         e = self.vms_quota_total
    15         f = self.vms_quota_active
    16         if not a:
    17             a = MAX_MEMORY_TOTAL
    18         if not b:
    19             b = MAX_MEMORY_SINGLE
    20         if not c:
    21             c = MAX_DISK_TOTAL
    22         if not d:
    23             d = MAX_DISK_SINGLE
    24         if not e:
    25             e = MAX_VMS_TOTAL
    26         if not f:
    27             f = MAX_VMS_ACTIVE
    28         return """<Owner %s: ram_quota_total=%s MiB ram_quota_single=%s MiB
    29 disk_quota_total=%s GiB disk_quota_single=%s GiB
    30 vms_quota_total=%s vms_quota_active=%s >""" % (self.owner_id, a,b,c,d,e,f)
     3class Owner(NullableRecord):
     4    _f = {
     5        'ram_quota_total': (512, 'MiB'),
     6        'ram_quota_single': (512, 'MiB'),
     7        'disk_quota_total': (50, 'GiB'),
     8        'disk_quota_single': (50, 'GiB'),
     9        'vms_quota_total': (10, ''),
     10        'vms_quota_active': (4, '')
     11    }
     12    _default = dict([(_k,_v[0]) for _k,_v in _f.items()])
     13    def _unitFormatter(unit):
     14        return lambda v:'%s%s'%(v,unit)
     15    _format = dict([(_k,_unitFormatter(_v[1])) for _k,_v in _f.items()])
     16    _identity_field = 'owner_id'
     17
    3118    def getMemoryQuotas(owner):
    3219        owner_info = Owner.query().filter_by(owner_id=owner).first()
    33         if owner_info != None:
    34             quota_total = owner_info.ram_quota_total
    35             if quota_total == None:
    36                 quota_total = MAX_MEMORY_TOTAL
    37             quota_single = owner_info.ram_quota_single
    38             if quota_single == None:
    39                 quota_single = MAX_MEMORY_SINGLE
    40         else:
    41             quota_total = MAX_MEMORY_TOTAL
    42             quota_single = MAX_MEMORY_SINGLE
    43         return (quota_total, quota_single)
     20        if owner_info == None:
     21            owner_info = Owner(owner_id=owner)
     22        return (owner_info.get('ram_quota_total'), owner_info.get('ram_quota_single'))
    4423    getMemoryQuotas = staticmethod(getMemoryQuotas)
     24
    4525    def getDiskQuotas(owner):
    4626        owner_info = Owner.query().filter_by(owner_id=owner).first()
    47         if owner_info != None:
    48             quota_total = owner_info.disk_quota_total
    49             if quota_total == None:
    50                 quota_total = MAX_DISK_TOTAL
    51             quota_single = owner_info.disk_quota_single
    52             if quota_single == None:
    53                 quota_single = MAX_DISK_SINGLE
    54         else:
    55             quota_total = MAX_DISK_TOTAL
    56             quota_single = MAX_DISK_SINGLE
    57         return (quota_total, quota_single)
     27        if owner_info == None:
     28            owner_info = Owner(owner_id=owner)
     29        return (owner_info.get('disk_quota_total'), owner_info.get('disk_quota_single'))
    5830    getDiskQuotas = staticmethod(getDiskQuotas)
     31
    5932    def getVMQuotas(owner):
    6033        owner_info = Owner.query().filter_by(owner_id=owner).first()
    61         if owner_info != None:
    62             quota_total = owner_info.vms_quota_total
    63             if quota_total == None:
    64                 quota_total = MAX_VMS_TOTAL
    65             quota_active = owner_info.vms_quota_active
    66             if quota_active == None:
    67                 quota_active = MAX_VMS_ACTIVE
    68         else:
    69             quota_total = MAX_VMS_TOTAL
    70             quota_active = MAX_VMS_ACTIVE
    71         return (quota_total, quota_active)
     34        if owner_info == None:
     35            owner_info = Owner(owner_id=owner)
     36        return (owner_info.get('vms_quota_total'), owner_info.get('vms_quota_active'))
    7237    getVMQuotas = staticmethod(getVMQuotas)
     38
     39    def _ignore(self):
     40        return super(Owner, self)._ignore() + ['getMemoryQuotas', 'getDiskQuotas', 'getVMQuotas']
Note: See TracChangeset for help on using the changeset viewer.