SubList views can now resolve deep relationships using dot notation for relationship specification

Made it so you can specify a relationship like 'parent.somelist'
This commit is contained in:
Akita Noek
2016-02-22 11:07:32 -05:00
parent 1e1f49c3eb
commit 0ff94e424d
2 changed files with 23 additions and 5 deletions

View File

@@ -359,7 +359,7 @@ class SubListCreateAttachDetachAPIView(SubListCreateAPIView):
def attach(self, request, *args, **kwargs): def attach(self, request, *args, **kwargs):
created = False created = False
parent = self.get_parent_object() parent = self.get_parent_object()
relationship = getattr(parent, self.relationship) relationship = getattrd(parent, self.relationship)
sub_id = request.data.get('id', None) sub_id = request.data.get('id', None)
data = request.data data = request.data
@@ -405,7 +405,7 @@ class SubListCreateAttachDetachAPIView(SubListCreateAPIView):
parent = self.get_parent_object() parent = self.get_parent_object()
parent_key = getattr(self, 'parent_key', None) parent_key = getattr(self, 'parent_key', None)
relationship = getattr(parent, self.relationship) relationship = getattrd(parent, self.relationship)
sub = get_object_or_400(self.model, pk=sub_id) sub = get_object_or_400(self.model, pk=sub_id)
if not request.user.can_access(self.parent_model, 'unattach', parent, if not request.user.can_access(self.parent_model, 'unattach', parent,

View File

@@ -30,7 +30,7 @@ __all__ = ['get_object_or_400', 'get_object_or_403', 'camelcase_to_underscore',
'get_ansible_version', 'get_ssh_version', 'get_awx_version', 'update_scm_url', 'get_ansible_version', 'get_ssh_version', 'get_awx_version', 'update_scm_url',
'get_type_for_model', 'get_model_for_type', 'to_python_boolean', 'get_type_for_model', 'get_model_for_type', 'to_python_boolean',
'ignore_inventory_computed_fields', 'ignore_inventory_group_removal', 'ignore_inventory_computed_fields', 'ignore_inventory_group_removal',
'_inventory_updates', 'get_pk_from_dict'] '_inventory_updates', 'get_pk_from_dict', 'getattrd', 'NoDefaultProvided']
def get_object_or_400(klass, *args, **kwargs): def get_object_or_400(klass, *args, **kwargs):
@@ -521,3 +521,21 @@ def timedelta_total_seconds(timedelta):
timedelta.microseconds + 0.0 + timedelta.microseconds + 0.0 +
(timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6 (timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6
class NoDefaultProvided(object):
pass
def getattrd(obj, name, default=NoDefaultProvided):
"""
Same as getattr(), but allows dot notation lookup
Discussed in:
http://stackoverflow.com/questions/11975781
"""
try:
return reduce(getattr, name.split("."), obj)
except AttributeError:
if default != NoDefaultProvided:
return default
raise