From d224ac91f2416c5ca0e24d45831fb820e02d6e3e Mon Sep 17 00:00:00 2001 From: Erick Otenyo Date: Mon, 13 Nov 2023 13:14:54 +0300 Subject: [PATCH 1/3] Update caching to include must-revalidate cache control header --- geomanager/decorators.py | 23 +++++++++++++++++++++++ geomanager/urls.py | 8 +++----- geomanager/views/nextjs.py | 2 -- geomanager/views/raster_file.py | 7 +++++++ geomanager/views/vector_file.py | 4 +++- geomanager/viewsets/vector.py | 6 ++++++ sandbox/sandbox/settings/base.py | 2 -- 7 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 geomanager/decorators.py diff --git a/geomanager/decorators.py b/geomanager/decorators.py new file mode 100644 index 0000000..aab887b --- /dev/null +++ b/geomanager/decorators.py @@ -0,0 +1,23 @@ +from functools import wraps + +from django.utils.cache import patch_cache_control + + +def revalidate_cache(view_func): + """ + Decorator that adds must_revalidate header to a response + """ + + @wraps(view_func) + def _wrapped_view_func(request, *args, **kwargs): + # Ensure argument looks like a request. + if not hasattr(request, "META"): + raise TypeError( + "revalidate_cache didn't receive an HttpRequest. If you are " + "decorating a classmethod, be sure to use @method_decorator." + ) + response = view_func(request, *args, **kwargs) + patch_cache_control(response, must_revalidate=True) + return response + + return _wrapped_view_func diff --git a/geomanager/urls.py b/geomanager/urls.py index 30fd320..7ba70a3 100644 --- a/geomanager/urls.py +++ b/geomanager/urls.py @@ -2,7 +2,6 @@ from rest_framework.routers import SimpleRouter from rest_framework_simplejwt.views import ( TokenRefreshView, ) -from wagtailcache.cache import nocache_page from geomanager.viewsets.aoi import AoiViewSet from .views import ( @@ -66,8 +65,8 @@ path('api/auth/register/', RegisterView.as_view(), name='auth_register'), path('api/auth/reset-password/', ResetPasswordView.as_view(), name='auth_password_reset'), path('api/auth/token/', EmailTokenObtainPairView.as_view(), name='token_obtain_pair'), - path('api/auth/token/verify/', nocache_page(UserTokenVerifyView.as_view()), name='token_verify'), - path('api/auth/token/refresh/', nocache_page(TokenRefreshView.as_view()), name='token_refresh'), + path('api/auth/token/verify/', UserTokenVerifyView.as_view(), name='token_verify'), + path('api/auth/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), # User Profile path('api/geomanager-profile/', get_geomanager_user_profile, @@ -126,8 +125,7 @@ name="raster_data_geostore_timeseries"), # FeatureServ - path(r'api/feature-serv/.geojson', GeoJSONPgTableView.as_view(), - name="feature_serv"), + path(r'api/feature-serv/.geojson', GeoJSONPgTableView.as_view(), name="feature_serv"), # Tiles GL path(r'api/tile-gl/tile////.pbf', tile_gl, name="tile_gl"), diff --git a/geomanager/views/nextjs.py b/geomanager/views/nextjs.py index 90a2933..ca01a25 100644 --- a/geomanager/views/nextjs.py +++ b/geomanager/views/nextjs.py @@ -1,12 +1,10 @@ from django_nextjs.render import render_nextjs_page_sync -from wagtailcache.cache import nocache_page from wagtailiconchooser.utils import get_svg_sprite_for_icons from geomanager.models import GeomanagerSettings, Category from geomanager.models.vector_file import get_legend_icons -@nocache_page def map_view(request, location_type=None, adm0=None, adm1=None, adm2=None): # get svg sprite for categories and legend icons category_icons = [category.icon for category in Category.objects.all()] diff --git a/geomanager/views/raster_file.py b/geomanager/views/raster_file.py index 87e5288..3712809 100644 --- a/geomanager/views/raster_file.py +++ b/geomanager/views/raster_file.py @@ -31,6 +31,7 @@ from wagtail_modeladmin.helpers import AdminURLHelper from wagtailcache.cache import cache_page +from geomanager.decorators import revalidate_cache from geomanager.errors import RasterFileNotFound, QueryParamRequired, GeostoreNotFound from geomanager.forms import LayerRasterFileForm from geomanager.models import ( @@ -513,6 +514,7 @@ def get_query_param(self, request: Request, key: str, default: Optional[Any] = ' return request.query_params.get(key, str(default)) +@method_decorator(revalidate_cache, name='get') @method_decorator(cache_page, name='get') class RasterTileView(RasterDataMixin, APIView): # TODO: Validate style query param thoroughly. If not validated, the whole app just exits without warning. @@ -571,6 +573,7 @@ def get(self, request, layer_id, z, x, y): return HttpResponse(tile_binary, content_type=mime_type) +@method_decorator(revalidate_cache, name='get') @method_decorator(cache_page, name='get') class RasterThumbnailView(RasterDataMixin, APIView): def get(self, request, file_id): @@ -622,6 +625,7 @@ def get(self, request, file_id): return HttpResponse(thumb_data, content_type=mime_type) +@method_decorator(revalidate_cache, name='get') @method_decorator(cache_page, name='get') class RasterDataPixelView(RasterDataMixin, APIView): renderer_classes = [JSONRenderer] @@ -637,6 +641,7 @@ def get(self, request, layer_id): return Response(pixel_data) +@method_decorator(revalidate_cache, name='get') @method_decorator(cache_page, name='get') class RasterDataPixelTimeseriesView(RasterDataMixin, APIView): renderer_classes = [JSONRenderer] @@ -659,6 +664,7 @@ def get(self, request, layer_id): return Response(timeseries_data) +@method_decorator(revalidate_cache, name='get') @method_decorator(cache_page, name='get') class RasterDataGeostoreView(RasterDataMixin, APIView): renderer_classes = [JSONRenderer] @@ -676,6 +682,7 @@ def get(self, request, layer_id): return Response(data) +@method_decorator(revalidate_cache, name='get') @method_decorator(cache_page, name='get') class RasterDataGeostoreTimeseriesView(RasterDataMixin, APIView): renderer_classes = [JSONRenderer] diff --git a/geomanager/views/vector_file.py b/geomanager/views/vector_file.py index 4451fa8..c856df9 100644 --- a/geomanager/views/vector_file.py +++ b/geomanager/views/vector_file.py @@ -19,6 +19,7 @@ from wagtail_modeladmin.helpers import AdminURLHelper from wagtailcache.cache import cache_page, clear_cache +from geomanager.decorators import revalidate_cache from geomanager.forms import VectorLayerFileForm, VectorTableForm from geomanager.models import Dataset from geomanager.models.core import GeomanagerSettings, Category @@ -316,6 +317,7 @@ def preview_vector_layers(request, dataset_id, layer_id=None): return render(request, template_name=template_name, context=context) +@method_decorator(revalidate_cache, name='get') @method_decorator(cache_page, name='get') class VectorTileView(View): def get(self, request, z, x, y): @@ -382,10 +384,10 @@ def get(self, request, z, x, y): return HttpResponse("Tile not found", status=404) return HttpResponse(tile, content_type="application/x-protobuf") except Exception as e: - print("Error", e) return HttpResponse(f"Error while fetching tile: {e}", status=500) +@method_decorator(revalidate_cache, name='get') @method_decorator(cache_page, name='get') class GeoJSONPgTableView(View): def get(self, request, table_name): diff --git a/geomanager/viewsets/vector.py b/geomanager/viewsets/vector.py index 46ddaf7..ff37eb1 100644 --- a/geomanager/viewsets/vector.py +++ b/geomanager/viewsets/vector.py @@ -14,6 +14,7 @@ from wagtailcache.cache import cache_page from geomanager import serializers +from geomanager.decorators import revalidate_cache from geomanager.models import Geostore from geomanager.models.vector_file import PgVectorTable from geomanager.serializers.geostore import GeostoreSerializer @@ -32,6 +33,7 @@ class AdminBoundaryViewSet(viewsets.ViewSet): renderer_classes = [JSONRenderer] @action(detail=True, methods=['get']) + @method_decorator(revalidate_cache) @method_decorator(cache_page) def get(self, request): countries = AdminBoundary.objects.filter(level=0) @@ -39,6 +41,7 @@ def get(self, request): return Response(data) @action(detail=True, methods=['get']) + @method_decorator(revalidate_cache) @method_decorator(cache_page) def get_regions(self, request, gid_0): countries = AdminBoundary.objects.filter(level=1, gid_0=gid_0) @@ -46,6 +49,7 @@ def get_regions(self, request, gid_0): return Response(data) @action(detail=True, methods=['get']) + @method_decorator(revalidate_cache) @method_decorator(cache_page) def get_sub_regions(self, request, gid_0, gid_1): countries = AdminBoundary.objects.filter(level=2, gid_0=gid_0, gid_1=gid_1) @@ -79,6 +83,7 @@ def post(self, request): return Response(res_data) @action(detail=True, methods=['get']) + @method_decorator(revalidate_cache) @method_decorator(cache_page) def get(self, request, geostore_id): try: @@ -89,6 +94,7 @@ def get(self, request, geostore_id): raise NotFound(detail='Geostore not found') @action(detail=True, methods=['get']) + @method_decorator(revalidate_cache) @method_decorator(cache_page) def get_by_admin(self, request, gid_0, gid_1=None, gid_2=None): abm_settings = AdminBoundarySettings.for_request(request) diff --git a/sandbox/sandbox/settings/base.py b/sandbox/sandbox/settings/base.py index 9b5944b..fded27d 100644 --- a/sandbox/sandbox/settings/base.py +++ b/sandbox/sandbox/settings/base.py @@ -95,7 +95,6 @@ ] MIDDLEWARE = [ - "wagtailcache.cache.UpdateCacheMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "corsheaders.middleware.CorsMiddleware", "django.middleware.common.CommonMiddleware", @@ -105,7 +104,6 @@ "django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.security.SecurityMiddleware", "wagtail.contrib.redirects.middleware.RedirectMiddleware", - "wagtailcache.cache.FetchFromCacheMiddleware", ] ROOT_URLCONF = "sandbox.urls" From a9ed24fe4d59b25770cab758a46db824fb7c5165 Mon Sep 17 00:00:00 2001 From: Erick Otenyo Date: Mon, 13 Nov 2023 16:19:29 +0300 Subject: [PATCH 2/3] Update getting full urls --- geomanager/models/raster_file.py | 10 ++++------ geomanager/models/tile_base.py | 3 ++- geomanager/models/vector_file.py | 6 +++--- geomanager/models/wms.py | 3 ++- geomanager/urls.py | 2 +- geomanager/views/core.py | 20 +++++++++++--------- geomanager/views/nextjs.py | 5 +++-- geomanager/views/raster_file.py | 9 +++++---- geomanager/views/tile_gl.py | 5 +++-- geomanager/views/vector_file.py | 16 +++++++++------- geomanager/views/vector_tile.py | 3 ++- geomanager/wagtail_hooks.py | 3 ++- sandbox/home/models.py | 5 +++-- sandbox/home/utils.py | 5 ++++- sandbox/home/views.py | 5 +++-- 15 files changed, 57 insertions(+), 43 deletions(-) diff --git a/geomanager/models/raster_file.py b/geomanager/models/raster_file.py index 54d4595..649d50a 100644 --- a/geomanager/models/raster_file.py +++ b/geomanager/models/raster_file.py @@ -11,6 +11,7 @@ from modelcluster.fields import ParentalKey from modelcluster.models import ClusterableModel from wagtail.admin.panels import FieldPanel, FieldRowPanel, MultiFieldPanel, InlinePanel +from wagtail.api.v2.utils import get_full_url from wagtail.fields import StreamField from wagtail.models import Orderable from wagtail_color_panel.edit_handlers import NativeColorPanel @@ -93,16 +94,14 @@ def base_tile_url(self): def get_tile_json_url(self, request=None): tile_json_url = reverse("raster_file_tile_json", args=(self.id,)) if request: - base_absolute_url = request.scheme + '://' + request.get_host() - tile_json_url = base_absolute_url + tile_json_url + tile_json_url = get_full_url(request, tile_json_url) return tile_json_url def layer_config(self, request=None): base_tile_url = self.base_tile_url if request: - base_absolute_url = request.scheme + '://' + request.get_host() - base_tile_url = base_absolute_url + base_tile_url + base_tile_url = get_full_url(request, base_tile_url) tile_url = f"{base_tile_url}?time={{time}}" @@ -209,8 +208,7 @@ def get_tile_json(self, request=None): timestamps = list(self.raster_files.all().values_list("time", flat=True)) if request: - base_absolute_url = request.scheme + '://' + request.get_host() - base_tile_url = base_absolute_url + base_tile_url + base_tile_url = get_full_url(request, base_tile_url) tile_json = { "tilejson": "3.0.0", diff --git a/geomanager/models/tile_base.py b/geomanager/models/tile_base.py index 38a495c..cd4a9cb 100644 --- a/geomanager/models/tile_base.py +++ b/geomanager/models/tile_base.py @@ -3,6 +3,7 @@ from django_extensions.db.models import TimeStampedModel from modelcluster.models import ClusterableModel from wagtail.admin.panels import FieldPanel, MultiFieldPanel +from wagtail.api.v2.utils import get_full_url from wagtail.fields import StreamField from wagtail.images.blocks import ImageChooserBlock from wagtail.images.models import Image @@ -208,7 +209,7 @@ def get_legend_config(self, request): if isinstance(legend_block.value, Image): image_url = legend_block.value.file.url if request: - image_url = request.build_absolute_uri(image_url) + image_url = get_full_url(request, image_url) config.update({"type": "image", "imageUrl": image_url}) return config diff --git a/geomanager/models/vector_file.py b/geomanager/models/vector_file.py index 74db260..c8c454e 100644 --- a/geomanager/models/vector_file.py +++ b/geomanager/models/vector_file.py @@ -11,6 +11,7 @@ from modelcluster.fields import ParentalKey from modelcluster.models import ClusterableModel from wagtail.admin.panels import FieldPanel +from wagtail.api.v2.utils import get_full_url from wagtail.fields import StreamField from wagtail.images.blocks import ImageChooserBlock @@ -86,8 +87,7 @@ def layer_config(self, request=None): base_tiles_url = base_tiles_url.replace("/0/0/0", r"/{z}/{x}/{y}") if request: - base_absolute_url = request.scheme + '://' + request.get_host() - base_tiles_url = base_absolute_url + base_tiles_url + base_tiles_url = get_full_url(request, base_tiles_url) tile_url = f"{base_tiles_url}?table_name={{table_name}}" @@ -149,7 +149,7 @@ def get_legend_config(self, request=None): if legend_block.block_type == "legend_image": image_url = legend_block.value.file.url if request: - image_url = request.build_absolute_uri(image_url) + image_url = get_full_url(request, image_url) config.update({"type": "image", "imageUrl": image_url}) return config diff --git a/geomanager/models/wms.py b/geomanager/models/wms.py index 90d314a..f6a1822 100644 --- a/geomanager/models/wms.py +++ b/geomanager/models/wms.py @@ -4,6 +4,7 @@ from modelcluster.fields import ParentalKey from modelcluster.models import ClusterableModel from wagtail.admin.panels import FieldPanel, FieldRowPanel, MultiFieldPanel, InlinePanel +from wagtail.api.v2.utils import get_full_url from wagtail.fields import StreamField from wagtail.images.blocks import ImageChooserBlock from wagtail.images.models import Image @@ -288,7 +289,7 @@ def get_legend_config(self, request): if isinstance(legend_block.value, Image): image_url = legend_block.value.file.url if request: - image_url = request.build_absolute_uri(image_url) + image_url = get_full_url(request, image_url) config.update({"type": "image", "imageUrl": image_url}) return config diff --git a/geomanager/urls.py b/geomanager/urls.py index 7ba70a3..9537a54 100644 --- a/geomanager/urls.py +++ b/geomanager/urls.py @@ -45,7 +45,7 @@ router.register(r'api/metadata', MetadataViewSet) router.register(r'api/file-raster', RasterLayerRasterFileDetailViewSet, basename="file-raster") -router.register(r'api/vector-data', VectorTableFileDetailViewSet) +router.register(r'api/vector-data', VectorTableFileDetailViewSet, basename="vector-data") router.register(r'api/aoi', AoiViewSet) diff --git a/geomanager/views/core.py b/geomanager/views/core.py index 912a8a3..d56f590 100644 --- a/geomanager/views/core.py +++ b/geomanager/views/core.py @@ -1,7 +1,9 @@ from adminboundarymanager.models import AdminBoundarySettings +from django.urls import reverse from rest_framework.decorators import api_view, renderer_classes from rest_framework.renderers import JSONRenderer from rest_framework.response import Response +from wagtail.api.v2.utils import get_full_url from geomanager.models import Category, VectorLayerIcon, VectorTileLayerIcon from geomanager.models.core import GeomanagerSettings @@ -21,33 +23,33 @@ def get_mapviewer_config(request): } links = { - "mapViewerBaseUrl": request.build_absolute_uri("/mapviewer"), + "mapViewerBaseUrl": get_full_url(request, (reverse("mapview"))), } if gm_settings.terms_of_service_page: - links.update({"termsOfServicePageUrl": request.build_absolute_uri(gm_settings.terms_of_service_page.url)}) + links.update({"termsOfServicePageUrl": get_full_url(request, gm_settings.terms_of_service_page.url)}) if gm_settings.privacy_policy_page: - links.update({"privacyPolicyPageUrl": request.build_absolute_uri(gm_settings.privacy_policy_page.url)}) + links.update({"privacyPolicyPageUrl": get_full_url(request, gm_settings.privacy_policy_page.url)}) response.update({"links": links}) icon_images = [] for icon in VectorLayerIcon.objects.all(): - icon_images.append({"name": icon.name, "url": request.build_absolute_uri(icon.file.url)}) + icon_images.append({"name": icon.name, "url": get_full_url(request, icon.file.url)}) for icon in VectorTileLayerIcon.objects.all(): - icon_images.append({"name": icon.name, "url": request.build_absolute_uri(icon.file.url)}) + icon_images.append({"name": icon.name, "url": get_full_url(request, icon.file.url)}) response.update({"vectorLayerIcons": icon_images}) if gm_settings.logo: logo = { - "imageUrl": request.build_absolute_uri(gm_settings.logo.file.url) + "imageUrl": get_full_url(request, gm_settings.logo.file.url) } if gm_settings.logo_page: - logo.update({"linkUrl": request.build_absolute_uri(gm_settings.logo_page.url)}) + logo.update({"linkUrl": get_full_url(request, gm_settings.logo_page.url)}) if not gm_settings.logo_page and gm_settings.logo_external_link: logo.update({"linkUrl": gm_settings.logo_external_link, "external": True}) @@ -71,9 +73,9 @@ def get_mapviewer_config(request): data = base_map.block.get_api_representation(base_map.value) for key, value in base_map.value.items(): if key == "image" and value: - data.update({"image": request.build_absolute_uri(value.file.url)}) + data.update({"image": get_full_url(request, value.file.url)}) - data.update({"mapStyle": request.build_absolute_uri(tile_gl_source.map_style_url)}) + data.update({"mapStyle": get_full_url(request, tile_gl_source.map_style_url)}) base_maps_data.append(data) response.update({"basemaps": base_maps_data}) diff --git a/geomanager/views/nextjs.py b/geomanager/views/nextjs.py index ca01a25..057f96a 100644 --- a/geomanager/views/nextjs.py +++ b/geomanager/views/nextjs.py @@ -1,4 +1,5 @@ from django_nextjs.render import render_nextjs_page_sync +from wagtail.api.v2.utils import get_full_url from wagtailiconchooser.utils import get_svg_sprite_for_icons from geomanager.models import GeomanagerSettings, Category @@ -16,11 +17,11 @@ def map_view(request, location_type=None, adm0=None, adm1=None, adm2=None): context = { "svg_sprite": svg_sprite, "logo": gm_settings.logo, - "logo_url": request.build_absolute_uri("/") + "logo_url": get_full_url(request, "") } if gm_settings.logo_page: - context.update({"logo_url": request.build_absolute_uri(gm_settings.logo_page.url)}) + context.update({"logo_url": get_full_url(request, gm_settings.logo_page.url)}) if not gm_settings.logo_page and gm_settings.logo_external_link: context.update({"logo_url": gm_settings.logo_external_link, "logo_url_external": True}) diff --git a/geomanager/views/raster_file.py b/geomanager/views/raster_file.py index 3712809..b21578a 100644 --- a/geomanager/views/raster_file.py +++ b/geomanager/views/raster_file.py @@ -26,6 +26,7 @@ user_has_any_page_permission, permission_denied ) +from wagtail.api.v2.utils import get_full_url from wagtail.models import Site from wagtail.snippets.permissions import get_permission_name from wagtail_modeladmin.helpers import AdminURLHelper @@ -385,7 +386,7 @@ def delete_raster_upload(request, upload_id): def preview_raster_layers(request, dataset_id, layer_id=None): dataset = get_object_or_404(Dataset, pk=dataset_id) - base_absolute_url = request.scheme + '://' + request.get_host() + base_absolute_url = get_full_url(request, "") category_admin_helper = AdminURLHelper(Category) categories_url = category_admin_helper.get_action_url("index") @@ -417,9 +418,9 @@ def preview_raster_layers(request, dataset_id, layer_id=None): "selected_layer": selected_layer, "datasets_index_url": dataset_list_url, "image_file_layer_list_url": raster_file_layer_list_url, - "file_raster_list_url": request.build_absolute_uri(reverse("file-raster-list")), - "large_image_color_maps_url": request.build_absolute_uri(reverse("large-image-colormaps")), - "file_raster_metadata_url": request.build_absolute_uri(reverse("file-raster-metadata", args=("0",))), + "file_raster_list_url": get_full_url(request, reverse("file-raster-list")), + "large_image_color_maps_url": get_full_url(request, reverse("large-image-colormaps")), + "file_raster_metadata_url": get_full_url(request, reverse("file-raster-metadata", args=("0",))), "base_absolute_url": base_absolute_url, "navigation_items": navigation_items, } diff --git a/geomanager/views/tile_gl.py b/geomanager/views/tile_gl.py index c5d4422..0f3fb0a 100644 --- a/geomanager/views/tile_gl.py +++ b/geomanager/views/tile_gl.py @@ -1,5 +1,6 @@ from django.http import HttpResponse, JsonResponse from django.urls import reverse +from wagtail.api.v2.utils import get_full_url from wagtailcache.cache import cache_page from geomanager.errors import MissingTileError @@ -76,7 +77,7 @@ def tile_json_gl(request, source_slug): ) # Tile - tile_url = request.build_absolute_uri(reverse("tile_gl", args=(source.slug, 0, 0, 0))) + tile_url = get_full_url(request, (reverse("tile_gl", args=(source.slug, 0, 0, 0)))) tile_url = tile_url.replace("/0/0/0.pbf", r"/{z}/{x}/{y}.pbf") spec["tiles"] = [tile_url] @@ -89,7 +90,7 @@ def tile_json_gl(request, source_slug): @cache_page def style_json_gl(request, source_slug): source = MBTSource.objects.get(slug=source_slug) - tilejson_url = request.build_absolute_uri(reverse("tile_json_gl", args=[source.slug])) + tilejson_url = get_full_url(request, reverse("tile_json_gl", args=[source.slug])) style_config = source.json_style diff --git a/geomanager/views/vector_file.py b/geomanager/views/vector_file.py index c856df9..0d86904 100644 --- a/geomanager/views/vector_file.py +++ b/geomanager/views/vector_file.py @@ -14,6 +14,7 @@ from django.views import View from wagtail.admin import messages from wagtail.admin.auth import user_passes_test, user_has_any_page_permission, permission_denied +from wagtail.api.v2.utils import get_full_url from wagtail.models import Site from wagtail.snippets.permissions import get_permission_name from wagtail_modeladmin.helpers import AdminURLHelper @@ -239,8 +240,6 @@ def preview_vector_layers(request, dataset_id, layer_id=None): template_name = 'geomanager/vector_file/vector_file_layer_preview.html' dataset = get_object_or_404(Dataset, pk=dataset_id) - base_absolute_url = request.scheme + '://' + request.get_host() - category_admin_helper = AdminURLHelper(Category) categories_url = category_admin_helper.get_action_url("index") @@ -251,8 +250,7 @@ def preview_vector_layers(request, dataset_id, layer_id=None): vector_layer_list_url = vector_layer_admin_helper.get_action_url("index") vector_layer_list_url = vector_layer_list_url + f"?dataset__id__exact={dataset_id}" - geojson_url = request.build_absolute_uri( - reverse("feature_serv", args=("table_name",)).replace("table_name.geojson", "")) + geojson_url = get_full_url(request, reverse("feature_serv", args=("table_name",)).replace("table_name.geojson", "")) data_table = PgVectorTable.objects.filter(layer__id=layer_id) @@ -271,7 +269,7 @@ def preview_vector_layers(request, dataset_id, layer_id=None): icon_images = [] layers_id = [layer.get("id") for layer in dataset_layers] for icon in VectorLayerIcon.objects.filter(layer__in=layers_id): - icon_images.append({"name": icon.name, "url": request.build_absolute_uri(icon.file.url)}) + icon_images.append({"name": icon.name, "url": get_full_url(request, icon.file.url)}) navigation_items = [ {"url": categories_url, "label": Category._meta.verbose_name_plural}, @@ -280,14 +278,18 @@ def preview_vector_layers(request, dataset_id, layer_id=None): {"url": "#", "label": _("Preview")}, ] + vector_tiles_url = reverse("vector_tiles", args=(0, 0, 0)) + vector_tiles_url = vector_tiles_url.replace("/0/0/0", r"/{z}/{x}/{y}") + vector_tiles_url = get_full_url(request, vector_tiles_url) + context = { "dataset": dataset, "dataset_layers": json.dumps(dataset_layers, cls=UUIDEncoder), "selected_layer": layer_id, "datasets_index_url": dataset_list_url, "vector_layer_list_url": vector_layer_list_url, - "data_vector_api_base_url": request.build_absolute_uri("/api/vector-data"), - "vector_tiles_url": base_absolute_url + "/api/vector-tiles/{z}/{x}/{y}", + "data_vector_api_base_url": get_full_url(request, reverse("vector-data-list")), + "vector_tiles_url": vector_tiles_url, "geojson_url": geojson_url, "data_table": data_table, "icon_images": icon_images, diff --git a/geomanager/views/vector_tile.py b/geomanager/views/vector_tile.py index 76f28da..cc4333b 100644 --- a/geomanager/views/vector_tile.py +++ b/geomanager/views/vector_tile.py @@ -6,6 +6,7 @@ user_passes_test, user_has_any_page_permission ) +from wagtail.api.v2.utils import get_full_url from wagtail_modeladmin.helpers import AdminURLHelper from geomanager.models import ( @@ -40,7 +41,7 @@ def preview_vector_tile_layers(request, dataset_id, layer_id=None): icon_images = [] layers_id = [layer.get("id") for layer in dataset_layers] for icon in VectorTileLayerIcon.objects.filter(layer__in=layers_id): - icon_images.append({"name": icon.name, "url": request.build_absolute_uri(icon.file.url)}) + icon_images.append({"name": icon.name, "url": get_full_url(request, icon.file.url)}) navigation_items = [ {"url": categories_url, "label": Category._meta.verbose_name_plural}, diff --git a/geomanager/wagtail_hooks.py b/geomanager/wagtail_hooks.py index 53aacca..acd1997 100644 --- a/geomanager/wagtail_hooks.py +++ b/geomanager/wagtail_hooks.py @@ -1,5 +1,6 @@ from adminboundarymanager.models import AdminBoundarySettings from wagtail import hooks +from wagtail.api.v2.utils import get_full_url from wagtail_modeladmin.options import modeladmin_register from .admin import GeoManagerAdminGroup, urls as geomanager_urls @@ -38,7 +39,7 @@ def register_geomanager_datasets(request): abm_settings = AdminBoundarySettings.for_request(request) boundary_tiles_url = abm_settings.boundary_tiles_url - boundary_tiles_url = request.scheme + '://' + request.get_host() + boundary_tiles_url + boundary_tiles_url = get_full_url(request, boundary_tiles_url) # create boundary dataset boundary_dataset = create_boundary_dataset(boundary_tiles_url) diff --git a/sandbox/home/models.py b/sandbox/home/models.py index 6c42601..3e1d1e2 100644 --- a/sandbox/home/models.py +++ b/sandbox/home/models.py @@ -6,6 +6,7 @@ from django.utils.translation import gettext_lazy as _ from django_tables2 import tables, LazyPaginator, TemplateColumn from wagtail.admin.panels import FieldPanel +from wagtail.api.v2.utils import get_full_url from wagtail.contrib.routable_page.models import path, RoutablePageMixin from wagtail.contrib.settings.models import BaseSiteSetting from wagtail.contrib.settings.registry import register_setting @@ -141,7 +142,7 @@ def all_stations(self, request, *args, **kwargs): context = {} station_settings = StationSettings.for_request(request) - stations_vector_tiles_url = request.scheme + '://' + request.get_host() + station_settings.stations_vector_tiles_url + stations_vector_tiles_url = get_full_url(request, station_settings.stations_vector_tiles_url) context.update({ "mapConfig": { @@ -158,7 +159,7 @@ def all_stations(self, request, *args, **kwargs): table_fields = [field.get("name") for field in station_table_columns_list] - page_url = request.build_absolute_uri(self.url) + page_url = get_full_url(request, self.url) class StationTable(tables.Table): detail_url = TemplateColumn('') diff --git a/sandbox/home/utils.py b/sandbox/home/utils.py index 9aedc20..6f49dc6 100644 --- a/sandbox/home/utils.py +++ b/sandbox/home/utils.py @@ -1,3 +1,6 @@ +from wagtail.api.v2.utils import get_full_url + + def create_stations_geomanager_dataset(station_settings, request=None): sub_category = station_settings.geomanager_subcategory @@ -28,7 +31,7 @@ def create_stations_geomanager_dataset(station_settings, request=None): station_tiles_url = station_settings.stations_vector_tiles_url if request: - station_tiles_url = request.scheme + '://' + request.get_host() + station_tiles_url + station_tiles_url = get_full_url(request, station_tiles_url) layer = { "id": dataset_id, diff --git a/sandbox/home/views.py b/sandbox/home/views.py index c80bdaa..9e68673 100644 --- a/sandbox/home/views.py +++ b/sandbox/home/views.py @@ -8,6 +8,8 @@ from django.utils.decorators import method_decorator from django.utils.translation import gettext as _ from django.views import View +from wagtail.api.v2.utils import get_full_url + from geomanager.utils.vector_utils import ogr_db_import from wagtail.admin import messages from wagtail.admin.auth import user_passes_test, user_has_any_page_permission @@ -96,8 +98,7 @@ def preview_stations(request): template = "stations/stations_preview.html" stations_settings = StationSettings.for_request(request) - stations_vector_tiles_url = request.scheme + '://' + request.get_host() + \ - stations_settings.stations_vector_tiles_url + stations_vector_tiles_url = get_full_url(request, stations_settings.stations_vector_tiles_url) context = { "mapConfig": { From 1eaa2e07ff063a5b10b95d8f861e75152f7ad577 Mon Sep 17 00:00:00 2001 From: Erick Otenyo Date: Mon, 13 Nov 2023 16:21:47 +0300 Subject: [PATCH 3/3] Bump version --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 833442a..ccfe9ea 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = geomanager -version = 0.3.4 +version = 0.3.5 description = Wagtail based Geospatial Data Manager long_description = file:README.md long_description_content_type = text/markdown