forked from django-import-export/django-import-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
158 lines (115 loc) · 3.6 KB
/
widgets.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from decimal import Decimal
from datetime import datetime
class Widget(object):
"""
Widget takes care of converting between import and export representations.
Widget objects have two functions:
* converts object field value to export representation
* converts import value and converts it to appropriate python
representation
"""
def clean(self, value):
"""
Returns appropriate python objects for import value.
"""
return value
def render(self, value):
"""
Returns export representation of python value.
"""
return unicode(value)
class IntegerWidget(Widget):
"""
Widget for converting integer fields.
"""
def clean(self, value):
if not value:
return None
return int(value)
class DecimalWidget(Widget):
"""
Widget for converting decimal fields.
"""
def clean(self, value):
if not value:
return None
return Decimal(value)
class CharWidget(Widget):
"""
Widget for converting text fields.
"""
def render(self, value):
return unicode(value)
class BooleanWidget(Widget):
"""
Widget for converting boolean fields.
"""
TRUE_VALUES = ["1", 1]
FALSE_VALUE = "0"
def render(self, value):
return self.TRUE_VALUES[0] if value else self.FALSE_VALUE
def clean(self, value):
return True if value in self.TRUE_VALUES else False
class DateWidget(Widget):
"""
Widget for converting date fields.
Takes optional ``format`` parameter.
"""
def __init__(self, format=None):
if format is None:
format = "%Y-%m-%d"
self.format = format
def clean(self, value):
if not value:
return None
return datetime.strptime(value, self.format).date()
def render(self, value):
return value.strftime(self.format)
class DateTimeWidget(Widget):
"""
Widget for converting date fields.
Takes optional ``format`` parameter.
"""
def __init__(self, format=None):
if format is None:
format = "%Y-%m-%d %H:%M:%S"
self.format = format
def clean(self, value):
if not value:
return None
return datetime.strptime(value, self.format)
def render(self, value):
return value.strftime(self.format)
class ForeignKeyWidget(Widget):
"""
Widget for ``ForeignKey`` model field that represent ForeignKey as
integer value.
Requires a positional argument: the class to which the field is related.
"""
def __init__(self, model, *args, **kwargs):
self.model = model
super(ForeignKeyWidget, self).__init__(*args, **kwargs)
def clean(self, value):
pk = super(ForeignKeyWidget, self).clean(value)
return self.model.objects.get(pk=pk) if pk else None
def render(self, value):
if value is None:
return ""
return value.pk
class ManyToManyWidget(Widget):
"""
Widget for ``ManyToManyField`` model field that represent m2m field
as comma separated pk values.
Requires a positional argument: the class to which the field is related.
"""
def __init__(self, model, *args, **kwargs):
self.model = model
super(ManyToManyWidget, self).__init__(*args, **kwargs)
def clean(self, value):
if not value:
return self.model.objects.none()
ids = value.split(",")
return self.model.objects.filter(pk__in=ids)
def render(self, value):
ids = [str(obj.pk) for obj in value.all()]
return ",".join(ids)