12345678910111213141516171819202122232425262728293031323334353637383940 |
- from django import forms
- from django.contrib.gis.geos import Point
- from .models import Antenna
- class StopAllocationForm(forms.Form):
- pass
- class AntennaForm(forms.ModelForm):
- longitude = forms.FloatField(
- min_value=-180,
- max_value=180,
- required=False,
- )
- latitude = forms.FloatField(
- min_value=-90,
- max_value=90,
- required=False,
- )
- class Meta:
- model = Antenna
- fields = ('label', 'mode', 'ssid', 'mac', 'longitude', 'latitude', 'orientation', 'contact', 'notes', 'position')
- widgets = {'position': forms.HiddenInput}
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- coordinates = self.initial.get('position', self.instance.position)
- if isinstance(coordinates, Point):
- self.initial['longitude'], self.initial['latitude'] = coordinates.tuple
- def clean(self):
- data = super().clean()
- latitude = data.get('latitude')
- longitude = data.get('longitude')
- if latitude and longitude:
- data['position'] = Point(longitude, latitude)
- return data
|