fixed tests, added docs, renamed helper method

This commit is contained in:
Oscar Vilaplana 2013-05-19 15:45:33 +02:00
parent 6bbedfd7ae
commit 7a5cd090aa
3 changed files with 22 additions and 15 deletions

View File

@ -15,8 +15,8 @@ from django.test import TestCase
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from rest_framework import serializers from rest_framework import serializers
from rest_framework.fields import (humanize_field, humanize_field_type, humanize_form_fields, from rest_framework.fields import (humanize_field, humanize_field_type,
Field) humanize_form_fields, Field)
from rest_framework.serializers import Serializer from rest_framework.serializers import Serializer
from rest_framework.tests.models import RESTFrameworkModel from rest_framework.tests.models import RESTFrameworkModel
@ -882,10 +882,14 @@ class HumanizedSerializer(TestCase):
def test_humanized(self): def test_humanized(self):
humanized = humanize_form_fields(Form()) humanized = humanize_form_fields(Form())
self.assertEqual(humanized, { expected = {
'field1': { 'field1': {u'help_text': u'',
u'help_text': u'', u'required': True, u'label': u'field one',
u'type': u'String', u'label': 'field one'}, u'max_length': 3,
'field2': { u'required': True,
u'help_text': u'', u'required': True, u'type': u'String'},
u'type': u'String', u'label': 'field two'}}) 'field2': {u'help_text': u'',
u'label': u'field two',
u'required': True,
u'type': u'String'}}
self.assertEqual(humanized, expected)

View File

@ -131,6 +131,7 @@ class TestRootView(TestCase):
# TODO add help_text and label when they are available # TODO add help_text and label when they are available
#'help_text': '', #'help_text': '',
#'label': None, #'label': None,
'max_length': 100,
'read_only': False, 'read_only': False,
'required': True, 'required': True,
'type': 'String', 'type': 'String',
@ -272,6 +273,7 @@ class TestInstanceView(TestCase):
# available # available
#'description': '', #'description': '',
#'label': None, #'label': None,
'max_length': 100,
'read_only': False, 'read_only': False,
'required': True, 'required': True,
'type': 'String', 'type': 'String',

View File

@ -61,15 +61,16 @@ class APIView(View):
'renders': [renderer.media_type for renderer in self.renderer_classes], 'renders': [renderer.media_type for renderer in self.renderer_classes],
'parses': [parser.media_type for parser in self.parser_classes], 'parses': [parser.media_type for parser in self.parser_classes],
} }
action_metadata = self._generate_action_metadata(request) content['actions'] = self.action_metadata(request)
if action_metadata is not None:
content['actions'] = action_metadata
return content return content
def _generate_action_metadata(self, request): def action_metadata(self, request):
""" """Return a dictionary with the fields required fo reach allowed method. If no method is allowed,
Helper for generating the fields metadata for allowed and permitted methods. return an empty dictionary.
:param request: Request for which to return the metadata of the allowed methods.
:return: A dictionary of the form {method: {field: {field attribute: value}}}
""" """
actions = {} actions = {}
for method in self.allowed_methods: for method in self.allowed_methods: