Full map download endpoint

This commit is contained in:
David Larlet 2023-11-08 15:20:59 -05:00
parent 3472f45159
commit 0be42d39cb
No known key found for this signature in database
GPG key ID: 3E2953A359E7E7BD
3 changed files with 87 additions and 3 deletions

View file

@ -603,3 +603,62 @@ def test_can_send_link_on_anonymous_map_with_cookie(cookieclient, anonymap):
assert resp.status_code == 200
assert len(mail.outbox) == 1
assert mail.outbox[0].subject == "The uMap edit link for your map: test map"
def test_download(client, map, datalayer):
url = reverse("map_download", args=(map.pk,))
response = client.get(url)
assert response.status_code == 200
# Test response is a json
j = json.loads(response.content.decode())
assert j["type"] == "umap"
assert j["uri"] == "http://testserver/en/map/test-map_1"
assert j["geometry"] == {
"coordinates": [13.447265624999998, 48.94415123418794],
"type": "Point",
}
assert j["properties"] == {
"datalayersControl": True,
"description": "Which is just the Danube, at the end",
"displayCaptionOnLoad": False,
"displayDataBrowserOnLoad": False,
"displayPopupFooter": False,
"licence": "",
"miniMap": False,
"moreControl": True,
"name": "Cruising on the Donau",
"scaleControl": True,
"tilelayer": {
"attribution": "© OSM Contributors",
"maxZoom": 18,
"minZoom": 0,
"url_template": "http://{s}.osm.fr/{z}/{x}/{y}.png",
},
"tilelayersControl": True,
"zoom": 7,
"zoomControl": True,
}
assert j["layers"] == [
{
"_umap_options": {
"browsable": True,
"displayOnLoad": True,
"name": "test datalayer",
},
"features": [
{
"geometry": {
"coordinates": [13.68896484375, 48.55297816440071],
"type": "Point",
},
"properties": {
"_umap_options": {"color": "DarkCyan", "iconClass": "Ball"},
"description": "Da place anonymous again 755",
"name": "Here",
},
"type": "Feature",
}
],
"type": "FeatureCollection",
},
]

View file

@ -39,6 +39,11 @@ urlpatterns = [
),
re_path(r"^i18n/", include("django.conf.urls.i18n")),
re_path(r"^agnocomplete/", include("agnocomplete.urls")),
re_path(
r"^map/(?P<map_id>\d+)/download/",
can_view_map(views.MapDownload.as_view()),
name="map_download",
),
]
i18n_urls = [

View file

@ -469,9 +469,7 @@ class MapDetailMixin:
else:
map_statuses = AnonymousMapPermissionsForm.STATUS
datalayer_statuses = AnonymousDataLayerPermissionsForm.STATUS
properties["edit_statuses"] = [
(i, str(label)) for i, label in map_statuses
]
properties["edit_statuses"] = [(i, str(label)) for i, label in map_statuses]
properties["datalayer_edit_statuses"] = [
(i, str(label)) for i, label in datalayer_statuses
]
@ -606,6 +604,28 @@ class MapView(MapDetailMixin, PermissionsMixin, DetailView):
return Star.objects.filter(by=user, map=self.object).exists()
class MapDownload(DetailView):
model = Map
pk_url_kwarg = "map_id"
def get_canonical_url(self):
return reverse("map_download", args=(self.object.pk,))
def render_to_response(self, context, *args, **kwargs):
geojson = self.object.settings
geojson["type"] = "umap"
geojson["uri"] = self.request.build_absolute_uri(self.object.get_absolute_url())
datalayers = []
for datalayer in self.object.datalayer_set.all():
with open(datalayer.geojson.path, "rb") as f:
layer = json.loads(f.read())
if datalayer.settings:
layer["_umap_options"] = datalayer.settings
datalayers.append(layer)
geojson["layers"] = datalayers
return simple_json_response(**geojson)
class MapViewGeoJSON(MapView):
def get_canonical_url(self):
return reverse("map_geojson", args=(self.object.pk,))