Skip to content

Commit

Permalink
Strip lookup field validation
Browse files Browse the repository at this point in the history
  • Loading branch information
stevelacey committed Feb 7, 2022
1 parent d6f67ab commit 71dbaff
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 45 deletions.
2 changes: 0 additions & 2 deletions worf/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions worf/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
38 changes: 0 additions & 38 deletions worf/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions worf/views/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 71dbaff

Please sign in to comment.