-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforms.py
72 lines (56 loc) · 2.59 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
#a tree form widget that
#!/usr/bin/python
# -*- coding: utf-8 -*-
from django import forms
from django.forms import fields
from django.utils.safestring import mark_safe
from django.template.loader import get_template
from django.template.context import Context
from django.forms.widgets import Widget, Select
from simple_locations.models import Area,AreaType
from django.conf import settings
from mptt.forms import TreeNodeChoiceField
from django.forms.util import ErrorList
from code_generator.code_generator import generate_tracking_tag
class LocationForm(forms.Form):
name = forms.CharField(max_length=100)
code = forms.CharField(max_length=50,required=False)
pk = forms.CharField(widget=forms.HiddenInput(), required=False)
target = TreeNodeChoiceField(queryset=Area.tree.all(),
level_indicator=u'+--', required=False)
lat = forms.DecimalField(required=False)
lon = forms.DecimalField(required=False)
kind=forms.ChoiceField(required=False,choices=(('','-----'),)+tuple([(int(w.pk),w.name) for w in AreaType.objects.all() ]))
move_choice = forms.BooleanField(required=False)
position = forms.ChoiceField(choices=(('last-child', 'inside'), ('left', 'before'), ('right', 'after')),
required=False)
def clean(self):
""" make sure that both lat and lon are provided. if lat is given then lon is also required and vice versa. """
lat=self.cleaned_data['lat']
lon=self.cleaned_data['lon']
code=self.cleaned_data['code']
if not lat and lon:
msg=u"Please provide the latitude"
self._errors["lat"]=ErrorList([msg])
return ''
elif lat and not lon:
msg=u"Please provide the longitude"
self._errors["lon"]=ErrorList([msg])
return ''
if lat and lon:
if not -90 <= lat <= 90 :
msg=u'Invalid latitude must be between 90 and -90'
self._errors["lat"]=ErrorList([msg])
return ''
if not -180 <= lon <=180 :
msg=u'Invalid latitude must be between 180 and -180'
self._errors["lon"]=ErrorList([msg])
return ''
if not code:
try:
self.cleaned_data['code']=generate_tracking_tag(Area.objects.order_by('-pk')[0].code)
except:
self.cleaned_data['code']=generate_tracking_tag()
else:
self.cleaned_data['code']='2'+code
return self.cleaned_data