Skip to content

Commit

Permalink
Fixed issue with reverting objects from urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Relrin committed Nov 1, 2016
1 parent 379199b commit 73dd826
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
27 changes: 21 additions & 6 deletions aiorest_ws/db/orm/django/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import OrderedDict

from aiorest_ws.db.orm import relations
from aiorest_ws.exceptions import ImproperlyConfigured
from aiorest_ws.utils.fields import get_attribute, is_simple_callable

from django.core.exceptions import ObjectDoesNotExist
Expand Down Expand Up @@ -138,19 +139,33 @@ def get_object(self, view_name, view_args, view_kwargs):
Takes the matched URL conf arguments, and should return an
object instance, or raise an `ObjectDoesNotExist` exception.
"""
# TODO: Check on getting this values correctly
lookup_value = view_kwargs[self.lookup_url_kwarg]
lookup_kwargs = {self.lookup_field: lookup_value}
return self.get_queryset().get(**lookup_kwargs)
try:
lookup_value = view_kwargs[self.lookup_url_kwarg]
lookup_kwargs = {self.lookup_field: lookup_value}
return self.get_queryset().get(**lookup_kwargs)
except ObjectDoesNotExist:
self.raise_error('does_not_exist')
except KeyError:
raise ImproperlyConfigured(
"Missing primary key in the endpoint path. For fixing it just "
"specify for a requested endpoint URL with included "
"`{field_name}` parameter in the path or override"
"`lookup_url_kwarg` in the constructor for the concrete field."
.format(field_name=self.lookup_url_kwarg)
)
except (TypeError, ValueError):
self.raise_error(
'incorrect_type', data_type=type(view_kwargs).__name__
)

def is_saved_in_database(self, obj):
if not obj or not obj.pk:
return False
return True

def get_lookup_value(self, obj):
# TODO: extract pk fields and return pk values as tuple
pass
pk = getattr(obj, self.lookup_field)
return pk if isinstance(pk, (tuple, list)) else (pk, )


class HyperlinkedIdentityField(relations.HyperlinkedIdentityField,
Expand Down
5 changes: 5 additions & 0 deletions aiorest_ws/db/orm/django/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ def run_validation(self, data=empty):
if is_empty_value:
return data

for field in self.fields.values():
for validator in field.validators:
if hasattr(validator, 'set_context'):
validator.set_context(field)

value = self.to_internal_value(data)
try:
self.run_validators(value)
Expand Down

0 comments on commit 73dd826

Please sign in to comment.