This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #209 from Amsterdam/feature/json-input
Feature/json input
- Loading branch information
Showing
5 changed files
with
210 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from django import forms | ||
from django.contrib.gis.geos import LineString, Polygon | ||
|
||
from peoplemeasurement.models import Area, Sensors | ||
|
||
|
||
class AreaForm(forms.ModelForm): | ||
json_input = forms.JSONField(required=False) | ||
|
||
def clean(self): | ||
cleaned_data = super().clean() | ||
|
||
# TODO: Use a serializer to do this properly | ||
# TODO: Also raise a ValidationError if it's a new object and no json is inserted (this is not possible, since the area points can only be inserted using the json). | ||
json_input = cleaned_data.get('json_input') | ||
if json_input: | ||
# Check keys | ||
for k in ('sensor', 'areas'): | ||
if not json_input.get(k): | ||
raise forms.ValidationError(f"{k} missing in json") | ||
for k in ('area_id', 'area', 'points'): | ||
if not json_input['areas'].get('area_id'): | ||
raise forms.ValidationError(f"{k} missing in json") | ||
|
||
# Check if sensor exists | ||
# TODO: move this to the model | ||
if Sensors.objects.filter(objectnummer=json_input['sensor']).count() == 0: | ||
raise forms.ValidationError(f"Sensor with objectnummer '{json_input['sensor']}' does not exist.") | ||
|
||
# Check points | ||
points = json_input['areas']['points'] | ||
if len(points) <= 3: # The last point should be the same as the first point, and we need at least a triangle to have an area | ||
raise forms.ValidationError("Not enough points") | ||
if points[0] != points[-1]: | ||
raise forms.ValidationError("The geom points in the json do not form a closed loop.") | ||
|
||
return cleaned_data | ||
|
||
def save(self, commit=True): | ||
instance = super().save(commit=commit) | ||
json_input = self.cleaned_data.get('json_input', None) | ||
if json_input and json_input != 'null': | ||
# There is json input, so we overwrite all fields with the info from the json | ||
sensor_objectnummer = json_input['sensor'] | ||
instance.sensor = Sensors.objects.filter(objectnummer=sensor_objectnummer)[0] | ||
instance.name = json_input['areas']['area_id'] | ||
instance.area = json_input['areas']['area'] | ||
geom_points = json_input['areas']['points'] | ||
instance.geom = Polygon([(coordinate['longitude'], coordinate['latitude']) for coordinate in geom_points]) | ||
if commit: | ||
instance.save() | ||
|
||
return instance | ||
|
||
class Meta: | ||
model = Area | ||
fields = '__all__' | ||
|
||
|
||
class LineForm(forms.ModelForm): | ||
json_input = forms.JSONField(required=False) | ||
|
||
def clean(self): | ||
cleaned_data = super().clean() | ||
|
||
# TODO: Use a serializer to do this properly | ||
# TODO: Also raise a ValidationError if it's a new object and no json is inserted (this is not possible, since the area points can only be inserted using the json). | ||
json_input = cleaned_data.get('json_input') | ||
if json_input: | ||
# Check keys | ||
for k in ('sensor', 'lines'): | ||
if not json_input.get(k): | ||
raise forms.ValidationError(f"{k} missing in json") | ||
for k in ('line_id', 'azimuth', 'points'): | ||
if not json_input['lines'].get(k): | ||
raise forms.ValidationError(f"{k} missing in json") | ||
|
||
# Check if sensor exists | ||
# TODO: move this to the model | ||
if Sensors.objects.filter(objectnummer=json_input['sensor']).count() == 0: | ||
raise forms.ValidationError(f"Sensor with objectnummer '{json_input['sensor']}' does not exist.") | ||
|
||
# Check points | ||
points = json_input['lines']['points'] | ||
if len(points) < 2: | ||
raise forms.ValidationError("We need at least two points") | ||
|
||
return cleaned_data | ||
|
||
def save(self, commit=True): | ||
instance = super().save(commit=commit) | ||
json_input = self.cleaned_data.get('json_input', None) | ||
if json_input: | ||
# There is json input, so we overwrite all fields with the info from the json | ||
sensor_objectnummer = json_input['sensor'] | ||
instance.sensor = Sensors.objects.filter(objectnummer=sensor_objectnummer)[0] | ||
instance.name = json_input['lines']['line_id'] | ||
instance.azimuth = json_input['lines']['azimuth'] | ||
geom_points = json_input['lines']['points'] | ||
instance.geom = LineString([(coordinate['longitude'], coordinate['latitude']) for coordinate in geom_points]) | ||
if commit: | ||
instance.save() | ||
|
||
return instance | ||
|
||
class Meta: | ||
model = Area | ||
fields = '__all__' |
35 changes: 35 additions & 0 deletions
35
src/peoplemeasurement/migrations/0031_auto_20220713_1846.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Generated by Django 3.2.12 on 2022-07-13 16:46 | ||
|
||
import django.contrib.gis.db.models.fields | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('peoplemeasurement', '0030_auto_20220616_1228'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='area', | ||
name='area', | ||
field=models.IntegerField(blank=True), | ||
), | ||
migrations.AlterField( | ||
model_name='area', | ||
name='geom', | ||
field=django.contrib.gis.db.models.fields.PolygonField(blank=True, srid=4326), | ||
), | ||
migrations.AlterField( | ||
model_name='area', | ||
name='name', | ||
field=models.CharField(blank=True, max_length=255, unique=True), | ||
), | ||
migrations.AlterField( | ||
model_name='area', | ||
name='sensor', | ||
field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, related_name='areas', to='peoplemeasurement.sensors'), | ||
), | ||
] |
35 changes: 35 additions & 0 deletions
35
src/peoplemeasurement/migrations/0032_auto_20220713_2146.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Generated by Django 3.2.12 on 2022-07-13 19:46 | ||
|
||
import django.contrib.gis.db.models.fields | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('peoplemeasurement', '0031_auto_20220713_1846'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='line', | ||
name='azimuth', | ||
field=models.FloatField(blank=True), | ||
), | ||
migrations.AlterField( | ||
model_name='line', | ||
name='geom', | ||
field=django.contrib.gis.db.models.fields.LineStringField(blank=True, srid=4326), | ||
), | ||
migrations.AlterField( | ||
model_name='line', | ||
name='name', | ||
field=models.CharField(blank=True, max_length=255, unique=True), | ||
), | ||
migrations.AlterField( | ||
model_name='line', | ||
name='sensor', | ||
field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, related_name='lines', to='peoplemeasurement.sensors'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters