order locations by relevance
This commit is contained in:
parent
0cfae778f0
commit
c35d833640
2 changed files with 23 additions and 3 deletions
|
@ -213,7 +213,7 @@ class LocationGroupViewSet(MapdataViewSet):
|
|||
class LocationViewSet(RetrieveModelMixin, GenericViewSet):
|
||||
"""
|
||||
only accesses locations that have can_search or can_describe set to true.
|
||||
add ?search to only show locations with can_search set to true
|
||||
add ?search to only show locations with can_search set to true ordered by relevance
|
||||
add ?detailed to show all attributes
|
||||
add ?geometry to show geometries
|
||||
/{id}/ add ?show_redirect=1 to suppress redirects and show them as JSON.
|
||||
|
@ -266,8 +266,11 @@ class LocationViewSet(RetrieveModelMixin, GenericViewSet):
|
|||
queryset = self.get_queryset(mode=('search' if search else 'search-describe'))
|
||||
cache.set(queryset_cache_key, queryset, 300)
|
||||
|
||||
result = tuple(obj.get_child().serialize(include_type=True, detailed=detailed, geometry=geometry)
|
||||
for obj in queryset)
|
||||
queryset = (obj.get_child() for obj in queryset)
|
||||
if search:
|
||||
queryset = sorted(queryset, key=operator.attrgetter('order'), reverse=True)
|
||||
|
||||
result = tuple(obj.serialize(include_type=True, detailed=detailed, geometry=geometry) for obj in queryset)
|
||||
cache.set(cache_key, result, 300)
|
||||
|
||||
return Response(result)
|
||||
|
|
|
@ -3,6 +3,8 @@ from contextlib import suppress
|
|||
|
||||
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.translation import ugettext_lazy as _
|
||||
|
||||
from c3nav.mapdata.models.access import AccessRestrictionMixin
|
||||
|
@ -54,6 +56,10 @@ class LocationSlug(SerializableMixin, models.Model):
|
|||
result['slug'] = self.get_slug()
|
||||
return result
|
||||
|
||||
@cached_property
|
||||
def order(self):
|
||||
return (-1, 0)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Location with Slug')
|
||||
verbose_name_plural = _('Location with Slug')
|
||||
|
@ -160,6 +166,13 @@ class SpecificLocation(Location, models.Model):
|
|||
groups = tuple(self.groups.all())
|
||||
return groups[0].title if groups else str(self.__class__._meta.verbose_name)
|
||||
|
||||
@cached_property
|
||||
def order(self):
|
||||
groups = tuple(self.groups.all())
|
||||
if not groups:
|
||||
return (0, 0)
|
||||
return (0, groups[0].category.priority, groups[0].priority)
|
||||
|
||||
|
||||
class LocationGroupCategory(TitledMixin, models.Model):
|
||||
name = models.SlugField(_('Name'), unique=True, max_length=50)
|
||||
|
@ -260,6 +273,10 @@ class LocationGroup(Location, models.Model):
|
|||
for obj in query:
|
||||
obj.register_change(force=True)
|
||||
|
||||
@cached_property
|
||||
def order(self):
|
||||
return (self.category.priority, self.priority)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if (self.orig_color != self.color or
|
||||
self.priority != self.orig_priority or
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue