diff --git a/src/c3nav/mapdata/api.py b/src/c3nav/mapdata/api.py index 10497ed8..d1ea952e 100644 --- a/src/c3nav/mapdata/api.py +++ b/src/c3nav/mapdata/api.py @@ -440,7 +440,7 @@ class LocationViewSet(LocationViewSetBase): else: locations = visible_locations_for_request(request).values() - result = tuple(obj.serialize(include_type=True, detailed=detailed, + result = tuple(obj.serialize(include_type=True, detailed=detailed, search=searchable, geometry=geometry and MapdataViewSet.can_access_geometry(request, obj), simple_geometry=True) for obj in locations) diff --git a/src/c3nav/mapdata/models/base.py b/src/c3nav/mapdata/models/base.py index e2380dae..f6b4e4c9 100644 --- a/src/c3nav/mapdata/models/base.py +++ b/src/c3nav/mapdata/models/base.py @@ -77,6 +77,10 @@ class TitledMixin(SerializableMixin, models.Model): result['title'] = self.title return result + @property + def other_titles(self): + return tuple(title for lang, title in self.titles.items() if lang != get_language()) + def details_display(self, **kwargs): result = super().details_display(**kwargs) for lang, title in sorted(self.titles.items(), key=lambda item: item[0] != get_language()): diff --git a/src/c3nav/mapdata/models/locations.py b/src/c3nav/mapdata/models/locations.py index 2480e099..5174c949 100644 --- a/src/c3nav/mapdata/models/locations.py +++ b/src/c3nav/mapdata/models/locations.py @@ -104,16 +104,21 @@ class Location(LocationSlug, AccessRestrictionMixin, TitledMixin, models.Model): result = super().serialize(detailed=detailed, **kwargs) if not detailed: fields = ('id', 'type', 'slug', 'title', 'subtitle', 'icon', 'point', 'bounds', 'grid_square', - 'locations', 'on_top_of', 'label_settings', 'label_override') + 'locations', 'on_top_of', 'label_settings', 'label_override', 'add_search') result = {name: result[name] for name in fields if name in result} return result - def _serialize(self, **kwargs): + def _serialize(self, search=False, **kwargs): result = super()._serialize(**kwargs) result['subtitle'] = str(self.subtitle) result['icon'] = self.get_icon() result['can_search'] = self.can_search result['can_describe'] = self.can_search + if search: + result['add_search'] = ' '.join(( + *(redirect.slug for redirect in self.redirects.all()), + *self.other_titles, + )) return result def details_display(self, **kwargs): diff --git a/src/c3nav/mapdata/utils/locations.py b/src/c3nav/mapdata/utils/locations.py index 453b3b0c..4a74134c 100644 --- a/src/c3nav/mapdata/utils/locations.py +++ b/src/c3nav/mapdata/utils/locations.py @@ -40,11 +40,16 @@ def locations_for_request(request) -> Mapping[int, LocationSlug]: # noinspection PyUnresolvedReferences condition &= model.q_for_request(request, prefix=related_name + '__') conditions.append(condition) + locations = locations.select_related( + related_name + '__label_settings' + ).prefetch_related( + related_name + '__redirects' + ) locations = locations.filter(reduce(operator.or_, conditions)) - locations.select_related('redirect', 'locationgroups__category') + locations = locations.select_related('redirect', 'locationgroups__category') # prefetch locationgroups - base_qs = LocationGroup.qs_for_request(request).select_related('category') + base_qs = LocationGroup.qs_for_request(request).select_related('category', 'label_settings') for model in get_submodels(SpecificLocation): locations = locations.prefetch_related(Prefetch(model._meta.default_related_name + '__groups', queryset=base_qs)) diff --git a/src/c3nav/site/static/site/js/c3nav.js b/src/c3nav/site/static/site/js/c3nav.js index d33360d1..2f32fa2d 100644 --- a/src/c3nav/site/static/site/js/c3nav.js +++ b/src/c3nav/site/static/site/js/c3nav.js @@ -115,7 +115,7 @@ c3nav = { location.elem = c3nav._build_location_html(location); location.title_words = location.title.toLowerCase().split(/\s+/); location.subtitle_words = location.subtitle.toLowerCase().split(/\s+/); - location.match = ' ' + location.title_words.join(' ') + ' ' + location.subtitle_words.join(' ') + ' ' + location.slug; + location.match = ' ' + location.title_words.join(' ') + ' ' + location.subtitle_words.join(' ') + ' ' + location.slug + ' ' + location.add_search; locations.push(location); locations_by_id[location.id] = location; if (location.point && location.label_settings) {