2014-09-12 20:03:42 +04:00
|
|
|
from rest_framework.compat import smart_text, urlparse
|
2014-09-02 18:07:56 +04:00
|
|
|
from rest_framework.fields import Field
|
2014-09-03 19:34:09 +04:00
|
|
|
from rest_framework.reverse import reverse
|
2014-09-12 20:03:42 +04:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
|
2014-09-12 20:06:37 +04:00
|
|
|
from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch, Resolver404
|
2014-09-12 13:59:51 +04:00
|
|
|
from django.db.models.query import QuerySet
|
2014-09-12 20:03:42 +04:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-09-02 18:07:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
class RelatedField(Field):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.queryset = kwargs.pop('queryset', None)
|
2014-09-12 20:03:42 +04:00
|
|
|
assert self.queryset is not None or kwargs.get('read_only', None), (
|
2014-09-12 13:59:51 +04:00
|
|
|
'Relational field must provide a `queryset` argument, '
|
|
|
|
'or set read_only=`True`.'
|
|
|
|
)
|
2014-09-12 20:03:42 +04:00
|
|
|
assert not (self.queryset is not None and kwargs.get('read_only', None)), (
|
|
|
|
'Relational fields should not provide a `queryset` argument, '
|
|
|
|
'when setting read_only=`True`.'
|
|
|
|
)
|
2014-09-02 18:07:56 +04:00
|
|
|
super(RelatedField, self).__init__(**kwargs)
|
|
|
|
|
2014-09-18 17:23:00 +04:00
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
# We override this method in order to automagically create
|
|
|
|
# `ManyRelation` classes instead when `many=True` is set.
|
|
|
|
if kwargs.pop('many', False):
|
2014-09-18 17:58:08 +04:00
|
|
|
return ManyRelation(
|
|
|
|
child_relation=cls(*args, **kwargs),
|
|
|
|
read_only=kwargs.get('read_only', False)
|
|
|
|
)
|
2014-09-18 17:23:00 +04:00
|
|
|
return super(RelatedField, cls).__new__(cls, *args, **kwargs)
|
|
|
|
|
2014-09-12 13:59:51 +04:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = self.queryset
|
|
|
|
if isinstance(queryset, QuerySet):
|
|
|
|
# Ensure queryset is re-evaluated whenever used.
|
|
|
|
queryset = queryset.all()
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class StringRelatedField(Field):
|
2014-09-12 20:03:42 +04:00
|
|
|
"""
|
|
|
|
A read only field that represents its targets using their
|
|
|
|
plain string representation.
|
|
|
|
"""
|
|
|
|
|
2014-09-12 13:59:51 +04:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
kwargs['read_only'] = True
|
|
|
|
super(StringRelatedField, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
def to_representation(self, value):
|
|
|
|
return str(value)
|
2014-09-02 18:07:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
class PrimaryKeyRelatedField(RelatedField):
|
2014-09-12 20:03:42 +04:00
|
|
|
default_error_messages = {
|
2014-09-02 18:07:56 +04:00
|
|
|
'required': 'This field is required.',
|
|
|
|
'does_not_exist': "Invalid pk '{pk_value}' - object does not exist.",
|
2014-09-12 20:03:42 +04:00
|
|
|
'incorrect_type': 'Incorrect type. Expected pk value, received {data_type}.',
|
2014-09-02 18:07:56 +04:00
|
|
|
}
|
|
|
|
|
2014-09-12 13:59:51 +04:00
|
|
|
def to_internal_value(self, data):
|
2014-09-02 18:07:56 +04:00
|
|
|
try:
|
2014-09-12 13:59:51 +04:00
|
|
|
return self.get_queryset().get(pk=data)
|
2014-09-02 18:07:56 +04:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
self.fail('does_not_exist', pk_value=data)
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
self.fail('incorrect_type', data_type=type(data).__name__)
|
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
def to_representation(self, value):
|
|
|
|
return value.pk
|
|
|
|
|
2014-09-02 18:07:56 +04:00
|
|
|
|
|
|
|
class HyperlinkedRelatedField(RelatedField):
|
|
|
|
lookup_field = 'pk'
|
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
default_error_messages = {
|
2014-09-02 18:07:56 +04:00
|
|
|
'required': 'This field is required.',
|
|
|
|
'no_match': 'Invalid hyperlink - No URL match',
|
|
|
|
'incorrect_match': 'Invalid hyperlink - Incorrect URL match.',
|
2014-09-12 20:03:42 +04:00
|
|
|
'does_not_exist': 'Invalid hyperlink - Object does not exist.',
|
|
|
|
'incorrect_type': 'Incorrect type. Expected URL string, received {data_type}.',
|
2014-09-02 18:07:56 +04:00
|
|
|
}
|
|
|
|
|
2014-09-18 14:20:56 +04:00
|
|
|
def __init__(self, view_name=None, **kwargs):
|
|
|
|
assert view_name is not None, 'The `view_name` argument is required.'
|
2014-09-12 20:03:42 +04:00
|
|
|
self.view_name = view_name
|
2014-09-02 18:07:56 +04:00
|
|
|
self.lookup_field = kwargs.pop('lookup_field', self.lookup_field)
|
|
|
|
self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field)
|
2014-09-12 20:03:42 +04:00
|
|
|
self.format = kwargs.pop('format', None)
|
|
|
|
|
|
|
|
# We include these simply for dependancy injection in tests.
|
|
|
|
# We can't add them as class attributes or they would expect an
|
|
|
|
# implict `self` argument to be passed.
|
|
|
|
self.reverse = reverse
|
|
|
|
self.resolve = resolve
|
|
|
|
|
2014-09-02 18:07:56 +04:00
|
|
|
super(HyperlinkedRelatedField, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
def get_object(self, view_name, view_args, view_kwargs):
|
|
|
|
"""
|
|
|
|
Return the object corresponding to a matched URL.
|
|
|
|
|
|
|
|
Takes the matched URL conf arguments, and should return an
|
|
|
|
object instance, or raise an `ObjectDoesNotExist` exception.
|
|
|
|
"""
|
|
|
|
lookup_value = view_kwargs[self.lookup_url_kwarg]
|
|
|
|
lookup_kwargs = {self.lookup_field: lookup_value}
|
2014-09-12 13:59:51 +04:00
|
|
|
return self.get_queryset().get(**lookup_kwargs)
|
2014-09-02 18:07:56 +04:00
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
def get_url(self, obj, view_name, request, format):
|
|
|
|
"""
|
|
|
|
Given an object, return the URL that hyperlinks to the object.
|
|
|
|
|
|
|
|
May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
|
|
|
|
attributes are not configured to correctly match the URL conf.
|
|
|
|
"""
|
|
|
|
# Unsaved objects will not yet have a valid URL.
|
|
|
|
if obj.pk is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
lookup_value = getattr(obj, self.lookup_field)
|
|
|
|
kwargs = {self.lookup_url_kwarg: lookup_value}
|
|
|
|
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
2014-09-02 18:07:56 +04:00
|
|
|
try:
|
2014-09-12 20:03:42 +04:00
|
|
|
http_prefix = data.startswith(('http:', 'https:'))
|
2014-09-02 18:07:56 +04:00
|
|
|
except AttributeError:
|
2014-09-12 20:03:42 +04:00
|
|
|
self.fail('incorrect_type', data_type=type(data).__name__)
|
2014-09-02 18:07:56 +04:00
|
|
|
|
|
|
|
if http_prefix:
|
|
|
|
# If needed convert absolute URLs to relative path
|
2014-09-12 20:03:42 +04:00
|
|
|
data = urlparse.urlparse(data).path
|
2014-09-02 18:07:56 +04:00
|
|
|
prefix = get_script_prefix()
|
2014-09-12 20:03:42 +04:00
|
|
|
if data.startswith(prefix):
|
|
|
|
data = '/' + data[len(prefix):]
|
2014-09-02 18:07:56 +04:00
|
|
|
|
|
|
|
try:
|
2014-09-12 20:03:42 +04:00
|
|
|
match = self.resolve(data)
|
2014-09-12 20:06:37 +04:00
|
|
|
except Resolver404:
|
2014-09-02 18:07:56 +04:00
|
|
|
self.fail('no_match')
|
|
|
|
|
|
|
|
if match.view_name != self.view_name:
|
|
|
|
self.fail('incorrect_match')
|
|
|
|
|
|
|
|
try:
|
|
|
|
return self.get_object(match.view_name, match.args, match.kwargs)
|
|
|
|
except (ObjectDoesNotExist, TypeError, ValueError):
|
|
|
|
self.fail('does_not_exist')
|
|
|
|
|
2014-09-12 12:49:35 +04:00
|
|
|
def to_representation(self, value):
|
2014-09-03 19:34:09 +04:00
|
|
|
request = self.context.get('request', None)
|
|
|
|
format = self.context.get('format', None)
|
|
|
|
|
|
|
|
assert request is not None, (
|
2014-09-12 20:03:42 +04:00
|
|
|
"`%s` requires the request in the serializer"
|
2014-09-03 19:34:09 +04:00
|
|
|
" context. Add `context={'request': request}` when instantiating "
|
2014-09-12 20:03:42 +04:00
|
|
|
"the serializer." % self.__class__.__name__
|
2014-09-03 19:34:09 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
# By default use whatever format is given for the current context
|
|
|
|
# unless the target is a different type to the source.
|
|
|
|
#
|
|
|
|
# Eg. Consider a HyperlinkedIdentityField pointing from a json
|
|
|
|
# representation to an html property of that representation...
|
|
|
|
#
|
|
|
|
# '/snippets/1/' should link to '/snippets/1/highlight/'
|
|
|
|
# ...but...
|
|
|
|
# '/snippets/1/.json' should link to '/snippets/1/highlight/.html'
|
|
|
|
if format and self.format and self.format != format:
|
|
|
|
format = self.format
|
|
|
|
|
|
|
|
# Return the hyperlink, or error if incorrectly configured.
|
|
|
|
try:
|
|
|
|
return self.get_url(value, self.view_name, request, format)
|
|
|
|
except NoReverseMatch:
|
|
|
|
msg = (
|
|
|
|
'Could not resolve URL for hyperlinked relationship using '
|
|
|
|
'view name "%s". You may have failed to include the related '
|
|
|
|
'model in your API, or incorrectly configured the '
|
|
|
|
'`lookup_field` attribute on this field.'
|
|
|
|
)
|
2014-09-12 20:03:42 +04:00
|
|
|
raise ImproperlyConfigured(msg % self.view_name)
|
|
|
|
|
|
|
|
|
|
|
|
class HyperlinkedIdentityField(HyperlinkedRelatedField):
|
|
|
|
"""
|
|
|
|
A read-only field that represents the identity URL for an object, itself.
|
|
|
|
|
|
|
|
This is in contrast to `HyperlinkedRelatedField` which represents the
|
|
|
|
URL of relationships to other objects.
|
|
|
|
"""
|
|
|
|
|
2014-09-18 14:20:56 +04:00
|
|
|
def __init__(self, view_name=None, **kwargs):
|
|
|
|
assert view_name is not None, 'The `view_name` argument is required.'
|
2014-09-12 20:03:42 +04:00
|
|
|
kwargs['read_only'] = True
|
|
|
|
kwargs['source'] = '*'
|
|
|
|
super(HyperlinkedIdentityField, self).__init__(view_name, **kwargs)
|
2014-09-03 19:34:09 +04:00
|
|
|
|
2014-09-02 18:07:56 +04:00
|
|
|
|
|
|
|
class SlugRelatedField(RelatedField):
|
2014-09-12 20:03:42 +04:00
|
|
|
"""
|
|
|
|
A read-write field the represents the target of the relationship
|
|
|
|
by a unique 'slug' attribute.
|
|
|
|
"""
|
|
|
|
|
|
|
|
default_error_messages = {
|
|
|
|
'does_not_exist': _("Object with {slug_name}={value} does not exist."),
|
|
|
|
'invalid': _('Invalid value.'),
|
|
|
|
}
|
|
|
|
|
2014-09-18 14:20:56 +04:00
|
|
|
def __init__(self, slug_field=None, **kwargs):
|
|
|
|
assert slug_field is not None, 'The `slug_field` argument is required.'
|
2014-09-12 20:03:42 +04:00
|
|
|
self.slug_field = slug_field
|
|
|
|
super(SlugRelatedField, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
try:
|
|
|
|
return self.get_queryset().get(**{self.slug_field: data})
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
self.fail('does_not_exist', slug_name=self.slug_field, value=smart_text(data))
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
self.fail('invalid')
|
|
|
|
|
|
|
|
def to_representation(self, obj):
|
|
|
|
return getattr(obj, self.slug_field)
|
2014-09-18 17:23:00 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ManyRelation(Field):
|
|
|
|
"""
|
|
|
|
Relationships with `many=True` transparently get coerced into instead being
|
|
|
|
a ManyRelation with a child relationship.
|
|
|
|
|
|
|
|
The `ManyRelation` class is responsible for handling iterating through
|
|
|
|
the values and passing each one to the child relationship.
|
|
|
|
|
|
|
|
You shouldn't need to be using this class directly yourself.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, child_relation=None, *args, **kwargs):
|
|
|
|
self.child_relation = child_relation
|
|
|
|
assert child_relation is not None, '`child_relation` is a required argument.'
|
|
|
|
super(ManyRelation, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def bind(self, field_name, parent, root):
|
|
|
|
# ManyRelation needs to provide the current context to the child relation.
|
|
|
|
super(ManyRelation, self).bind(field_name, parent, root)
|
|
|
|
self.child_relation.bind(field_name, parent, root)
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
return [
|
|
|
|
self.child_relation.to_internal_value(item)
|
|
|
|
for item in data
|
|
|
|
]
|
|
|
|
|
|
|
|
def to_representation(self, obj):
|
|
|
|
return [
|
|
|
|
self.child_relation.to_representation(value)
|
|
|
|
for value in obj.all()
|
|
|
|
]
|