Make kind read-only for PUT/PATCH, use isinstance in Host Manager, update field fasly check

This commit is contained in:
Wayne Witzel III
2017-05-02 13:00:17 -04:00
parent bdb13ecd71
commit af35838aff
3 changed files with 15 additions and 5 deletions

View File

@@ -1686,6 +1686,15 @@ class InventoryDetail(RetrieveUpdateDestroyAPIView):
model = Inventory model = Inventory
serializer_class = InventoryDetailSerializer serializer_class = InventoryDetailSerializer
def update(self, request, *args, **kwargs):
obj = self.get_object()
kind = self.request.data.get('kind') or kwargs.get('kind')
# Do not allow changes to an Inventory kind.
if kind is not None and obj.kind != kind:
return self.http_method_not_allowed(request, *args, **kwargs)
return super(InventoryDetail, self).update(request, *args, **kwargs)
def destroy(self, request, *args, **kwargs): def destroy(self, request, *args, **kwargs):
with ignore_inventory_computed_fields(): with ignore_inventory_computed_fields():
with ignore_inventory_group_removal(): with ignore_inventory_group_removal():

View File

@@ -331,8 +331,10 @@ class ImplicitRoleField(models.ForeignKey):
class DynamicFilterField(models.TextField): class DynamicFilterField(models.TextField):
def get_prep_value(self, value): def get_prep_value(self, value):
if value is None: # Change any false value to none.
return value # https://docs.python.org/2/library/stdtypes.html#truth-value-testing
if not value:
return None
try: try:
DynamicFilter().query_from_string(value) DynamicFilter().query_from_string(value)
except RuntimeError, e: except RuntimeError, e:

View File

@@ -27,9 +27,8 @@ class HostManager(models.Manager):
set. Use the `host_filter` to generate the queryset for the hosts. set. Use the `host_filter` to generate the queryset for the hosts.
""" """
qs = super(HostManager, self).get_queryset() qs = super(HostManager, self).get_queryset()
if hasattr(self, 'instance') and self.instance is not None: if hasattr(self, 'instance') and isinstance(self.instance, models.Inventory):
if hasattr(self.instance, 'kind') and self.instance.kind == 'dynamic': if self.instance.kind == 'dynamic' and self.instance.host_filter is not None:
if hasattr(self.instance, 'host_filter') and self.instance.host_filter is not None:
q = DynamicFilter.query_from_string(self.instance.host_filter) q = DynamicFilter.query_from_string(self.instance.host_filter)
# If we are using host_filters, disable the core_filters, this allows # If we are using host_filters, disable the core_filters, this allows
# us to access all of the available Host entries, not just the ones associated # us to access all of the available Host entries, not just the ones associated