forms.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django import forms
  2. from django.contrib.gis.geos import Point
  3. from .models import Antenna
  4. class StopAllocationForm(forms.Form):
  5. pass
  6. class AntennaForm(forms.ModelForm):
  7. longitude = forms.FloatField(
  8. min_value=-180,
  9. max_value=180,
  10. required=False,
  11. )
  12. latitude = forms.FloatField(
  13. min_value=-90,
  14. max_value=90,
  15. required=False,
  16. )
  17. class Meta:
  18. model = Antenna
  19. fields = ('label', 'mode', 'ssid', 'mac', 'longitude', 'latitude', 'orientation', 'contact', 'notes', 'position')
  20. widgets = {'position': forms.HiddenInput}
  21. def __init__(self, *args, **kwargs):
  22. super().__init__(*args, **kwargs)
  23. coordinates = self.initial.get('position', self.instance.position)
  24. if isinstance(coordinates, Point):
  25. self.initial['longitude'], self.initial['latitude'] = coordinates.tuple
  26. def clean(self):
  27. data = super().clean()
  28. latitude = data.get('latitude')
  29. longitude = data.get('longitude')
  30. if latitude and longitude:
  31. data['position'] = Point(longitude, latitude)
  32. return data