summaryrefslogtreecommitdiff
path: root/main/forms.py
blob: 2343869a6a9c72f781dced38e9acfbc1160be89d (plain)
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
159
160
161
162
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2008  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

"""
Forms
"""
from django import forms
from django.contrib.gis.db import models

from chimere.main.models import Marker, Route, PropertyModel, Property

class MarkerAdminForm(forms.ModelForm):
    """
    Main form for marker
    """
    # declare properties
    for property in PropertyModel.objects.filter(available=True):
        exec('property_%d_%d = forms.CharField(label="%s", widget=forms.%s, \
required=False)' % (property.order, property.id, property.name,
                    PropertyModel.TYPE_WIDGET[property.type]))
    class Meta:
        model = Marker

    def __init__(self, *args, **keys):
        """
        Custom initialization method in order to manage properties
        """
        if 'instance' in keys and keys['instance']:
            instance = keys['instance']
            property_dct = {}
            for pm in PropertyModel.objects.filter(available=True):
                property  = instance.getProperty(pm)
                if property:
                    property_dct[pm.getNamedId()] = property.value
            if 'initial' in keys:
                keys['initial'].update(property_dct)
            else:
                keys['initial'] = property_dct
        super(MarkerAdminForm, self).__init__(*args, **keys)

    def save(self, *args, **keys):
        """
        Custom save method in order to manage associeted properties
        """
        new_marker = super(MarkerAdminForm, self).save(*args, **keys)
        if 'status' not in self.cleaned_data:
            new_marker.status = 'S'
        new_marker.save()
        # save each property
        for propertymodel in PropertyModel.objects.filter(available=True):
            properties = Property.objects.filter(marker=new_marker,
                                                 propertymodel=propertymodel)
            # new property
            if not properties:
                new_property = Property.objects.create(marker=new_marker,
                                    propertymodel=propertymodel,
                                    value=self.cleaned_data['property_%d_%d' % (
                                        propertymodel.order, propertymodel.id)])
                new_property.save()
            else:
                # in case of multiple edition as the same time delete arbitrary
                # the others
                if len(properties) > 1:
                    for property in properties[1:]:
                        property.delete()
                property = properties[0]
                property.value = self.cleaned_data['property_%d_%d' % (
                                         propertymodel.order, propertymodel.id)]
                property.save()
        return new_marker

class MarkerForm(MarkerAdminForm):
    """
    Form for the edit page
    """
    class Meta:
        model = Marker
        exclude = ('status',)

class RouteAdminForm(forms.ModelForm):
    """
    Main form for route
    """
    # declare properties
    for property in PropertyModel.objects.filter(available=True):
        exec('property_%d_%d = forms.CharField(label="%s", widget=forms.%s, \
required=False)' % (property.order, property.id, property.name,
                    PropertyModel.TYPE_WIDGET[property.type]))
    class Meta:
        model = Route

    def __init__(self, *args, **keys):
        """
        Custom initialization method in order to manage properties
        """
        if 'instance' in keys and keys['instance']:
            instance = keys['instance']
            property_dct = {}
            for pm in PropertyModel.objects.filter(available=True):
                property  = instance.getProperty(pm)
                if property:
                    property_dct[pm.getNamedId()] = property.value
            if 'initial' in keys:
                keys['initial'].update(property_dct)
            else:
                keys['initial'] = property_dct
        super(RouteAdminForm, self).__init__(*args, **keys)

    def save(self, *args, **keys):
        """
        Custom save method in order to manage associeted properties
        """
        new_marker = super(RouteAdminForm, self).save(*args, **keys)
        if 'status' not in self.cleaned_data:
            new_marker.status = 'S'
        new_marker.save()
        # save each property
        for propertymodel in PropertyModel.objects.filter(available=True):
            properties = Property.objects.filter(marker=new_marker,
                                                 propertymodel=propertymodel)
            # new property
            if not properties:
                new_property = Property.objects.create(marker=new_marker,
                                    propertymodel=propertymodel,
                                    value=self.cleaned_data['property_%d_%d' % (
                                        propertymodel.order, propertymodel.id)])
                new_property.save()
            else:
                # in case of multiple edition as the same time delete arbitrary
                # the others
                if len(properties) > 1:
                    for property in properties[1:]:
                        property.delete()
                property = properties[0]
                property.value = self.cleaned_data['property_%d_%d' % (
                                         propertymodel.order, propertymodel.id)]
                property.save()
        return new_marker

class RouteForm(RouteAdminForm):
    """
    Form for the edit page
    """
    class Meta:
        model = Route
        exclude = ('status',)