From 71dbaffe1aad09f1ec6acc6359caeda518bdf8a9 Mon Sep 17 00:00:00 2001 From: Steve Lacey Date: Fri, 17 Dec 2021 17:17:36 +0700 Subject: [PATCH] Strip lookup field validation --- worf/lookups.py | 2 -- worf/validators.py | 5 ++--- worf/views/base.py | 38 -------------------------------------- worf/views/list.py | 2 -- 4 files changed, 2 insertions(+), 45 deletions(-) diff --git a/worf/lookups.py b/worf/lookups.py index 19de334..8e0459e 100644 --- a/worf/lookups.py +++ b/worf/lookups.py @@ -6,8 +6,6 @@ class FindInstance: def get_instance(self): self.lookup_kwargs = {self.lookup_field: self.kwargs[self.lookup_url_kwarg]} - self.validate_lookup_field_values() - if not hasattr(self, "instance"): self.instance = self.get_queryset().get(**self.lookup_kwargs) diff --git a/worf/validators.py b/worf/validators.py index b7b3627..aef2fee 100644 --- a/worf/validators.py +++ b/worf/validators.py @@ -126,11 +126,10 @@ def validate_numeric(self, value): raise ValidationError(f"Expected numeric, got {value}") def validate_uuid(self, value): - if value is None: - raise ValidationError(f"Expected UUID, got {value}") try: + assert value is not None return UUID(str(value)) - except (TypeError, ValueError): + except (AssertionError, TypeError, ValueError): raise ValidationError(f"Expected UUID, got {value}") def validate_email(self, value): diff --git a/worf/views/base.py b/worf/views/base.py index 7fdc3fb..d454371 100644 --- a/worf/views/base.py +++ b/worf/views/base.py @@ -10,7 +10,6 @@ ObjectDoesNotExist, ValidationError, ) -from django.db import models from django.http import HttpResponse, JsonResponse from django.template.response import TemplateResponse from django.views import View @@ -131,25 +130,6 @@ def _check_permissions(self): ) ) - def _get_lookup_field(self, field): - related = field.find("__") - - """Support one level of related field reference.""" - if related != -1: - related_field = field[:related] - target_field = field[related + 2 :] - - if target_field in ["gt", "lt", "contains", "startswith", "gte", "lte"]: - return False - - if target_field.find("__") != -1: - return False - - return self.get_related_model(related_field)._meta.get_field(target_field) - # TODO if there is another reference, recurse - - return self.model._meta.get_field(field) - def get_related_model(self, field): return self.model._meta.get_field(field).related_model @@ -161,24 +141,6 @@ def get_serializer(self): msg = f"{type(self).__name__}.get_serializer() did not return a serializer" raise ImproperlyConfigured(msg) - def validate_lookup_field_values(self): - # todo check for each lookup kwarg - for field, url_kwarg in self.lookup_kwargs.items(): - lookup_field = self._get_lookup_field(field) - - if isinstance(lookup_field, models.UUIDField): - self.validate_uuid(url_kwarg) - elif isinstance( - lookup_field, - ( - models.ForeignKey, - models.IntegerField, - models.PositiveIntegerField, - models.SmallIntegerField, - ), - ): - self.validate_numeric(url_kwarg) - def flatten_bundle(self, raw_bundle): # parse_qs gives us a dictionary where all values are lists return { diff --git a/worf/views/list.py b/worf/views/list.py index d428999..a27a588 100644 --- a/worf/views/list.py +++ b/worf/views/list.py @@ -74,8 +74,6 @@ def _set_base_lookup_kwargs(self): {self.lookup_field: self.kwargs[self.lookup_url_kwarg]} ) - self.validate_lookup_field_values() - def set_search_lookup_kwargs(self): """ Set generic lookup kwargs based on q and additional GET params.