From d3d9572ad69bbb6a3390bd6ce6954592995f8a59 Mon Sep 17 00:00:00 2001 From: Lacey Williams Henschel Date: Thu, 17 Aug 2023 15:22:50 -0700 Subject: [PATCH] - Add featured field to Library model - Add featured library to context of home page - Add the featured library to the homepage template - Return a random library if no library is marked as featured - Add link to library detail page - Add default author image - Add hover text with author name - Limit "random" featured library to libraries within the most recent Boost version --- ak/tests/test_default_pages.py | 4 +- ak/views.py | 17 ++++++- libraries/migrations/0013_library_featured.py | 21 +++++++++ libraries/models.py | 5 ++ templates/homepage.html | 47 ++++++++++++------- 5 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 libraries/migrations/0013_library_featured.py diff --git a/ak/tests/test_default_pages.py b/ak/tests/test_default_pages.py index d811a49a..e5da7444 100644 --- a/ak/tests/test_default_pages.py +++ b/ak/tests/test_default_pages.py @@ -4,7 +4,7 @@ import pytest from django.test.utils import override_settings -def test_homepage(version, tp): +def test_homepage(library, version, tp): """Ensure we can hit the homepage""" # Use any page that is named 'home' otherwise use / url = tp.reverse("home") @@ -15,12 +15,12 @@ def test_homepage(version, tp): assert "entries" in response.context assert "latest_version" in response.context assert response.context["latest_version"] == version + assert "featured_library" in response.context def test_homepage_beta(db, tp): """Ensure we can hit the beta homepage""" url = tp.reverse("home-beta") - tp.get_check_200(url) diff --git a/ak/views.py b/ak/views.py index 704cef33..eab67c0d 100644 --- a/ak/views.py +++ b/ak/views.py @@ -22,9 +22,24 @@ class HomepageView(TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["entries"] = Entry.objects.published().order_by("-publish_at")[:3] - context["latest_version"] = Version.objects.most_recent() + latest_version = Version.objects.most_recent() + context["latest_version"] = latest_version + context["featured_library"] = self.get_featured_library(latest_version) return context + def get_featured_library(self, latest_version): + library = Library.objects.filter(featured=True).first() + + # If we don't have a featured library, return a random library + if not library: + library = ( + Library.objects.filter(library_version__version=latest_version) + .order_by("?") + .first() + ) + + return library + class HomepageBetaView(TemplateView): """ diff --git a/libraries/migrations/0013_library_featured.py b/libraries/migrations/0013_library_featured.py new file mode 100644 index 00000000..70d74dc4 --- /dev/null +++ b/libraries/migrations/0013_library_featured.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.2 on 2023-08-17 22:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("libraries", "0012_libraryversion_documentation_url"), + ] + + operations = [ + migrations.AddField( + model_name="library", + name="featured", + field=models.BooleanField( + db_index=True, + default=False, + help_text="Should this library be featured on the home page?", + ), + ), + ] diff --git a/libraries/models.py b/libraries/models.py index b8a47e0d..60a04a7a 100644 --- a/libraries/models.py +++ b/libraries/models.py @@ -134,6 +134,11 @@ class Library(models.Model): closed_prs_per_month = models.IntegerField(blank=True, null=True) open_issues = models.IntegerField(blank=True, null=True) commits_per_release = models.IntegerField(blank=True, null=True) + featured = models.BooleanField( + default=False, + db_index=True, + help_text="Should this library be featured on the home page?", + ) class Meta: verbose_name_plural = "Libraries" diff --git a/templates/homepage.html b/templates/homepage.html index 3ceead7f..629688cf 100644 --- a/templates/homepage.html +++ b/templates/homepage.html @@ -53,26 +53,37 @@
-
- library spotlight -
-

Boost.JSON

-

Released 4/5/22

-

Boost.JSON is a portable C++ library which provides containers and algorithms that implment - JavaScript Object Notation, or simply "JSON", a lightweight data-interchange format.

-
Authors user
-
-
- - Download - + {% if featured_library %} +
+ library spotlight
-
- - Version Details - +

{{ featured_library.name }}

+

{% if featured_library.first_github_tag_date %}Released {{ featured_library.first_github_tag_date|date:"m/d/y"}}{% endif %}

+

{{ featured_library.description }}

+
+ {% if featured_library.authors %} + Authors + {% for author in featured_library.authors.all %} + {% if author.image %} + Photo of {{ author.get_full_name }} + {% else %} + + {% endif %} + {% endfor %} + {% endif %}
+ -
+ {% endif %}