Merge pull request #1531 from umap-project/home-highlighted

Introduce UMAP_HOME_FEED to control which maps are shown on the home page
This commit is contained in:
Yohan Boniface 2024-01-29 12:48:24 +01:00 committed by GitHub
commit caeb9d5b44
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 75 additions and 5 deletions

View file

@ -203,6 +203,13 @@ ready for production use (no backup, etc.)
Link to show on the header under the "Feedback and help" label. Link to show on the header under the "Feedback and help" label.
#### UMAP_HOME_FEED
Which feed to display on the home page. Three valid values:
- `"latest"`, which shows the latest maps (default)
- `"highlighted"`, which shows the maps that have been starred by a staff member
- `None`, which does not show any map on the home page
#### UMAP_MAPS_PER_PAGE #### UMAP_MAPS_PER_PAGE
How many maps to show in maps list, like search or home page. How many maps to show in maps list, like search or home page.

View file

@ -255,6 +255,7 @@ DATABASES = {"default": env.db(default="postgis://localhost:5432/umap")}
UMAP_DEFAULT_SHARE_STATUS = None UMAP_DEFAULT_SHARE_STATUS = None
UMAP_DEFAULT_EDIT_STATUS = None UMAP_DEFAULT_EDIT_STATUS = None
UMAP_DEFAULT_FEATURES_HAVE_OWNERS = False UMAP_DEFAULT_FEATURES_HAVE_OWNERS = False
UMAP_HOME_FEED = "latest"
UMAP_READONLY = env("UMAP_READONLY", default=False) UMAP_READONLY = env("UMAP_READONLY", default=False)
UMAP_GZIP = True UMAP_GZIP = True

View file

@ -10,7 +10,9 @@
</div> </div>
{% endif %} {% endif %}
<div class="wrapper"> <div class="wrapper">
<h2 class="section">{% blocktrans %}Get inspired, browse maps{% endblocktrans %}</h2> {% if maps %}
<div class="map_list row">{% include "umap/map_list.html" %}</div> <h2 class="section">{% blocktrans %}Get inspired, browse maps{% endblocktrans %}</h2>
<div class="map_list row">{% include "umap/map_list.html" %}</div>
{% endif %}
</div> </div>
{% endblock maincontent %} {% endblock maincontent %}

View file

@ -10,6 +10,7 @@ from django.urls import reverse
from django.utils.timezone import make_aware from django.utils.timezone import make_aware
from umap import VERSION from umap import VERSION
from umap.models import Map, Star
from umap.views import validate_url from umap.views import validate_url
from .base import MapFactory, UserFactory from .base import MapFactory, UserFactory
@ -391,3 +392,51 @@ def test_webmanifest(client):
}, },
] ]
} }
@pytest.mark.django_db
def test_home_feed(client, settings, user, tilelayer):
settings.UMAP_HOME_FEED = "latest"
staff = UserFactory(username="Staff", is_staff=True)
starred = MapFactory(
owner=user, name="A public map starred by staff", share_status=Map.PUBLIC
)
MapFactory(
owner=user, name="A public map not starred by staff", share_status=Map.PUBLIC
)
non_staff = MapFactory(
owner=user, name="A public map starred by non staff", share_status=Map.PUBLIC
)
private = MapFactory(
owner=user, name="A private map starred by staff", share_status=Map.PRIVATE
)
reserved = MapFactory(
owner=user, name="A reserved map starred by staff", share_status=Map.OPEN
)
Star.objects.create(by=staff, map=starred)
Star.objects.create(by=staff, map=private)
Star.objects.create(by=staff, map=reserved)
Star.objects.create(by=user, map=non_staff)
response = client.get(reverse("home"))
content = response.content.decode()
assert "A public map starred by staff" in content
assert "A public map not starred by staff" in content
assert "A public map starred by non staff" in content
assert "A private map starred by staff" not in content
assert "A reserved map starred by staff" not in content
settings.UMAP_HOME_FEED = "highlighted"
response = client.get(reverse("home"))
content = response.content.decode()
assert "A public map starred by staff" in content
assert "A public map not starred by staff" not in content
assert "A public map starred by non staff" not in content
assert "A private map starred by staff" not in content
assert "A reserved map starred by staff" not in content
settings.UMAP_HOME_FEED = None
response = client.get(reverse("home"))
content = response.content.decode()
assert "A public map starred by staff" not in content
assert "A public map not starred by staff" not in content
assert "A public map starred by non staff" not in content
assert "A private map starred by staff" not in content
assert "A reserved map starred by staff" not in content

View file

@ -121,13 +121,26 @@ class PublicMapsMixin(object):
maps = qs.order_by("-modified_at") maps = qs.order_by("-modified_at")
return maps return maps
def get_highlighted_maps(self):
staff = User.objects.filter(is_staff=True)
stars = Star.objects.filter(by__in=staff).values("map")
qs = Map.public.filter(pk__in=stars)
maps = qs.order_by("-modified_at")
return maps
class Home(PaginatorMixin, TemplateView, PublicMapsMixin): class Home(PaginatorMixin, TemplateView, PublicMapsMixin):
template_name = "umap/home.html" template_name = "umap/home.html"
list_template_name = "umap/map_list.html" list_template_name = "umap/map_list.html"
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
maps = self.get_public_maps() if settings.UMAP_HOME_FEED is None:
maps = []
elif settings.UMAP_HOME_FEED == "highlighted":
maps = self.get_highlighted_maps()
else:
maps = self.get_public_maps()
maps = self.paginate(maps, settings.UMAP_MAPS_PER_PAGE)
demo_map = None demo_map = None
if hasattr(settings, "UMAP_DEMO_PK"): if hasattr(settings, "UMAP_DEMO_PK"):
@ -143,8 +156,6 @@ class Home(PaginatorMixin, TemplateView, PublicMapsMixin):
except Map.DoesNotExist: except Map.DoesNotExist:
pass pass
maps = self.paginate(maps, settings.UMAP_MAPS_PER_PAGE)
return { return {
"maps": maps, "maps": maps,
"demo_map": demo_map, "demo_map": demo_map,