Skip to content

Commit

Permalink
Add editor url
Browse files Browse the repository at this point in the history
  • Loading branch information
meomancer committed May 31, 2024
1 parent 6c7ccef commit 6bafda1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
16 changes: 15 additions & 1 deletion django_project/context_layer_management/admin/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"""Context Layer Management."""

from django.contrib import admin
from django.utils.safestring import mark_safe

from context_layer_management.forms.layer import LayerForm, LayerUploadForm
from context_layer_management.models.layer import Layer, LayerField
from context_layer_management.models.layer_upload import LayerUpload
from context_layer_management.tasks import import_data
from context_layer_management.utils.layer import layer_style_url

EDITOR_URL = 'http://127.0.0.1:8888/'


class LayerFieldInline(admin.TabularInline):
Expand All @@ -31,7 +35,7 @@ class LayerAdmin(admin.ModelAdmin):
"""Layer admin."""

list_display = (
'unique_id', 'name', 'created_by', 'created_at', 'tile_url', 'metadata'
'unique_id', 'name', 'created_by', 'created_at', 'tile_url', 'editor'
)
form = LayerForm
inlines = [LayerFieldInline]
Expand All @@ -56,6 +60,16 @@ def field_names(self, obj: Layer):
"""Return fields."""
return obj.field_names

def editor(self, obj: Layer):
"""Return fields."""
return mark_safe(
f"<a target='__blank__' href='{EDITOR_URL}?"
f"styleUrl={layer_style_url(obj, obj.default_style, self.request)}"
f"'>Editor</a>"
)

editor.allow_tags = True


class LayerUploadAdmin(admin.ModelAdmin):
"""Layer admin."""
Expand Down
22 changes: 7 additions & 15 deletions django_project/context_layer_management/serializer/layer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# coding=utf-8
"""Context Layer Management."""

from django.urls import reverse
from rest_framework import serializers

from context_layer_management.models.layer import Layer
from context_layer_management.models.style import Style
from context_layer_management.utils.layer import layer_style_url


class LayerSerializer(serializers.ModelSerializer):
Expand All @@ -16,17 +15,6 @@ class LayerSerializer(serializers.ModelSerializer):
default_style = serializers.SerializerMethodField()
styles = serializers.SerializerMethodField()

def layer_style_url(self, obj: Layer, style: Style) -> str:
"""Return layer style url."""
request = self.context.get('request', None)
return request.build_absolute_uri('/')[:-1] + reverse(
'context-layer-management-style-view-set-detail',
kwargs={
'layer_id': obj.id,
'id': style.id
}
)

def get_tile_url(self, obj: Layer):
"""Return tile_url."""
request = self.context.get('request', None)
Expand All @@ -39,7 +27,9 @@ def get_created_by(self, obj: Layer):
def get_default_style(self, obj: Layer):
"""Return default style url."""
if obj.default_style:
return self.layer_style_url(obj, obj.default_style)
return layer_style_url(
obj, obj.default_style, self.context.get('request', None)
)
else:
return None

Expand All @@ -49,7 +39,9 @@ def get_styles(self, obj: Layer):
{
'id': style.id,
'name': style.name,
'style': self.layer_style_url(obj, style)
'style': layer_style_url(
obj, style, self.context.get('request', None)
)
} for style in obj.styles.all()
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def to_representation(self, instance):

class Meta: # noqa: D106
model = Style
fields = []
fields = ['id']
18 changes: 18 additions & 0 deletions django_project/context_layer_management/utils/layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""Context Layer Management."""

from django.urls import reverse

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


def layer_style_url(obj: Layer, style: Style, request) -> str:
"""Return layer style url."""
return request.build_absolute_uri('/')[:-1] + reverse(
'context-layer-management-style-view-set-detail',
kwargs={
'layer_id': obj.id,
'id': style.id
}
)

0 comments on commit 6bafda1

Please sign in to comment.