-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathforms.py
More file actions
41 lines (35 loc) · 1.78 KB
/
Copy pathforms.py
File metadata and controls
41 lines (35 loc) · 1.78 KB
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
from django.contrib.gis import forms
from django.utils import timezone
from leaflet.forms.widgets import LeafletWidget
class BaseSearchForm(forms.Form):
""" Basic version of form for basic seaching of django-geospaas metadata """
polygon = forms.GeometryField(label=False,
widget=LeafletWidget(attrs={
'settings_overrides': {
'DEFAULT_CENTER': (89.0, 179.0),
'DEFAULT_ZOOM': 1,
'NO_GLOBALS': False,
'PLUGINS': {'forms': {'auto-include': True}},
}
}),
required=False)
time_coverage_start = forms.DateTimeField(
initial=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc))
time_coverage_end = forms.DateTimeField(initial=timezone.now())
def filter(self, ds):
""" Filtering method of the form. All filtering processes are coded here """
# filtering based on time
t_0 = self.cleaned_data['time_coverage_start']
t_1 = self.cleaned_data['time_coverage_end']
# Too early datasets are excluded from the filtering results
ds = ds.exclude(time_coverage_end__lte=t_0)
# Too late datasets are excluded from the filtering results
ds = ds.exclude(time_coverage_start__gt=t_1)
# spatial filtering
if self.cleaned_data['polygon']:
# filtering by user provided polygon
ds = ds.filter(
geographic_location__geometry__intersects=self.cleaned_data['polygon'])
# sorting
ds = ds.order_by('time_coverage_start')
return ds