Skip to content

Commit

Permalink
Update maptunik to be able to save style
Browse files Browse the repository at this point in the history
  • Loading branch information
meomancer committed Jun 6, 2024
1 parent 0849226 commit 363c33a
Show file tree
Hide file tree
Showing 30 changed files with 67 additions and 1,707 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "maputnik"]
path = maputnik
url = git@github.com:kartoza/maputnik.git
url = https://github.com/kartoza/maputnik.git
branch = cloud-native-gis
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include django_project/LICENSE
include README.md
recursive-include django_project/cloud_native_gis/templates *
10 changes: 2 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,12 @@ serve-maputnik:
@echo "------------------------------------------------------------------"
@cd maputnik; npm install --verbose; npm run start;

run-build-maputnik:
@echo
@echo "------------------------------------------------------------------"
@echo "Build maptunik"
@echo "------------------------------------------------------------------"
@cd maputnik; npm install --verbose; npm run build-django;

build-maputnik: run-build-maputnik
build-maputnik:
@echo
@echo "------------------------------------------------------------------"
@echo "Change html to django"
@echo "------------------------------------------------------------------"
@cd maputnik; npm install --verbose; npm run build-django;
@cd ../../;
@python3 maputnik_html_to_django.py

Expand Down
4 changes: 2 additions & 2 deletions django_project/cloud_native_gis/admin/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from cloud_native_gis.models.layer import Layer, LayerAttributes
from cloud_native_gis.models.layer_upload import LayerUpload
from cloud_native_gis.tasks import import_data
from cloud_native_gis.utils.layer import layer_api_url, MAPUTNIK_URL
from cloud_native_gis.utils.layer import layer_api_url, maputnik_url


class LayerAttributeInline(admin.TabularInline):
Expand Down Expand Up @@ -61,7 +61,7 @@ def field_names(self, obj: Layer):
def editor(self, obj: Layer):
"""Return fields."""
return mark_safe(
f"<a target='__blank__' href='{MAPUTNIK_URL}?"
f"<a target='__blank__' href='{maputnik_url()}?"
f"api-url={layer_api_url(obj, self.request)}"
f"'>Editor</a>"
)
Expand Down
4 changes: 2 additions & 2 deletions django_project/cloud_native_gis/api/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from cloud_native_gis.models.style import Style
from cloud_native_gis.serializer.layer import LayerSerializer
from cloud_native_gis.serializer.style import LayerStyleSerializer
from cloud_native_gis.utils.layer import layer_style_url, MAPUTNIK_URL
from cloud_native_gis.utils.layer import layer_style_url, maputnik_url


class LayerViewSet(BaseApi):
Expand Down Expand Up @@ -129,6 +129,6 @@ def update(self, request, *args, **kwargs):
layer.styles.add(style)

return Response(
f'{MAPUTNIK_URL}?styleUrl='
f'{maputnik_url()}?styleUrl='
f'{layer_style_url(layer, style, self.request)}'
)
827 changes: 0 additions & 827 deletions django_project/cloud_native_gis/maputnik/assets/index-B7ypWbQA.js

This file was deleted.

This file was deleted.

This file was deleted.

827 changes: 0 additions & 827 deletions django_project/cloud_native_gis/maputnik/assets/index-Cm8ZtJ1I.js

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 3 additions & 3 deletions django_project/cloud_native_gis/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2024-06-05 10:37
# Generated by Django 4.2.7 on 2024-06-06 10:12

import cloud_native_gis.models.layer_upload
from django.conf import settings
Expand Down Expand Up @@ -30,7 +30,7 @@ class Migration(migrations.Migration):
('abstract', models.TextField(blank=True, null=True)),
('attribution', models.CharField(blank=True, max_length=512, null=True)),
('metadata', models.JSONField(blank=True, null=True)),
('created_by', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('created_by', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_related', related_query_name='%(app_label)s_%(class)ss', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
Expand Down Expand Up @@ -70,7 +70,7 @@ class Migration(migrations.Migration):
('status', models.CharField(choices=[('Start', 'Start'), ('Running', 'Running'), ('Failed', 'Failed'), ('Success', 'Success')], default='Start', max_length=100)),
('note', models.TextField(blank=True, null=True)),
('folder', models.TextField(default=cloud_native_gis.models.layer_upload.generate_folder)),
('created_by', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('created_by', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_related', related_query_name='%(app_label)s_%(class)ss', to=settings.AUTH_USER_MODEL)),
('layer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cloud_native_gis.layer')),
],
options={
Expand Down
4 changes: 3 additions & 1 deletion django_project/cloud_native_gis/models/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class AbstractResource(models.Model):

created_by = models.ForeignKey(
User, on_delete=models.CASCADE,
editable=False
editable=False,
related_name="%(app_label)s_%(class)s_related",
related_query_name="%(app_label)s_%(class)ss",
)
created_at = models.DateTimeField(
default=timezone.now,
Expand Down
2 changes: 1 addition & 1 deletion django_project/cloud_native_gis/models/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def tile_url(self):
return None

return reverse(
'cloud-native-gis-tile-api',
'cloud-native-gis-vector-tile',
kwargs={
'identifier': self.unique_id,
'x': '0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<meta charset="utf-8">
<title>Maputnik</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="{% static "/assets/manifest-BrZzkYP9.json" %}">
<link rel="icon" href="{% static "/assets/favicon-DBn6BKLx.ico" %}" type="image/x-icon" />
<link rel="manifest" href="{% static "/cloud_native_gis/manifest-BrZzkYP9.json" %}">
<link rel="icon" href="{% static "/cloud_native_gis/favicon-DBn6BKLx.ico" %}" type="image/x-icon" />
<style>
html {
background-color: rgb(28, 31, 36);
Expand Down Expand Up @@ -38,9 +38,12 @@
}

</style>
<script type="module" crossorigin src="{% static "/assets/index-DvofjCiq.js" %}"></script>
<link rel="stylesheet" crossorigin href="{% static "/assets/index-v8ZkhupJ.css" %}">
</head>
<script type="module" crossorigin src="{% static "/cloud_native_gis/index-DRZ64yjZ.js" %}"></script>
<link rel="stylesheet" crossorigin href="{% static "/cloud_native_gis/index-DW0d2Ij5.css" %}">

<script>
window.csrfmiddlewaretoken = '{{ csrf_token }}';
</script>
<body>
<!-- From <https://github.com/hail2u/color-blindness-emulation> -->
<svg
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions django_project/cloud_native_gis/tests/api/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setUp(self):

def test_list_api(self):
"""Test List API."""
url = reverse('cloud-native-gis-view-set-list')
url = reverse('cloud-native-gis-layer-view-set-list')
response = self.assertRequestGetView(url, 200, user=self.user)
self.assertEqual(len(response.json()['results']), 2)

Expand All @@ -45,13 +45,13 @@ def test_list_api_by_filter(self):
'name__contains': 'Layer 2'
}
)
url = reverse('cloud-native-gis-view-set-list') + '?' + params
url = reverse('cloud-native-gis-layer-view-set-list') + '?' + params
response = self.assertRequestGetView(url, 200, user=self.user)
self.assertEqual(len(response.json()['results']), 1)

def test_create_api(self):
"""Test POST API."""
url = reverse('cloud-native-gis-view-set-list')
url = reverse('cloud-native-gis-layer-view-set-list')
response = self.assertRequestPostView(
url, 201,
user=self.user,
Expand All @@ -69,11 +69,11 @@ def test_create_api(self):

def test_detail_api(self):
"""Test GET detail api."""
url = reverse('cloud-native-gis-view-set-list', args=[0])
url = reverse('cloud-native-gis-layer-view-set-list', args=[0])
self.assertRequestGetView(url, 404)

url = reverse(
'cloud-native-gis-view-set-detail',
'cloud-native-gis-layer-view-set-detail',
kwargs={'id': self.layer_1.id}
)
response = self.assertRequestGetView(url, 200, user=self.user).json()
Expand All @@ -86,11 +86,11 @@ def test_detail_api(self):

def test_update_api(self):
"""Test PUT API."""
url = reverse('cloud-native-gis-view-set-list', args=[0])
url = reverse('cloud-native-gis-layer-view-set-list', args=[0])
self.assertRequestPutView(url, 404, data={})

url = reverse(
'cloud-native-gis-view-set-detail',
'cloud-native-gis-layer-view-set-detail',
kwargs={'id': self.layer_1.id}
)
self.assertRequestPutView(url, 403, data={})
Expand Down Expand Up @@ -118,10 +118,10 @@ def test_update_api(self):
def test_delete_api(self):
"""Test DELETE API."""
_id = self.layer_1.id
url = reverse('cloud-native-gis-view-set-detail', args=[0])
url = reverse('cloud-native-gis-layer-view-set-detail', args=[0])
self.assertRequestDeleteView(url, 404)
url = reverse(
'cloud-native-gis-view-set-detail', kwargs={'id': _id}
'cloud-native-gis-layer-view-set-detail', kwargs={'id': _id}
)
self.assertRequestDeleteView(url, 403)
self.assertRequestDeleteView(url, 403, user=self.user_1)
Expand Down
7 changes: 4 additions & 3 deletions django_project/cloud_native_gis/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

router = DefaultRouter()
router.register(
r'layer', LayerViewSet, basename='cloud-native-gis-view-set'
r'layer', LayerViewSet, basename='cloud-native-gis-layer-view-set'
)
layer_router = NestedSimpleRouter(
router, r'layer', lookup='layer'
Expand All @@ -27,12 +27,13 @@
path(
'<str:identifier>/tile/<int:z>/<int:x>/<int:y>/',
VectorTileLayer.as_view(),
name='cloud-native-gis-tile-api'
name='cloud-native-gis-vector-tile'
),
path('api/', include(router.urls)),
path('api/', include(layer_router.urls)),
path(
'maputnik/',
TemplateView.as_view(template_name='maputnik/index.html')
TemplateView.as_view(template_name='cloud_native_gis/index.html'),
name='cloud-native-gis-maputnik'
)
]
16 changes: 10 additions & 6 deletions django_project/cloud_native_gis/utils/layer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# coding=utf-8
"""Cloud Native GIS."""

from django.conf import settings
import os

from django.urls import reverse

from cloud_native_gis.models.layer import Layer
from cloud_native_gis.models.style import Style

try:
MAPUTNIK_URL = settings.MAPUTNIK_URL
except AttributeError:
MAPUTNIK_URL = '/maputnik'

def maputnik_url() -> str:
"""Return url for mapnik layer."""
try:
return os.environ['MAPUTNIK_URL']
except KeyError:
return reverse('cloud-native-gis-maputnik')


def layer_style_url(layer: Layer, style: Style, request) -> str:
Expand All @@ -29,7 +33,7 @@ def layer_style_url(layer: Layer, style: Style, request) -> str:
def layer_api_url(layer: Layer, request) -> str:
"""Return layer api url."""
return request.build_absolute_uri('/')[:-1] + reverse(
'cloud-native-gis-view-set-detail',
'cloud-native-gis-layer-view-set-detail',
kwargs={
'id': layer.id
}
Expand Down
2 changes: 1 addition & 1 deletion django_project/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
# Don't forget to use absolute paths, not relative paths.
absolute_path('core', 'static'),
absolute_path('frontend', 'bundles'),
absolute_path('cloud_native_gis', 'maputnik'),
absolute_path('cloud_native_gis', 'templates', 'cloud_native_gis'),
)

# Every cache key will get prefixed with this value - here we set it to
Expand Down
2 changes: 0 additions & 2 deletions django_project/core/settings/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@
TEMPLATES[0]['DIRS'] += [
absolute_path('frontend', 'templates'),
]

MAPUTNIK_URL = os.environ.get('MAPUTNIK_URL', '/maputnik')
2 changes: 1 addition & 1 deletion maputnik
10 changes: 9 additions & 1 deletion maputnik_html_to_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def maputnik_html_to_django():
"""Change hmtl file to django file."""
path = 'django_project/cloud_native_gis/maputnik/index.html'
path = 'django_project/cloud_native_gis/templates/cloud_native_gis/index.html'

Check failure on line 11 in maputnik_html_to_django.py

View workflow job for this annotation

GitHub Actions / Python Lint

maputnik_html_to_django.py#L11

[E501] line too long

with open(path, 'r') as file:
filedata = file.read()
Expand All @@ -25,6 +25,14 @@ def maputnik_html_to_django():
filedata
)
filedata = filedata.replace('/maputnik', '')
filedata = filedata.replace('/static', '')
filedata = filedata.replace(
'</head>',
'''
<script>
window.csrfmiddlewaretoken = '{{ csrf_token }}';
</script>'''
)

with open(path, 'w') as file:
file.write(filedata)
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[egg_info]
tag_build =
tag_date = 0

0 comments on commit 363c33a

Please sign in to comment.