Skip to content

Commit

Permalink
apps/bplan: revert changes for the point field back to geojson
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk committed Dec 19, 2024
1 parent 16b7c59 commit 6081050
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 51 deletions.
20 changes: 14 additions & 6 deletions docs/bplan_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ The following fields need to be provided:
- End date of the participation in
- [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601)
(if no time zone is defined, german time zones UTC+01 and UTC+02 are used)
- *point*: string
- string containing coordinates separated by a comma, e.g. "1492195.544958444,6895923.461738203"
- *point*: geojson
- Location of the bplan
- Projection: WGS84 / EPSG:3857
- Projection: WGS84 / EPSG:4326
- *(diplan only) tile_image*: string
- Base64 encoded image
- Minimal resolution 500x300 px (width x height)
Expand Down Expand Up @@ -133,7 +132,16 @@ The following fields need to be provided:
"is_draft": false,
"start_date": "2017-01-01T00:00",
"end_date": "2018-01-01T00:00",
"point": "1492195.544958444,6895923.461738203",
"point": {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
13.411924777644563,
52.499598134440944
]
}
},
"image_url": "http://berlin.de/images/.../bebauungsplan.649020.png",
"image_copyright": "BA Marzahn-Hellersdorf"
}
Expand Down Expand Up @@ -195,7 +203,7 @@ curl \
"office_worker_email": "test@example.com",
"start_date": "2019-01-01T00:00",
"end_date": "2022-01-01T00:00",
"point": "1492195.544958444,6895923.461738203"
"point": {"type": "Feature","geometry": {"type": "Point", "coordinates":[13.411924777644563,52.499598134440944]}}
}
'
```
Expand All @@ -216,7 +224,7 @@ curl -X POST http://127.0.0.1:8003/api/organisations/1/bplan/ \
"office_worker_email": "test@example.com",
"start_date": "2019-01-01T00:00",
"end_date": "2022-01-01T00:00",
"point": "1492195.544958444,6895923.461738203"
"point": {"type": "Feature","geometry": {"type": "Point", "coordinates":[13.411924777644563,52.499598134440944]}}
}
'
```
Expand Down
33 changes: 3 additions & 30 deletions meinberlin/apps/bplan/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext as _
from pyproj import Transformer
from rest_framework import serializers

from adhocracy4.dashboard import components
Expand Down Expand Up @@ -72,6 +71,9 @@ class BplanSerializer(serializers.ModelSerializer):
required=False,
write_only=True,
)
# overwrite the point model field so it's expecting json, the original field is validated as a string and therefore
# doesn't pass validation when not receiving a string
point = serializers.JSONField(required=False, write_only=True)

class Meta:
model = Bplan
Expand Down Expand Up @@ -103,7 +105,6 @@ class Meta:
"url": {"write_only": True},
"office_worker_email": {"write_only": True},
"identifier": {"write_only": True},
"point": {"write_only": True, "required": False},
}

def to_representation(self, instance):
Expand All @@ -130,20 +131,6 @@ def create(self, validated_data):
bplan_id = validated_data.pop("bplan_id")
validated_data["identifier"] = bplan_id

# We receive the point as a string containing coordinates in epsg3875 but internally
# use epsg4326 so we need to convert them and save them as valid geojson
if "point" in validated_data:
point = validated_data["point"].split(",")
transformer = Transformer.from_crs("EPSG:3857", "EPSG:4326")
new_point = transformer.transform(point[0].strip(), point[1].strip())
validated_data["point"] = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [new_point[1], new_point[0]],
},
}

start_date = validated_data["start_date"]
end_date = validated_data["end_date"]

Expand Down Expand Up @@ -198,20 +185,6 @@ def update(self, instance, validated_data):
bplan_id = validated_data.pop("bplan_id")
validated_data["identifier"] = bplan_id

# We receive the point as a string containing coordinates in epsg3875 but internally
# use epsg4326 so we need to convert them and save them as valid geojson
if "point" in validated_data:
point = validated_data["point"].split(",")
transformer = Transformer.from_crs("EPSG:3857", "EPSG:4326")
new_point = transformer.transform(point[0].strip(), point[1].strip())
validated_data["point"] = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [new_point[1], new_point[0]],
},
}

image_url = validated_data.pop("image_url", None)
if image_url:
validated_data["tile_image"] = self._download_image_from_url(image_url)
Expand Down
1 change: 0 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ brotli==1.1.0
django-celery-beat==2.7.0
django-cloudflare-push==0.2.2
django_csp==3.8
pyproj==3.7.0
redis==5.2.0
requests==2.32.3
sentry-sdk[django]==2.19.0
Expand Down
27 changes: 13 additions & 14 deletions tests/bplan/test_bplan_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def test_add_bplan_diplan_response_no_embed(apiclient, districts, organisation):
"start_date": "2013-01-01 18:00",
"end_date": "2021-01-01 18:00",
"bplan_id": "1-234",
"point": "0,0",
"point": "[0,0]",
}
user = organisation.initiators.first()
apiclient.force_authenticate(user=user)
Expand Down Expand Up @@ -412,7 +412,7 @@ def test_bplan_api_adds_is_diplan_if_point_is_sent(apiclient, districts, organis
"url": "https://bplan.net",
"start_date": "2013-01-01 18:00",
"identifier": "1-234",
"point": "0,0",
"point": "[0,0]",
"end_date": "2021-01-01 18:00",
}
user = organisation.initiators.first()
Expand Down Expand Up @@ -464,7 +464,7 @@ def test_bplan_api_location_task_not_called_if_point_included(
"end_date": "2021-01-01 18:00",
"identifier": "1-234",
"is_published": True,
"point": "0,0",
"point": "[0,0]",
}
user = organisation.initiators.first()
apiclient.force_authenticate(user=user)
Expand All @@ -474,18 +474,12 @@ def test_bplan_api_location_task_not_called_if_point_included(
bplan = bplan_models.Bplan.objects.first()
assert bplan.is_draft is False
assert bplan.is_diplan is True
assert bplan.point == {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.0, 0.0],
},
}
assert bplan.point == "[0,0]"
mock.assert_not_called()


@pytest.mark.django_db
def test_bplan_api_accepts_string_as_point_and_converts_to_epsg4326(
def test_bplan_api_accepts_valid_geojson(
apiclient, districts, organisation, django_capture_on_commit_callbacks
):
url = reverse("bplan-list", kwargs={"organisation_pk": organisation.pk})
Expand All @@ -497,9 +491,14 @@ def test_bplan_api_accepts_string_as_point_and_converts_to_epsg4326(
"end_date": "2021-01-01 18:00",
"identifier": "1-234",
"is_published": True,
"point": "1492195.544958444,6895923.461738203",
"point": {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.411924777644563, 52.499598134440944],
},
},
}

user = organisation.initiators.first()
apiclient.force_authenticate(user=user)
with django_capture_on_commit_callbacks(execute=True):
Expand All @@ -512,7 +511,7 @@ def test_bplan_api_accepts_string_as_point_and_converts_to_epsg4326(
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [13.404620649312287, 52.526688152152744],
"coordinates": [13.411924777644563, 52.499598134440944],
},
}

Expand Down

0 comments on commit 6081050

Please sign in to comment.