-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforms.py
More file actions
66 lines (53 loc) · 2.25 KB
/
forms.py
File metadata and controls
66 lines (53 loc) · 2.25 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
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
import os
import zipfile
from django import forms
from django.forms import widgets
from django.forms.formsets import formset_factory
from webfinches.models import DataFile, DataLayer, UploadEvent
class ZipUploadForm(forms.ModelForm):
"""For uploading .zip files that contain .shp files"""
class Meta:
model = DataFile
fields = ['file']
def clean_file(self):
zip_file = self.cleaned_data['file']
zf = zipfile.ZipFile(zip_file)
contents = zf.namelist()
filetypes = [os.path.splitext(c)[1] for c in contents]
if '.shp' not in filetypes:
raise forms.ValidationError('.zip uploads must contain .shp files')
if '.dbf' not in filetypes:
raise forms.ValidationError('.zip uploads must contain .dbf files')
if '.shx' not in filetypes:
raise forms.ValidationError('.zip uploads must contain .shx files')
return zip_file
def save(self, upload, commit=True):
"""Data Files need a UploadEvent in order to be saved"""
# create a DataFile object
data_file = super(ZipUploadForm, self).save(commit=False)
# attach the UploadEvent
data_file.upload = upload
data_file.save(commit)
return data_file
class LayerReviewForm(forms.ModelForm):
"""For editing and configuring the layer information for each layer."""
data_file_id = forms.IntegerField(widget=forms.HiddenInput())
class Meta:
model = DataLayer
fields = ['name', 'notes', 'geometry_type', 'srs', 'tags', 'data_file_id','pathy']
class LayerBrowseForm(forms.ModelForm):
"""For browsing and editing layers generally"""
#tags = forms.CharField()
class Meta:
model = DataLayer
fields = ['name', 'notes', 'srs','tags']
class SiteConfigurationForm(forms.ModelForm):
"""For browsing and editing layers generally"""
radius = forms.IntegerField()
class Meta:
model = DataLayer
fields = ['name', 'srs','notes','geometry_type', 'tags']
ZipFormSet = formset_factory(ZipUploadForm, extra=3)
LayerReviewFormSet = formset_factory(LayerReviewForm, extra=0)
LayerBrowseFormSet = formset_factory(LayerBrowseForm, extra=0)
SiteConfigurationFormSet = formset_factory(SiteConfigurationForm, extra=0)