Apply Oslo ModelBase to NeutronBase

Oslo.db has ModelBase which implements most of NeutronBase's code.
Therefore NeutronBase should inherit from ModelBase to keep things DRY.

Fixes bug #1171055

Change-Id: I1a08636a6aa225da8b5d43a0bbcb9b59c057df42
This commit is contained in:
Zhongyue Luo 2013-04-20 16:49:23 +08:00
parent 5be7739099
commit 1f4e8a3ccf

View File

@ -16,21 +16,14 @@
from sqlalchemy.ext import declarative from sqlalchemy.ext import declarative
from sqlalchemy import orm from sqlalchemy import orm
from neutron.openstack.common.db.sqlalchemy import models
class NeutronBase(object):
class NeutronBase(models.ModelBase):
"""Base class for Neutron Models.""" """Base class for Neutron Models."""
__table_args__ = {'mysql_engine': 'InnoDB'} __table_args__ = {'mysql_engine': 'InnoDB'}
def __setitem__(self, key, value):
setattr(self, key, value)
def __getitem__(self, key):
return getattr(self, key)
def get(self, key, default=None):
return getattr(self, key, default)
def __iter__(self): def __iter__(self):
self._i = iter(orm.object_mapper(self).columns) self._i = iter(orm.object_mapper(self).columns)
return self return self
@ -39,22 +32,6 @@ class NeutronBase(object):
n = self._i.next().name n = self._i.next().name
return n, getattr(self, n) return n, getattr(self, n)
def update(self, values):
"""Make the model object behave like a dict."""
for k, v in values.iteritems():
setattr(self, k, v)
def iteritems(self):
"""Make the model object behave like a dict.
Includes attributes from joins.
"""
local = dict(self)
joined = dict([(k, v) for k, v in self.__dict__.iteritems()
if not k[0] == '_'])
local.update(joined)
return local.iteritems()
def __repr__(self): def __repr__(self):
"""sqlalchemy based automatic __repr__ method.""" """sqlalchemy based automatic __repr__ method."""
items = ['%s=%r' % (col.name, getattr(self, col.name)) items = ['%s=%r' % (col.name, getattr(self, col.name))