summaryrefslogtreecommitdiff
path: root/main/forms.py
diff options
context:
space:
mode:
Diffstat (limited to 'main/forms.py')
-rw-r--r--main/forms.py37
1 files changed, 36 insertions, 1 deletions
diff --git a/main/forms.py b/main/forms.py
index 2343869..6c03c7d 100644
--- a/main/forms.py
+++ b/main/forms.py
@@ -22,8 +22,10 @@ Forms
"""
from django import forms
from django.contrib.gis.db import models
+from django.utils.translation import ugettext as _
-from chimere.main.models import Marker, Route, PropertyModel, Property
+from chimere.main.models import Marker, Route, PropertyModel, Property, Area
+from chimere.main.widgets import AreaField, PointField
class MarkerAdminForm(forms.ModelForm):
"""
@@ -160,3 +162,36 @@ class RouteForm(RouteAdminForm):
class Meta:
model = Route
exclude = ('status',)
+
+class AreaAdminForm(forms.ModelForm):
+ """
+ Admin page to create an area
+ """
+ area = AreaField(label=_("Area"), fields=(PointField(), PointField()))
+ class Meta:
+ model = Area
+
+ def __init__(self, *args, **keys):
+ """
+ Custom initialization method in order to manage area
+ """
+ if 'instance' in keys and keys['instance']:
+ instance = keys['instance']
+ dct = {'area':(instance.upper_left_corner,
+ instance.lower_right_corner)}
+ if 'initial' in keys:
+ keys['initial'].update(dct)
+ else:
+ keys['initial'] = dct
+ super(AreaAdminForm, self).__init__(*args, **keys)
+
+ def save(self, *args, **keys):
+ """
+ Custom save method in order to manage area
+ """
+ new_area = super(AreaAdminForm, self).save(*args, **keys)
+ area = self.cleaned_data['area']
+ new_area.upper_left_corner = 'POINT(%s %s)' % (area[0][0], area[0][1])
+ new_area.lower_right_corner = 'POINT(%s %s)' % (area[1][0],
+ area[1][1])
+ return new_area