Allow sub-resources to have standard attributes

Prior to this change a DB model with standard
attributes could declare that it was mapping to
an API resource, but not declare a mapping to a
sub-resources (see bug 1763347).

This change allows DB models with standard attributes
to advertise that they map API *sub*-resources, and
modifies the code that extends DB resources to support
these specific declarations.

Closes-Bug: 1763347
Needed-By: I77ce46c0f33e2a366076d51ce6586fb3008dc6b1

Change-Id: I7630aab5e4f38d0fba862adc2426d4a7ca04a679
This commit is contained in:
Thomas Morin
2018-04-12 15:07:20 +02:00
committed by Thomas Morin
parent d786643214
commit 5179896e77
6 changed files with 87 additions and 18 deletions

View File

@@ -98,6 +98,22 @@ class HasStandardAttributes(object):
return cls.api_collections
raise NotImplementedError("%s must define api_collections" % cls)
@classmethod
def get_api_sub_resources(cls):
"""Define the API sub-resources this object will appear under.
This should return a list of API sub-resources that the object
will be exposed under.
This is used by the standard attr extensions to discover which
sub-resources need to be extended with the standard attr fields
(e.g. created_at/updated_at/etc).
"""
try:
return cls.api_sub_resources
except AttributeError:
return []
@classmethod
def get_collection_resource_map(cls):
try:
@@ -173,17 +189,26 @@ class HasStandardAttributes(object):
self.standard_attr.bump_revision()
def get_standard_attr_resource_model_map():
def _resource_model_map_helper(rs_map, resource, subclass):
if resource in rs_map:
raise RuntimeError("Model %(sub)s tried to register for API resource "
"%(res)s which conflicts with model %(other)s." %
dict(sub=subclass,
other=rs_map[resource],
res=resource))
rs_map[resource] = subclass
def get_standard_attr_resource_model_map(include_resources=True,
include_sub_resources=True):
rs_map = {}
for subclass in HasStandardAttributes.__subclasses__():
for resource in subclass.get_api_collections():
if resource in rs_map:
raise RuntimeError("Model %(sub)s tried to register for "
"API resource %(res)s which conflicts "
"with model %(other)s." %
dict(sub=subclass, other=rs_map[resource],
res=resource))
rs_map[resource] = subclass
if include_resources:
for resource in subclass.get_api_collections():
_resource_model_map_helper(rs_map, resource, subclass)
if include_sub_resources:
for sub_resource in subclass.get_api_sub_resources():
_resource_model_map_helper(rs_map, sub_resource, subclass)
return rs_map