-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathforms.py
115 lines (89 loc) · 3.64 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
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
import logging
from django.contrib.gis import forms
from django.contrib.gis.geos import Point
from inspections.models import Establishment, Inspection, Violation
DATE_FORMATS = ['%Y-%m-%dT%H:%M:%S', # '2006-10-25T14:30:59'
'%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
'%Y-%m-%d %H:%M', # '2006-10-25 14:30'
'%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59'
'%m/%d/%Y %H:%M', # '10/25/2006 14:30'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59'
'%m/%d/%y %H:%M', # '10/25/06 14:30'
'%m/%d/%y', # '10/25/06'
'%d-%b-%Y', # '25-Oct-2006'
'%m/%d/%Y %I:%M:%S %p', # '12/6/2013 12:00:00 AM'
]
logger = logging.getLogger(__name__)
class EstablishmentForm(forms.ModelForm):
"Validate and clean Durham's establishment data"
status = forms.CharField()
lat = forms.FloatField(required=False)
lon = forms.FloatField(required=False)
update_date = forms.DateTimeField(input_formats=DATE_FORMATS)
opening_date = forms.DateTimeField(input_formats=DATE_FORMATS)
class Meta:
model = Establishment
exclude = ('location','property_id')
def clean_city(self):
city = self.cleaned_data['city']
return city.title()
def clean_status(self):
status = self.cleaned_data['status']
if status == 'ACTIVE':
return 'active'
elif status == 'DELETED':
return 'deleted'
raise forms.ValidationError('Invalid status')
def clean_phone(self):
phone = self.cleaned_data['phone']
# Force empty phone value to empty string
if phone == 'NULL':
phone = ''
return phone
def clean(self):
lat = self.cleaned_data.get('lat', None)
lon = self.cleaned_data.get('lon', None)
if lat and lon:
self.cleaned_data['location'] = Point(lon, lat)
return self.cleaned_data
def save(self, commit=True):
instance = super().save(commit=False)
if 'location' in self.cleaned_data:
instance.location = self.cleaned_data['location']
instance.save()
return instance
class InspectionForm(forms.ModelForm):
"Validate and clean Durham's inspection data"
score = forms.FloatField(required=False)
date = forms.DateTimeField(input_formats=DATE_FORMATS)
update_date = forms.DateTimeField(input_formats=DATE_FORMATS)
class Meta:
model = Inspection
fields = "__all__"
def clean_score(self):
# Force empty score value to None so we save to DB as NULL
score = self.cleaned_data['score']
if not score:
score = None
return score
def clean_description(self):
# Force API sent description strings of "NULL" to empty string
description = self.cleaned_data['description']
if description == 'NULL':
description = ''
return description
class ViolationForm(forms.ModelForm):
"Validate and clean Durham's violation data"
date = forms.DateTimeField(input_formats=DATE_FORMATS)
update_date = forms.DateTimeField(input_formats=DATE_FORMATS)
class Meta:
model = Violation
fields = "__all__"
def clean_description(self):
# Force API sent description strings of "0" to empty string
description = self.cleaned_data['description']
if description == '0':
description = ''
return description