-
Notifications
You must be signed in to change notification settings - Fork 16
/
forms.py
179 lines (155 loc) · 5.22 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# coding=utf-8
import logging
from django import forms
from django.forms import models
from django.utils.translation import ugettext as _
from geonode.layers.models import Layer
from geonode.people.models import Profile
from geosafe.models import Analysis
__author__ = 'ismailsunni'
LOGGER = logging.getLogger(__name__)
class AnalysisCreationForm(models.ModelForm):
"""A form for creating an event."""
class Meta:
model = Analysis
fields = (
'user_title',
'user_extent',
'exposure_layer',
'hazard_layer',
'aggregation_layer',
'aggregation_filter',
'language_code',
'extent_option',
'keep',
)
user_title = forms.CharField(
label='Analysis Title',
required=False,
widget=forms.TextInput(
attrs={'placeholder': _('Default title generated')})
)
user_extent = forms.CharField(
label='Analysis Extent',
required=False,
)
exposure_layer = forms.ModelChoiceField(
label='Exposure Layer',
required=True,
queryset=Layer.objects.filter(
inasafe_metadata__layer_purpose='exposure'),
widget=forms.Select(
attrs={'class': 'form-control'})
)
hazard_layer = forms.ModelChoiceField(
label='Hazard Layer',
required=True,
queryset=Layer.objects.filter(
inasafe_metadata__layer_purpose='hazard'),
widget=forms.Select(
attrs={'class': 'form-control'})
)
aggregation_layer = forms.ModelChoiceField(
label='Aggregation Layer',
required=False,
queryset=Layer.objects.filter(
inasafe_metadata__layer_purpose='aggregation'),
widget=forms.Select(
attrs={'class': 'form-control'})
)
aggregation_filter = forms.CharField(
required=False,
widget=forms.HiddenInput()
)
# Filter format:
# This field should contain JSON object with the following format:
# {
# 'property_name': <the name of the property to filter>
# 'values': <list of values for this property>
# }
#
# Example:
#
# {
# 'property_name': 'area_name',
# 'values': ['area 1', 'area 2', 'area 3']
# }
language_code = forms.CharField(
required=False,
widget=forms.HiddenInput()
)
keep = forms.BooleanField(
label='Save Analysis',
required=False,
)
@staticmethod
def exposure_choice_queryset():
return Layer.objects.filter(
inasafe_metadata__layer_purpose='exposure')
@staticmethod
def hazard_choice_queryset():
return Layer.objects.filter(
inasafe_metadata__layer_purpose='hazard')
@staticmethod
def aggregation_choice_queryset():
return Layer.objects.filter(
inasafe_metadata__layer_purpose='aggregation')
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
self.language_code = kwargs.pop('language_code', None)
exposure_layer = kwargs.pop('exposure_layer', None)
hazard_layer = kwargs.pop('hazard_layer', None)
aggregation_layer = kwargs.pop('aggregation_layer', None)
super(AnalysisCreationForm, self).__init__(*args, **kwargs)
if exposure_layer:
self.fields['exposure_layer'].queryset = exposure_layer
else:
self.fields['exposure_layer'].queryset = \
self.exposure_choice_queryset()
if hazard_layer:
self.fields['hazard_layer'].queryset = hazard_layer
else:
self.fields['hazard_layer'].queryset = \
self.hazard_choice_queryset()
if aggregation_layer:
self.fields['aggregation_layer'].queryset = aggregation_layer
else:
self.fields['aggregation_layer'].queryset = \
self.aggregation_choice_queryset()
def save(self, commit=True):
instance = super(AnalysisCreationForm, self).save(commit=False)
if self.language_code:
instance.language_code = self.language_code
if self.user.username:
instance.user = self.user
else:
instance.user = Profile.objects.get(username='AnonymousUser')
# validate aggregation filter
if instance.aggregation_layer:
if not instance.aggregation_filter:
# Standardize to empty value
instance.aggregation_filter = None
instance.save()
return instance
class MetaSearchForm(forms.Form):
class Meta:
fields = (
'csw_url',
'keywords',
'user',
'password',
)
csw_url = forms.CharField(
label='CSW URL',
help_text='URL to CSW endpoint',
required=True)
keywords = forms.CharField(
help_text='Keywords to include in the search',
required=False)
user = forms.CharField(
help_text='User to connect to CSW Endpoint',
required=False)
password = forms.CharField(
help_text='Password to connect to CSW Endpoint',
required=False,
widget=forms.PasswordInput(render_value=True))