combo/combo/apps/maps/forms.py

98 lines
3.7 KiB
Python

# combo - content management system
# Copyright (C) 2017 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import forms
from django.utils.encoding import force_text
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
from combo.data.fields import TemplatableURLField
from .models import MapLayer, MapLayerOptions
class IconRadioSelect(forms.RadioSelect):
option_template_name = 'maps/icon_radio_option.html'
class MapLayerForm(forms.ModelForm):
geojson_url = TemplatableURLField(label=_('Geojson URL'))
class Meta:
model = MapLayer
exclude = ('kind',)
widgets = {
'marker_colour': forms.TextInput(attrs={'type': 'color'}),
'icon_colour': forms.TextInput(attrs={'type': 'color'}),
}
def __init__(self, *args, **kwargs):
super(MapLayerForm, self).__init__(*args, **kwargs)
if self.instance.pk is None:
# new instance, delete some fields
del self.fields['slug']
del self.fields['cache_duration']
del self.fields['include_user_identifier']
else:
# new widget for icon field
self.fields['icon'].widget = IconRadioSelect()
self.fields['icon'].choices = list(
sorted(self.fields['icon'].choices, key=lambda x: slugify(force_text(x[1]))))
if self.instance.kind == 'geojson':
todelete_fields = ['tiles_template_url', 'tiles_attribution', 'tiles_default']
else:
todelete_fields = [
'geojson_url', 'marker_colour', 'icon', 'icon_colour', 'cache_duration',
'include_user_identifier', 'properties']
for field in todelete_fields:
if field in self.fields:
del self.fields[field]
def clean(self):
cleaned_data = super(MapLayerForm, self).clean()
if self.instance.kind == 'tiles':
if MapLayer.objects.filter(kind='tiles', tiles_default=True).exclude(pk=self.instance.pk):
raise forms.ValidationError(_('Only one default tiles layer can be defined.'))
return cleaned_data
class MapLayerOptionsForm(forms.ModelForm):
class Meta:
model = MapLayerOptions
fields = ['map_layer', 'opacity']
widgets = {
'opacity': forms.NumberInput(attrs={'step': 0.1, 'min': 0, 'max': 1})
}
def __init__(self, *args, **kwargs):
self.kind = kwargs.pop('kind')
super(MapLayerOptionsForm, self).__init__(*args, **kwargs)
# if edition, no possibility to change the layer
if self.instance.pk:
del self.fields['map_layer']
else:
if self.kind == 'geojson':
self.fields['map_layer'].queryset = self.instance.map_cell.get_free_geojson_layers()
else:
self.fields['map_layer'].queryset = self.instance.map_cell.get_free_tiles_layers()
# init opacity field only for tiles layers
if self.kind == 'geojson':
del self.fields['opacity']
else:
self.fields['opacity'].required = True
self.fields['opacity'].initial = 1