71 lines
2.9 KiB
Python
71 lines
2.9 KiB
Python
from django import forms
|
|
from django.contrib.gis.geos import Point
|
|
from .models import SignPole, SignPanel
|
|
|
|
class SignPoleForm(forms.ModelForm):
|
|
lat = forms.FloatField(widget=forms.HiddenInput(), required=False)
|
|
lon = forms.FloatField(widget=forms.HiddenInput(), required=False)
|
|
|
|
class Meta:
|
|
model = SignPole
|
|
fields = ['model', 'organization', 'orientation']
|
|
widgets = {
|
|
'category': forms.Select(attrs={'class': 'form-select'}),
|
|
'model': forms.Select(attrs={'class': 'form-select'}),
|
|
'organization': forms.Select(attrs={'class': 'form-select'}),
|
|
'orientation': forms.HiddenInput(),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.fields['organization'].required = True
|
|
|
|
def save(self, commit=True):
|
|
instance = super().save(commit=False)
|
|
lat = self.cleaned_data.get('lat')
|
|
lon = self.cleaned_data.get('lon')
|
|
if lat and lon:
|
|
instance.lon = float(lon)
|
|
instance.lat = float(lat)
|
|
instance.geom = Point(float(lon), float(lat), srid=4326)
|
|
instance.geojson = instance.geom.geojson
|
|
instance.geom.transform(3812)
|
|
if commit:
|
|
instance.save()
|
|
return instance
|
|
|
|
class SignPanelForm(forms.ModelForm):
|
|
lat = forms.FloatField(widget=forms.HiddenInput(), required=False)
|
|
lon = forms.FloatField(widget=forms.HiddenInput(), required=False)
|
|
|
|
class Meta:
|
|
model = SignPanel
|
|
fields = ['signpanel_type', 'model', 'organization', 'orientation', 'support_type', 'is_temporary', 'signpanel_text', 'signpanel_value']
|
|
widgets = {
|
|
'signpanel_type': forms.Select(attrs={'class': 'form-select'}),
|
|
'model': forms.Select(attrs={'class': 'form-select'}),
|
|
'organization': forms.Select(attrs={'class': 'form-select'}),
|
|
'orientation': forms.NumberInput(attrs={'class': 'form-control'}),
|
|
'support_type': forms.Select(attrs={'class': 'form-select'}),
|
|
'signpanel_text': forms.TextInput(attrs={'class': 'form-control'}),
|
|
'signpanel_value': forms.NumberInput(attrs={'class': 'form-control'}),
|
|
'is_temporary': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.fields['organization'].required = True
|
|
|
|
def save(self, commit=True):
|
|
instance = super().save(commit=False)
|
|
lat = self.cleaned_data.get('lat')
|
|
lon = self.cleaned_data.get('lon')
|
|
if lat and lon:
|
|
instance.lon = float(lon)
|
|
instance.lat = float(lat)
|
|
instance.geom = Point(float(lon), float(lat), srid=4326)
|
|
instance.geojson = instance.geom.geojson
|
|
instance.geom.transform(3812)
|
|
if commit:
|
|
instance.save()
|
|
return instance
|