23
23
from django .utils .datastructures import SortedDict
24
24
25
25
from rest_framework import ISO_8601
26
- from rest_framework .compat import timezone , parse_date , parse_datetime , parse_time
26
+ from rest_framework .compat import (timezone , parse_date , parse_datetime ,
27
+ parse_time )
27
28
from rest_framework .compat import BytesIO
28
29
from rest_framework .compat import six
29
30
from rest_framework .compat import smart_text
30
31
from rest_framework .settings import api_settings
31
32
32
33
34
+ HUMANIZED_FIELD_TYPES = {
35
+ 'BooleanField' : u'Boolean' ,
36
+ 'CharField' : u'Single Character' ,
37
+ 'ChoiceField' : u'Single Choice' ,
38
+ 'ComboField' : u'Single Choice' ,
39
+ 'DateField' : u'Date' ,
40
+ 'DateTimeField' : u'Date and Time' ,
41
+ 'DecimalField' : u'Decimal' ,
42
+ 'EmailField' : u'Email' ,
43
+ 'Field' : u'Field' ,
44
+ 'FileField' : u'File' ,
45
+ 'FilePathField' : u'File Path' ,
46
+ 'FloatField' : u'Float' ,
47
+ 'GenericIPAddressField' : u'Generic IP Address' ,
48
+ 'IPAddressField' : u'IP Address' ,
49
+ 'ImageField' : u'Image' ,
50
+ 'IntegerField' : u'Integer' ,
51
+ 'MultiValueField' : u'Multiple Value' ,
52
+ 'MultipleChoiceField' : u'Multiple Choice' ,
53
+ 'NullBooleanField' : u'Nullable Boolean' ,
54
+ 'RegexField' : u'Regular Expression' ,
55
+ 'SlugField' : u'Slug' ,
56
+ 'SplitDateTimeField' : u'Split Date and Time' ,
57
+ 'TimeField' : u'Time' ,
58
+ 'TypedChoiceField' : u'Typed Single Choice' ,
59
+ 'TypedMultipleChoiceField' : u'Typed Multiple Choice' ,
60
+ 'URLField' : u'URL' ,
61
+ }
62
+
63
+
33
64
def is_simple_callable (obj ):
34
65
"""
35
66
True if the object is a callable that takes no arguments.
@@ -62,7 +93,8 @@ def get_component(obj, attr_name):
62
93
63
94
64
95
def readable_datetime_formats (formats ):
65
- format = ', ' .join (formats ).replace (ISO_8601 , 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]' )
96
+ format = ', ' .join (formats ).replace (ISO_8601 ,
97
+ 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]' )
66
98
return humanize_strptime (format )
67
99
68
100
@@ -71,6 +103,61 @@ def readable_date_formats(formats):
71
103
return humanize_strptime (format )
72
104
73
105
106
+ def humanize_field_type (field_type ):
107
+ """Return a human-readable name for a field type.
108
+
109
+ :param field_type: Either a field type class (for example
110
+ django.forms.fields.DateTimeField), or the name of a field type
111
+ (for example "DateTimeField").
112
+
113
+ :return: unicode
114
+
115
+ """
116
+ if isinstance (field_type , basestring ):
117
+ field_type_name = field_type
118
+ else :
119
+ field_type_name = field_type .__name__
120
+ try :
121
+ return HUMANIZED_FIELD_TYPES [field_type_name ]
122
+ except KeyError :
123
+ humanized = re .sub ('([a-z0-9])([A-Z])' , r'\1 \2' , field_type_name )
124
+ return humanized .capitalize ()
125
+
126
+
127
+ def humanize_field (field ):
128
+ """Return a human-readable description of a field.
129
+
130
+ :param field: A Django field.
131
+
132
+ :return: A dictionary of the form {type: type name, required: bool,
133
+ label: field label: read_only: bool,
134
+ help_text: optional help text}
135
+
136
+ """
137
+ humanized = {
138
+ 'type' : humanize_field_type (field .__class__ ),
139
+ 'required' : getattr (field , 'required' , False ),
140
+ 'label' : field .label ,
141
+ }
142
+ optional_attrs = ['read_only' , 'help_text' ]
143
+ for attr in optional_attrs :
144
+ if hasattr (field , attr ):
145
+ humanized [attr ] = getattr (field , attr )
146
+ return humanized
147
+
148
+
149
+ def humanize_form_fields (form ):
150
+ """Return a humanized description of all the fields in a form.
151
+
152
+ :param form: A Django form.
153
+ :return: A dictionary of {field_label: humanized description}
154
+
155
+ """
156
+ fields = SortedDict ([(name , humanize_field (field ))
157
+ for name , field in form .fields .iteritems ()])
158
+ return fields
159
+
160
+
74
161
def readable_time_formats (formats ):
75
162
format = ', ' .join (formats ).replace (ISO_8601 , 'hh:mm[:ss[.uuuuuu]]' )
76
163
return humanize_strptime (format )
0 commit comments