some more preview image and embed card related stuff

This commit is contained in:
Gwendolyn 2023-12-27 13:13:32 +01:00
parent 42d148441c
commit f498592390
4 changed files with 29 additions and 11 deletions

View file

@ -655,10 +655,17 @@ class Position(CustomLocationProxyMixin, models.Model):
})
return result
@property
def title(self):
return self.name
@property
def slug(self):
return 'p:%s' % self.secret
def get_slug(self):
return self.slug
def serialize(self, *args, **kwargs):
return {
'dynamic': True,

View file

@ -91,7 +91,6 @@ def cache_preview(request, key, last_update, render_fn):
import binascii
import hashlib
base_cache_key = build_base_cache_key(last_update)
# TODO: what's SECRET_TILE_KEY for, should we also have SECRET_PREVIEW_KEY, or can we leave it out completely?
preview_etag = '"' + binascii.b2a_base64(hashlib.sha256(
('%s:%s:%s' % (key, base_cache_key, settings.SECRET_TILE_KEY[:26])).encode()
).digest()[:15], newline=False).decode() + '"'
@ -144,6 +143,7 @@ def preview_location(request, slug):
from c3nav.mapdata.utils.locations import CustomLocation
from c3nav.mapdata.models.geometry.base import GeometryMixin
from c3nav.mapdata.models import LocationGroup
from c3nav.mapdata.models.locations import Position
location = check_location(slug, None)
highlight = True
@ -168,6 +168,12 @@ def preview_location(request, slug):
level = counts.most_common(1)[0][0]
geometries = [loc.geometry for loc in location.locations if loc.level_id == level]
highlight = True
elif isinstance(location, Position):
loc = location.get_custom_location()
if not loc:
raise Http404
geometries = [Point(loc.x, loc.y)]
level = loc.level.pk
else:
raise NotImplementedError(f'location type {type(location)} is not supported yet')

View file

@ -170,6 +170,8 @@ INITIAL_BOUNDS = config.get('c3nav', 'initial_bounds', fallback='').split(' ')
GRID_ROWS = config.get('c3nav', 'grid_rows', fallback=None)
GRID_COLS = config.get('c3nav', 'grid_cols', fallback=None)
MAIN_PREVIEW_SLUG = config.get('c3nav', 'main_preview_slug', fallback='level-0')
if len(INITIAL_BOUNDS) == 4:
try:
INITIAL_BOUNDS = tuple(float(i) for i in INITIAL_BOUNDS)

View file

@ -136,25 +136,27 @@ def map_index(request, mode=None, slug=None, slug2=None, details=None, options=N
metadata = {
'title': _('Route from %s to %s') % (origin.title, destination.title),
'preview_img_url': request.build_absolute_uri(reverse('mapdata.preview.route', kwargs={
'slug': slug,
'slug2': slug2,
'slug': origin.get_slug(),
'slug2': destination.get_slug(),
})),
'canonical_url': request.build_absolute_uri(reverse('site.index', kwargs={
'mode': 'r',
'slug': slug,
'slug2': slug2,
'slug': origin.get_slug(),
'slug2': destination.get_slug(),
'details': False,
'options': False,
})),
}
elif destination is not None or origin is not None:
loc_slug = destination.get_slug() if destination else origin.get_slug()
metadata = {
'title': destination.title,
'description': destination.subtitle,
'preview_img_url': request.build_absolute_uri(reverse('mapdata.preview.location', kwargs={'slug': slug})),
'description': destination.subtitle if hasattr(destination, 'subtitle') else None,
'preview_img_url': request.build_absolute_uri(reverse('mapdata.preview.location',
kwargs={'slug': loc_slug})),
'canonical_url': request.build_absolute_uri(reverse('site.index', kwargs={
'mode': 'l',
'slug': slug,
'slug': loc_slug,
'nearby': False,
'details': False,
})),
@ -163,7 +165,8 @@ def map_index(request, mode=None, slug=None, slug2=None, details=None, options=N
metadata = {
'title': 'c3nav',
# 'description': '',
# 'preview_img_url': '',
'preview_img_url': request.build_absolute_uri(reverse('mapdata.preview.location',
kwargs={'slug': settings.MAIN_PREVIEW_SLUG})),
'canonical_url': request.build_absolute_uri('/'),
}
else:
@ -217,8 +220,8 @@ def qr_code_etag(request, path):
@etag(qr_code_etag)
@cache_control(max_age=3600)
def qr_code(request, path):
data = (request.build_absolute_uri('/'+path.removeprefix('/')) +
('?'+request.META['QUERY_STRING'] if request.META['QUERY_STRING'] else ''))
data = (request.build_absolute_uri('/' + path.removeprefix('/')) +
('?' + request.META['QUERY_STRING'] if request.META['QUERY_STRING'] else ''))
if len(data) > 256:
return HttpResponseBadRequest()