better subtitles for location groups and more caching in query set
This commit is contained in:
parent
966b554169
commit
8beb6f9e8b
2 changed files with 36 additions and 7 deletions
|
@ -262,13 +262,34 @@ class LocationViewSet(RetrieveModelMixin, GenericViewSet):
|
|||
AccessPermission.cache_key_for_request(self.request)
|
||||
)
|
||||
queryset = cache.get(queryset_cache_key, None)
|
||||
if queryset is None:
|
||||
if queryset is None or 1:
|
||||
queryset = self.get_queryset(mode=('searchable' if searchable else 'searchable-describe'))
|
||||
cache.set(queryset_cache_key, queryset, 300)
|
||||
|
||||
queryset = (obj.get_child() for obj in queryset)
|
||||
if searchable:
|
||||
queryset = sorted(queryset, key=operator.attrgetter('order'), reverse=True)
|
||||
queryset = tuple(obj.get_child() for obj in queryset)
|
||||
|
||||
if searchable:
|
||||
queryset = sorted(queryset, key=operator.attrgetter('order'), reverse=True)
|
||||
else:
|
||||
queryset = tuple(queryset)
|
||||
|
||||
# add locations to groups
|
||||
locationgroups = {obj.pk: obj for obj in queryset if isinstance(obj, LocationGroup)}
|
||||
for group in locationgroups.values():
|
||||
group.locations = []
|
||||
for obj in queryset:
|
||||
if not isinstance(obj, SpecificLocation):
|
||||
continue
|
||||
for group in obj.groups.all():
|
||||
group = locationgroups.get(group.pk, None)
|
||||
if group is not None:
|
||||
group.locations.append(obj)
|
||||
|
||||
# precache cached properties
|
||||
for obj in queryset:
|
||||
# noinspection PyStatementEffect
|
||||
obj.subtitle, obj.order
|
||||
|
||||
cache.set(queryset_cache_key, queryset, 300)
|
||||
|
||||
result = tuple(obj.serialize(include_type=True, detailed=detailed, geometry=geometry) for obj in queryset)
|
||||
cache.set(cache_key, result, 300)
|
||||
|
|
|
@ -5,7 +5,9 @@ from django.apps import apps
|
|||
from django.db import models
|
||||
from django.db.models import Prefetch
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.text import format_lazy
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import ungettext_lazy
|
||||
|
||||
from c3nav.mapdata.models.access import AccessRestrictionMixin
|
||||
from c3nav.mapdata.models.base import SerializableMixin, TitledMixin
|
||||
|
@ -87,7 +89,7 @@ class Location(LocationSlug, AccessRestrictionMixin, TitledMixin, models.Model):
|
|||
|
||||
def _serialize(self, **kwargs):
|
||||
result = super()._serialize(**kwargs)
|
||||
result['subtitle'] = self.subtitle
|
||||
result['subtitle'] = str(self.subtitle)
|
||||
result['can_search'] = self.can_search
|
||||
result['can_describe'] = self.can_search
|
||||
return result
|
||||
|
@ -275,7 +277,13 @@ class LocationGroup(Location, models.Model):
|
|||
|
||||
@property
|
||||
def subtitle(self):
|
||||
return self.category.title
|
||||
result = self.category.title
|
||||
if hasattr(self, 'locations'):
|
||||
return format_lazy(_('{category_title}, {num_locations}'),
|
||||
category_title=result,
|
||||
num_locations=(ungettext_lazy('%(num)d location', '%(num)d locations', 'num') %
|
||||
{'num': len(self.locations)}))
|
||||
return result
|
||||
|
||||
@cached_property
|
||||
def order(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue