improve location matching and fix some performance issues as well
This commit is contained in:
parent
f5d9b13d75
commit
e59932f2b2
5 changed files with 20 additions and 6 deletions
|
@ -440,7 +440,7 @@ class LocationViewSet(LocationViewSetBase):
|
||||||
else:
|
else:
|
||||||
locations = visible_locations_for_request(request).values()
|
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),
|
geometry=geometry and MapdataViewSet.can_access_geometry(request, obj),
|
||||||
simple_geometry=True)
|
simple_geometry=True)
|
||||||
for obj in locations)
|
for obj in locations)
|
||||||
|
|
|
@ -77,6 +77,10 @@ class TitledMixin(SerializableMixin, models.Model):
|
||||||
result['title'] = self.title
|
result['title'] = self.title
|
||||||
return result
|
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):
|
def details_display(self, **kwargs):
|
||||||
result = super().details_display(**kwargs)
|
result = super().details_display(**kwargs)
|
||||||
for lang, title in sorted(self.titles.items(), key=lambda item: item[0] != get_language()):
|
for lang, title in sorted(self.titles.items(), key=lambda item: item[0] != get_language()):
|
||||||
|
|
|
@ -104,16 +104,21 @@ class Location(LocationSlug, AccessRestrictionMixin, TitledMixin, models.Model):
|
||||||
result = super().serialize(detailed=detailed, **kwargs)
|
result = super().serialize(detailed=detailed, **kwargs)
|
||||||
if not detailed:
|
if not detailed:
|
||||||
fields = ('id', 'type', 'slug', 'title', 'subtitle', 'icon', 'point', 'bounds', 'grid_square',
|
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}
|
result = {name: result[name] for name in fields if name in result}
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _serialize(self, **kwargs):
|
def _serialize(self, search=False, **kwargs):
|
||||||
result = super()._serialize(**kwargs)
|
result = super()._serialize(**kwargs)
|
||||||
result['subtitle'] = str(self.subtitle)
|
result['subtitle'] = str(self.subtitle)
|
||||||
result['icon'] = self.get_icon()
|
result['icon'] = self.get_icon()
|
||||||
result['can_search'] = self.can_search
|
result['can_search'] = self.can_search
|
||||||
result['can_describe'] = 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
|
return result
|
||||||
|
|
||||||
def details_display(self, **kwargs):
|
def details_display(self, **kwargs):
|
||||||
|
|
|
@ -40,11 +40,16 @@ def locations_for_request(request) -> Mapping[int, LocationSlug]:
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
condition &= model.q_for_request(request, prefix=related_name + '__')
|
condition &= model.q_for_request(request, prefix=related_name + '__')
|
||||||
conditions.append(condition)
|
conditions.append(condition)
|
||||||
|
locations = locations.select_related(
|
||||||
|
related_name + '__label_settings'
|
||||||
|
).prefetch_related(
|
||||||
|
related_name + '__redirects'
|
||||||
|
)
|
||||||
locations = locations.filter(reduce(operator.or_, conditions))
|
locations = locations.filter(reduce(operator.or_, conditions))
|
||||||
locations.select_related('redirect', 'locationgroups__category')
|
locations = locations.select_related('redirect', 'locationgroups__category')
|
||||||
|
|
||||||
# prefetch locationgroups
|
# 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):
|
for model in get_submodels(SpecificLocation):
|
||||||
locations = locations.prefetch_related(Prefetch(model._meta.default_related_name + '__groups',
|
locations = locations.prefetch_related(Prefetch(model._meta.default_related_name + '__groups',
|
||||||
queryset=base_qs))
|
queryset=base_qs))
|
||||||
|
|
|
@ -115,7 +115,7 @@ c3nav = {
|
||||||
location.elem = c3nav._build_location_html(location);
|
location.elem = c3nav._build_location_html(location);
|
||||||
location.title_words = location.title.toLowerCase().split(/\s+/);
|
location.title_words = location.title.toLowerCase().split(/\s+/);
|
||||||
location.subtitle_words = location.subtitle.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.push(location);
|
||||||
locations_by_id[location.id] = location;
|
locations_by_id[location.id] = location;
|
||||||
if (location.point && location.label_settings) {
|
if (location.point && location.label_settings) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue