forked from praekelt/django-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
76 lines (67 loc) · 3.06 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import inspect
from django import forms
from django.core import serializers
from export import fields
class Export(forms.Form):
export_format = forms.ChoiceField(
choices=[(format, format) for format in \
serializers.get_serializer_formats()],
required=False,
label='Format',
help_text='Designates export format.',
)
export_fields = forms.MultipleChoiceField(
choices=[('asc', 'Ascending'), ('desc', 'Descending')],
required=False,
label='Fields',
help_text="Fields to be included in the exported data. If none are \
selected all fields will be exported. Hold down 'Control', \
or 'Command' on a Mac, to select more than one.",
)
export_order_by = forms.ChoiceField(
required=False,
label='Order by',
help_text='Field to use for export ordering.',
)
export_order_direction = forms.ChoiceField(
choices=[('asc', 'Ascending'), ('dsc', 'Descending')],
required=False,
label='Order direction',
help_text='Sort elements in ascending or descending order.',
)
def __init__(self, model, *args, **kwargs):
super(Export, self).__init__(*args, **kwargs)
self.fieldsets = (('Options', {'fields': ('export_format', \
'export_fields', 'export_order_by', \
'export_order_direction')}), ('Filters', \
{'description': 'Objects will be filtered to match the \
criteria as specified in the fields below. If a value \
is not specified for a field the field is ignored during \
the filter process.', 'fields': []}))
field_choices = []
form_fields = forms.models.fields_for_model(model)
for field in model._meta.fields:
name = field.name
if name not in form_fields.keys():
continue
form_field = form_fields[name]
if form_field.__class__ in [forms.models.ModelChoiceField, \
forms.models.ModelMultipleChoiceField]:
self.fields[name] = getattr(fields, field.__class__.\
__name__)(form_field, form_field.queryset)
else:
try:
self.fields[name] = getattr(fields, \
field.__class__.__name__)(form_field)
except AttributeError:
for parent_field in inspect.getmro(field.__class__):
if parent_field.__module__ == \
'django.db.models.fields':
self.fields[name] = getattr(\
fields, parent_field.__name__)(form_field)
break
if name not in self.fieldsets[1][1]['fields']:
self.fieldsets[1][1]['fields'].append(name)
field_choices.append([name, form_field.label.capitalize()])
self.fields['export_fields'].choices = field_choices
self.fields['export_order_by'].choices = field_choices