From 29de1056e73196b22f92dbb1c23b2351aa5283e7 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 17 Apr 2017 14:43:17 -0400 Subject: [PATCH] Group V1 serializer fixes from QE feedback Straightforward bug fixes and edits to be more consistent with the 3.1 v1 API structure - remove `inventory_source` as a field from group - add inventory source as a group summary field - fix user_capabilities bug for group start permission - fix 500 error on /inventory_sources/N/update/ --- awx/api/serializers.py | 32 ++++++++++++++++---------------- awx/main/access.py | 3 --- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 9297a6f117..6fad43ba60 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1238,20 +1238,12 @@ class HostSerializer(BaseSerializerWithVariables): class GroupSerializer(BaseSerializerWithVariables): - inventory_source = serializers.SerializerMethodField( - help_text=_('Dedicated inventory source for the group, will be removed in 3.3.')) class Meta: model = Group fields = ('*', 'inventory', 'variables', 'has_active_failures', 'total_hosts', 'hosts_with_active_failures', 'total_groups', - 'groups_with_active_failures', 'has_inventory_sources', 'inventory_source') - - def get_fields(self): # TODO: remove in 3.3 - fields = super(GroupSerializer, self).get_fields() - if not self.V1: - fields.pop('inventory_source') - return fields + 'groups_with_active_failures', 'has_inventory_sources') @property def V1(self): @@ -1268,12 +1260,6 @@ class GroupSerializer(BaseSerializerWithVariables): else: return ['copy', 'edit', 'delete'] - def get_inventory_source(self, obj): # TODO: remove in 3.3 - try: - return obj.deprecated_inventory_source.id - except Group.deprecated_inventory_source.RelatedObjectDoesNotExist: - return None - def build_relational_field(self, field_name, relation_info): field_class, field_kwargs = super(GroupSerializer, self).build_relational_field(field_name, relation_info) # Inventory is read-only unless creating a new group. @@ -1282,6 +1268,20 @@ class GroupSerializer(BaseSerializerWithVariables): field_kwargs.pop('queryset', None) return field_class, field_kwargs + def get_summary_fields(self, obj): # TODO: remove in 3.3 + summary_fields = super(GroupSerializer, self).get_summary_fields(obj) + if self.V1: + try: + inv_src = obj.deprecated_inventory_source + summary_fields['inventory_source'] = {} + for field in SUMMARIZABLE_FK_FIELDS['inventory_source']: + fval = getattr(inv_src, field, None) + if fval is not None: + summary_fields['inventory_source'][field] = fval + except Group.deprecated_inventory_source.RelatedObjectDoesNotExist: + pass + return summary_fields + def get_related(self, obj): res = super(GroupSerializer, self).get_related(obj) res.update(dict( @@ -1507,7 +1507,7 @@ class InventorySourceSerializer(UnifiedJobTemplateSerializer, InventorySourceOpt def get_fields(self): # TODO: remove in 3.3 fields = super(InventorySourceSerializer, self).get_fields() if not self.V1: - fields.pop('group') + fields.pop('group', None) return fields def get_group(self, obj): # TODO: remove in 3.3 diff --git a/awx/main/access.py b/awx/main/access.py index cac7ac88f1..0145a7c3d3 100644 --- a/awx/main/access.py +++ b/awx/main/access.py @@ -354,9 +354,6 @@ class BaseAccess(object): except Group.deprecated_inventory_source.RelatedObjectDoesNotExist: user_capabilities[display_method] = False continue - if obj.inventory_source and not obj.inventory_source._can_update(): - user_capabilities[display_method] = False - continue elif display_method in ['start', 'schedule'] and isinstance(obj, (Project)): if obj.scm_type == '': user_capabilities[display_method] = False