Skip to content

Commit

Permalink
Merge pull request #13 from wmo-raf/develop
Browse files Browse the repository at this point in the history
Fix cache and getting full urls
  • Loading branch information
erick-otenyo authored Nov 13, 2023
2 parents e87e6ea + 1eaa2e0 commit c2c2dfa
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 54 deletions.
23 changes: 23 additions & 0 deletions geomanager/decorators.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 4 additions & 6 deletions geomanager/models/raster_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}}"

Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion geomanager/models/tile_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions geomanager/models/vector_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}}"

Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion geomanager/models/wms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions geomanager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -46,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)

Expand All @@ -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/<int:user_id>', get_geomanager_user_profile,
Expand Down Expand Up @@ -126,8 +125,7 @@
name="raster_data_geostore_timeseries"),

# FeatureServ
path(r'api/feature-serv/<str:table_name>.geojson', GeoJSONPgTableView.as_view(),
name="feature_serv"),
path(r'api/feature-serv/<str:table_name>.geojson', GeoJSONPgTableView.as_view(), name="feature_serv"),

# Tiles GL
path(r'api/tile-gl/tile/<str:source_slug>/<int:z>/<int:x>/<int:y>.pbf', tile_gl, name="tile_gl"),
Expand Down
20 changes: 11 additions & 9 deletions geomanager/views/core.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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})
Expand All @@ -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})
Expand Down
7 changes: 3 additions & 4 deletions geomanager/views/nextjs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django_nextjs.render import render_nextjs_page_sync
from wagtailcache.cache import nocache_page
from wagtail.api.v2.utils import get_full_url
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()]
Expand All @@ -18,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})
Expand Down
16 changes: 12 additions & 4 deletions geomanager/views/raster_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
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
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 (
Expand Down Expand Up @@ -384,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")
Expand Down Expand Up @@ -416,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,
}
Expand Down Expand Up @@ -513,6 +515,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.
Expand Down Expand Up @@ -571,6 +574,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):
Expand Down Expand Up @@ -622,6 +626,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]
Expand All @@ -637,6 +642,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]
Expand All @@ -659,6 +665,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]
Expand All @@ -676,6 +683,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]
Expand Down
5 changes: 3 additions & 2 deletions geomanager/views/tile_gl.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]

Expand All @@ -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

Expand Down
Loading

0 comments on commit c2c2dfa

Please sign in to comment.