2014-09-24 17:09:49 +04:00
|
|
|
"""
|
2014-12-04 05:11:42 +03:00
|
|
|
The metadata API is used to allow customization of how `OPTIONS` requests
|
2014-09-24 17:09:49 +04:00
|
|
|
are handled. We currently provide a single default implementation that returns
|
|
|
|
some fairly ad-hoc information about the view.
|
|
|
|
|
2014-12-04 05:11:42 +03:00
|
|
|
Future implementations might use JSON schema or other definitions in order
|
2014-09-24 17:09:49 +04:00
|
|
|
to return this information in a more standardized way.
|
|
|
|
"""
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.http import Http404
|
2014-12-04 05:11:42 +03:00
|
|
|
from django.utils.encoding import force_text
|
2014-09-24 17:09:49 +04:00
|
|
|
from rest_framework import exceptions, serializers
|
2014-12-04 05:11:42 +03:00
|
|
|
from rest_framework.compat import OrderedDict
|
2014-09-24 17:09:49 +04:00
|
|
|
from rest_framework.request import clone_request
|
|
|
|
from rest_framework.utils.field_mapping import ClassLookupDict
|
|
|
|
|
|
|
|
|
|
|
|
class BaseMetadata(object):
|
|
|
|
def determine_metadata(self, request, view):
|
|
|
|
"""
|
|
|
|
Return a dictionary of metadata about the view.
|
|
|
|
Used to return responses for OPTIONS requests.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError(".determine_metadata() must be overridden.")
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleMetadata(BaseMetadata):
|
|
|
|
"""
|
|
|
|
This is the default metadata implementation.
|
|
|
|
It returns an ad-hoc set of information about the view.
|
|
|
|
There are not any formalized standards for `OPTIONS` responses
|
|
|
|
for us to base this on.
|
|
|
|
"""
|
|
|
|
label_lookup = ClassLookupDict({
|
|
|
|
serializers.Field: 'field',
|
|
|
|
serializers.BooleanField: 'boolean',
|
2015-03-25 04:31:35 +03:00
|
|
|
serializers.NullBooleanField: 'boolean',
|
2014-09-24 17:09:49 +04:00
|
|
|
serializers.CharField: 'string',
|
|
|
|
serializers.URLField: 'url',
|
|
|
|
serializers.EmailField: 'email',
|
|
|
|
serializers.RegexField: 'regex',
|
|
|
|
serializers.SlugField: 'slug',
|
|
|
|
serializers.IntegerField: 'integer',
|
|
|
|
serializers.FloatField: 'float',
|
|
|
|
serializers.DecimalField: 'decimal',
|
|
|
|
serializers.DateField: 'date',
|
|
|
|
serializers.DateTimeField: 'datetime',
|
|
|
|
serializers.TimeField: 'time',
|
|
|
|
serializers.ChoiceField: 'choice',
|
|
|
|
serializers.MultipleChoiceField: 'multiple choice',
|
|
|
|
serializers.FileField: 'file upload',
|
|
|
|
serializers.ImageField: 'image upload',
|
|
|
|
})
|
|
|
|
|
|
|
|
def determine_metadata(self, request, view):
|
2014-11-06 15:00:30 +03:00
|
|
|
metadata = OrderedDict()
|
2014-09-24 17:09:49 +04:00
|
|
|
metadata['name'] = view.get_view_name()
|
|
|
|
metadata['description'] = view.get_view_description()
|
|
|
|
metadata['renders'] = [renderer.media_type for renderer in view.renderer_classes]
|
|
|
|
metadata['parses'] = [parser.media_type for parser in view.parser_classes]
|
|
|
|
if hasattr(view, 'get_serializer'):
|
|
|
|
actions = self.determine_actions(request, view)
|
|
|
|
if actions:
|
|
|
|
metadata['actions'] = actions
|
|
|
|
return metadata
|
|
|
|
|
|
|
|
def determine_actions(self, request, view):
|
|
|
|
"""
|
|
|
|
For generic class based views we return information about
|
|
|
|
the fields that are accepted for 'PUT' and 'POST' methods.
|
|
|
|
"""
|
|
|
|
actions = {}
|
|
|
|
for method in set(['PUT', 'POST']) & set(view.allowed_methods):
|
|
|
|
view.request = clone_request(request, method)
|
|
|
|
try:
|
|
|
|
# Test global permissions
|
|
|
|
if hasattr(view, 'check_permissions'):
|
|
|
|
view.check_permissions(view.request)
|
|
|
|
# Test object permissions
|
|
|
|
if method == 'PUT' and hasattr(view, 'get_object'):
|
|
|
|
view.get_object()
|
|
|
|
except (exceptions.APIException, PermissionDenied, Http404):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# If user has appropriate permissions for the view, include
|
|
|
|
# appropriate metadata about the fields that should be supplied.
|
|
|
|
serializer = view.get_serializer()
|
|
|
|
actions[method] = self.get_serializer_info(serializer)
|
|
|
|
finally:
|
|
|
|
view.request = request
|
|
|
|
|
|
|
|
return actions
|
|
|
|
|
|
|
|
def get_serializer_info(self, serializer):
|
|
|
|
"""
|
|
|
|
Given an instance of a serializer, return a dictionary of metadata
|
|
|
|
about its fields.
|
|
|
|
"""
|
2014-11-07 15:16:25 +03:00
|
|
|
if hasattr(serializer, 'child'):
|
|
|
|
# If this is a `ListSerializer` then we want to examine the
|
|
|
|
# underlying child serializer instance instead.
|
|
|
|
serializer = serializer.child
|
2014-11-06 15:00:30 +03:00
|
|
|
return OrderedDict([
|
2014-09-24 17:09:49 +04:00
|
|
|
(field_name, self.get_field_info(field))
|
2014-09-25 14:04:18 +04:00
|
|
|
for field_name, field in serializer.fields.items()
|
2014-09-24 17:09:49 +04:00
|
|
|
])
|
|
|
|
|
|
|
|
def get_field_info(self, field):
|
|
|
|
"""
|
|
|
|
Given an instance of a serializer field, return a dictionary
|
|
|
|
of metadata about it.
|
|
|
|
"""
|
2014-11-06 15:00:30 +03:00
|
|
|
field_info = OrderedDict()
|
2014-09-24 17:09:49 +04:00
|
|
|
field_info['type'] = self.label_lookup[field]
|
|
|
|
field_info['required'] = getattr(field, 'required', False)
|
|
|
|
|
2015-02-20 18:31:12 +03:00
|
|
|
attrs = [
|
|
|
|
'read_only', 'label', 'help_text',
|
|
|
|
'min_length', 'max_length',
|
|
|
|
'min_value', 'max_value'
|
|
|
|
]
|
|
|
|
|
|
|
|
for attr in attrs:
|
2014-09-24 17:09:49 +04:00
|
|
|
value = getattr(field, attr, None)
|
|
|
|
if value is not None and value != '':
|
|
|
|
field_info[attr] = force_text(value, strings_only=True)
|
|
|
|
|
2015-05-28 22:41:10 +03:00
|
|
|
if not field_info.get('read_only') and hasattr(field, 'choices'):
|
2014-09-24 17:09:49 +04:00
|
|
|
field_info['choices'] = [
|
2014-11-18 18:42:52 +03:00
|
|
|
{
|
|
|
|
'value': choice_value,
|
|
|
|
'display_name': force_text(choice_name, strings_only=True)
|
|
|
|
}
|
2014-09-24 17:09:49 +04:00
|
|
|
for choice_value, choice_name in field.choices.items()
|
|
|
|
]
|
|
|
|
|
|
|
|
return field_info
|