dynamic location as a starting point
This commit is contained in:
parent
cca263c605
commit
1ecdc89a68
7 changed files with 63 additions and 13 deletions
|
@ -470,6 +470,22 @@ class CustomLocationProxyMixin:
|
|||
def get_custom_location(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
return self.get_custom_location() is not None
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self.get_custom_location().x
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self.get_custom_location().y
|
||||
|
||||
@property
|
||||
def level(self):
|
||||
return self.get_custom_location().level
|
||||
|
||||
def serialize_position(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
@ -550,6 +566,9 @@ class Position(CustomLocationProxyMixin, models.Model):
|
|||
coordinates_id = models.CharField(_('coordinates'), null=True, max_length=48)
|
||||
api_secret = models.CharField(_('api secret'), max_length=64, default=get_position_api_secret)
|
||||
|
||||
can_search = True
|
||||
can_describe = False
|
||||
|
||||
coordinates = LocationById()
|
||||
|
||||
class Meta:
|
||||
|
@ -583,8 +602,8 @@ class Position(CustomLocationProxyMixin, models.Model):
|
|||
custom_location = self.get_custom_location()
|
||||
if custom_location is None:
|
||||
return {
|
||||
'id': self.secret,
|
||||
'slug': self.secret,
|
||||
'id': 'p:%s' % self.secret,
|
||||
'slug': 'p:%s' % self.secret,
|
||||
'available': False,
|
||||
'icon': 'my_location',
|
||||
'title': self.name,
|
||||
|
@ -606,6 +625,18 @@ class Position(CustomLocationProxyMixin, models.Model):
|
|||
})
|
||||
return result
|
||||
|
||||
@property
|
||||
def slug(self):
|
||||
return 'p:%s' % self.secret
|
||||
|
||||
def serialize(self, *args, **kwargs):
|
||||
return self.serialize_position()
|
||||
|
||||
def get_geometry(self, *args, **kwargs):
|
||||
return None
|
||||
|
||||
level_id = None
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
with transaction.atomic():
|
||||
super().save(*args, **kwargs)
|
||||
|
|
|
@ -4,7 +4,7 @@ import re
|
|||
from collections import OrderedDict
|
||||
from functools import reduce
|
||||
from itertools import chain
|
||||
from typing import List, Mapping, Optional
|
||||
from typing import List, Mapping, Optional, Union
|
||||
|
||||
from django.apps import apps
|
||||
from django.db.models import Prefetch, Q
|
||||
|
@ -18,7 +18,7 @@ from c3nav.mapdata.models.access import AccessPermission
|
|||
from c3nav.mapdata.models.geometry.base import GeometryMixin
|
||||
from c3nav.mapdata.models.geometry.level import LevelGeometryMixin, Space
|
||||
from c3nav.mapdata.models.geometry.space import SpaceGeometryMixin
|
||||
from c3nav.mapdata.models.locations import LocationRedirect, LocationSlug, SpecificLocation
|
||||
from c3nav.mapdata.models.locations import LocationRedirect, LocationSlug, Position, SpecificLocation
|
||||
from c3nav.mapdata.utils.cache.local import LocalCacheProxy
|
||||
from c3nav.mapdata.utils.models import get_submodels
|
||||
|
||||
|
@ -206,12 +206,18 @@ def get_location_by_id_for_request(pk, request):
|
|||
if isinstance(pk, str):
|
||||
if pk.isdigit():
|
||||
pk = int(pk)
|
||||
elif pk.startswith('p:'):
|
||||
try:
|
||||
# return immediately, don't cache for obvious reasons
|
||||
return Position.objects.get(secret=pk[2:])
|
||||
except Position.DoesNotExist:
|
||||
return None
|
||||
else:
|
||||
return get_custom_location_for_request(pk, request)
|
||||
return locations_for_request(request).get(pk)
|
||||
|
||||
|
||||
def get_location_by_slug_for_request(slug: str, request) -> Optional[LocationSlug]:
|
||||
def get_location_by_slug_for_request(slug: str, request) -> Optional[Union[LocationSlug, Position]]:
|
||||
cache_key = 'mapdata:location:by_slug:%s:%s' % (AccessPermission.cache_key_for_request(request), slug)
|
||||
location = proxied_cache.get(cache_key, None)
|
||||
if location is not None:
|
||||
|
@ -221,6 +227,12 @@ def get_location_by_slug_for_request(slug: str, request) -> Optional[LocationSlu
|
|||
location = get_custom_location_for_request(slug, request)
|
||||
if location is None:
|
||||
return None
|
||||
elif slug.startswith('p:'):
|
||||
try:
|
||||
# return immediately, don't cache for obvious reasons
|
||||
return Position.objects.get(secret=slug[2:])
|
||||
except Position.DoesNotExist:
|
||||
return None
|
||||
elif ':' in slug:
|
||||
code, pk = slug.split(':', 1)
|
||||
model_name = LocationSlug.LOCATION_TYPE_BY_CODE.get(code)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue