Merge pull request #1184 from umap-project/empty-search

Display latest created maps on empty search
This commit is contained in:
David Larlet 2023-07-01 10:21:35 -04:00 committed by GitHub
commit a4759763e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 13 deletions

View file

@ -2,9 +2,9 @@
{% load i18n %} {% load i18n %}
{% block maincontent %} {% block maincontent %}
{% include "umap/search_bar.html" %} {% include "umap/search_bar.html" %}
{% if q %} <div class="wrapper">
<div class="wrapper"> <div class="map_list row">
<div class="map_list row"> {% if q %}
{% if maps %} {% if maps %}
<h2> <h2>
{% blocktranslate count counter=count %} {% blocktranslate count counter=count %}
@ -17,7 +17,10 @@
{% else %} {% else %}
<h2>{% trans "No map found." %}</h2> <h2>{% trans "No map found." %}</h2>
{% endif %} {% endif %}
</div> {% else %}
<h2>{% trans "Latest created maps" %}</h2>
{% include "umap/map_list.html" with prefix="search_map" %}
{% endif %}
</div> </div>
{% endif %} </div>
{% endblock maincontent %} {% endblock maincontent %}

View file

@ -95,11 +95,9 @@ class PaginatorMixin(object):
return qs return qs
class Home(TemplateView, PaginatorMixin): class PublicMapsMixin(object):
template_name = "umap/home.html"
list_template_name = "umap/map_list.html"
def get_context_data(self, **kwargs): def get_public_maps(self):
qs = Map.public qs = Map.public
if ( if (
settings.UMAP_EXCLUDE_DEFAULT_MAPS settings.UMAP_EXCLUDE_DEFAULT_MAPS
@ -107,6 +105,17 @@ class Home(TemplateView, PaginatorMixin):
): ):
# Unsupported query type for sqlite. # Unsupported query type for sqlite.
qs = qs.filter(center__distance_gt=(DEFAULT_CENTER, D(km=1))) qs = qs.filter(center__distance_gt=(DEFAULT_CENTER, D(km=1)))
maps = qs.order_by("-modified_at")
return maps
class Home(TemplateView, PublicMapsMixin, PaginatorMixin):
template_name = "umap/home.html"
list_template_name = "umap/map_list.html"
def get_context_data(self, **kwargs):
maps = self.get_public_maps()
demo_map = None demo_map = None
if hasattr(settings, "UMAP_DEMO_PK"): if hasattr(settings, "UMAP_DEMO_PK"):
try: try:
@ -114,7 +123,8 @@ class Home(TemplateView, PaginatorMixin):
except Map.DoesNotExist: except Map.DoesNotExist:
pass pass
else: else:
qs = qs.exclude(id=demo_map.pk) maps = maps.exclude(id=demo_map.pk)
showcase_map = None showcase_map = None
if hasattr(settings, "UMAP_SHOWCASE_PK"): if hasattr(settings, "UMAP_SHOWCASE_PK"):
try: try:
@ -122,8 +132,8 @@ class Home(TemplateView, PaginatorMixin):
except Map.DoesNotExist: except Map.DoesNotExist:
pass pass
else: else:
qs = qs.exclude(id=showcase_map.pk) maps = maps.exclude(id=showcase_map.pk)
maps = qs.order_by("-modified_at")[:50]
maps = self.paginate(maps, settings.UMAP_MAPS_PER_PAGE) maps = self.paginate(maps, settings.UMAP_MAPS_PER_PAGE)
return { return {
@ -206,7 +216,7 @@ class UserStars(UserMaps):
user_stars = UserStars.as_view() user_stars = UserStars.as_view()
class Search(TemplateView, PaginatorMixin): class Search(TemplateView, PublicMapsMixin, PaginatorMixin):
template_name = "umap/search.html" template_name = "umap/search.html"
list_template_name = "umap/map_list.html" list_template_name = "umap/map_list.html"
@ -223,6 +233,8 @@ class Search(TemplateView, PaginatorMixin):
qs = qs.filter(share_status=Map.PUBLIC).order_by("-modified_at") qs = qs.filter(share_status=Map.PUBLIC).order_by("-modified_at")
qs_count = qs.count() qs_count = qs.count()
results = self.paginate(qs) results = self.paginate(qs)
else:
results = self.get_public_maps()[:settings.UMAP_MAPS_PER_SEARCH]
kwargs.update({"maps": results, "count": qs_count, "q": q}) kwargs.update({"maps": results, "count": qs_count, "q": q})
return kwargs return kwargs