diff options
| author | Tom Christie | 2013-05-24 21:21:56 +0100 |
|---|---|---|
| committer | Tom Christie | 2013-05-24 21:21:56 +0100 |
| commit | 760e8642bd04b5e03409601a8d378799c36eac1b (patch) | |
| tree | 69857f97ba5702fe040167a058fae0431abfe588 /rest_framework/fields.py | |
| parent | 78c53d530ff3d7a4a443b104ad73952d0b5b5b8b (diff) | |
| parent | a1deb5eac7d6d00c6269d88fce1cc6818d8ec04a (diff) | |
| download | django-rest-framework-760e8642bd04b5e03409601a8d378799c36eac1b.tar.bz2 | |
Merge branch 'issue-192-expose-fields-for-options' of https://github.com/grimborg/django-rest-framework into improved-options-support
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index d772c400..cb5f9a40 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -23,7 +23,8 @@ from django.utils.translation import ugettext_lazy as _ from django.utils.datastructures import SortedDict from rest_framework import ISO_8601 -from rest_framework.compat import timezone, parse_date, parse_datetime, parse_time +from rest_framework.compat import (timezone, parse_date, parse_datetime, + parse_time) from rest_framework.compat import BytesIO from rest_framework.compat import six from rest_framework.compat import smart_text, force_text, is_non_str_iterable @@ -61,7 +62,8 @@ def get_component(obj, attr_name): def readable_datetime_formats(formats): - format = ', '.join(formats).replace(ISO_8601, 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]') + format = ', '.join(formats).replace(ISO_8601, + 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]') return humanize_strptime(format) @@ -70,6 +72,18 @@ def readable_date_formats(formats): return humanize_strptime(format) +def humanize_form_fields(form): + """Return a humanized description of all the fields in a form. + + :param form: A Django form. + :return: A dictionary of {field_label: humanized description} + + """ + fields = SortedDict([(name, humanize_field(field)) + for name, field in form.fields.iteritems()]) + return fields + + def readable_time_formats(formats): format = ', '.join(formats).replace(ISO_8601, 'hh:mm[:ss[.uuuuuu]]') return humanize_strptime(format) @@ -193,6 +207,19 @@ class Field(object): return {'type': self.type_name} return {} + @property + def humanized(self): + humanized = { + 'type': self.type_name, + 'required': getattr(self, 'required', False), + } + optional_attrs = ['read_only', 'help_text', 'label', + 'min_length', 'max_length'] + for attr in optional_attrs: + if getattr(self, attr, None) is not None: + humanized[attr] = getattr(self, attr) + return humanized + class WritableField(Field): """ |
