standard attributes: expose created_at/updated_at on models

Instead of enforcing all model users to know that those fields belong to
a separate model, consistently use association_proxy for all standard
attributes similar to what we already do with description.

This change should reduce some complexity in db handling code, for
example for versioned objects.

Change-Id: I2df5c3514e1f868baa835adff2d95c1156ec7384
Related-Bug: #1541928
This commit is contained in:
Ihar Hrachyshka 2016-04-05 19:00:21 +02:00
parent b23e52666d
commit 34a328fe12
2 changed files with 13 additions and 5 deletions

View File

@ -141,3 +141,11 @@ class HasStandardAttributes(object):
@declarative.declared_attr
def description(cls):
return association_proxy('standard_attr', 'description')
@declarative.declared_attr
def created_at(cls):
return association_proxy('standard_attr', 'created_at')
@declarative.declared_attr
def updated_at(cls):
return association_proxy('standard_attr', 'updated_at')

View File

@ -70,7 +70,7 @@ class TimeStamp_db_mixin(object):
obj = objs_list.pop()
if (isinstance(obj, model_base.HasStandardAttributes)
and obj.standard_attr_id):
obj.standard_attr.updated_at = timeutils.utcnow()
obj.updated_at = timeutils.utcnow()
def register_db_events(self):
event.listen(model_base.StandardAttribute, 'before_insert',
@ -91,15 +91,15 @@ class TimeStamp_db_mixin(object):
listen_obj)
def _format_timestamp(self, resource_db, result):
result['created_at'] = (resource_db.standard_attr.created_at.
result['created_at'] = (resource_db.created_at.
strftime(self.ISO8601_TIME_FORMAT))
result['updated_at'] = (resource_db.standard_attr.updated_at.
result['updated_at'] = (resource_db.updated_at.
strftime(self.ISO8601_TIME_FORMAT))
def extend_resource_dict_timestamp(self, plugin_obj,
resource_res, resource_db):
if (resource_db and resource_db.standard_attr.created_at and
resource_db.standard_attr.updated_at):
if (resource_db and resource_db.created_at and
resource_db.updated_at):
self._format_timestamp(resource_db, resource_res)
def _add_timestamp(self, mapper, _conn, target):