Add recent post cards to homepage

This commit is contained in:
Jake Howard 2024-02-10 14:56:26 +00:00
parent 0424c2dba2
commit c69d8d8329
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 138 additions and 36 deletions

View file

@ -1,17 +1,34 @@
body.page-homepage { body.page-homepage {
min-height: 100vh;
@include tablet {
height: 100vh; height: 100vh;
}
main { main {
background-repeat: no-repeat; background-repeat: no-repeat;
background-size: cover; background-size: cover;
background-position: center; background-position: center;
height: 100%; height: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
color: $white; color: $white;
margin-bottom: 0; margin-bottom: 0;
padding-bottom: 2rem;
display: flex;
flex-direction: column;
justify-content: space-evenly;
}
.top-section {
min-width: 90%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-bottom: 1rem;
@include mobile {
margin: 2rem;
}
} }
h1 { h1 {
@ -23,7 +40,7 @@ body.page-homepage {
min-width: 45%; min-width: 45%;
} }
.latest { .box {
padding: 0.25rem 0.5rem; padding: 0.25rem 0.5rem;
margin-top: 2rem; margin-top: 2rem;
background-color: color.adjust($dark, $alpha: -0.2); background-color: color.adjust($dark, $alpha: -0.2);
@ -59,4 +76,60 @@ body.page-homepage {
#to-top { #to-top {
display: none; display: none;
} }
.content-list {
width: 100%;
.card-image {
overflow: hidden;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
position: absolute;
transition: filter 0.25s;
filter: brightness(0.9);
}
p {
position: absolute;
bottom: 0;
color: $white;
padding: 0.5rem;
transition: background-color 0.25s;
width: 100%;
background-color: rgb(0 0 0 / 50%);
}
figure {
margin: 0;
text-align: initial;
}
a:hover {
img {
filter: unset;
}
p {
background-color: rgb(0 0 0 / 70%);
}
}
}
.recent-posts {
display: flex;
align-items: center;
flex-direction: column;
width: 100%;
flex-grow: unset;
margin-top: 2rem;
.box {
margin: 0 auto;
display: inline-block;
}
}
} }

View file

@ -1,8 +1,5 @@
from typing import Optional, Tuple
from django.db import models from django.db import models
from django.http.request import HttpRequest from django.http.request import HttpRequest
from django_cache_decorator import django_cache_decorator
from wagtail.admin.panels import FieldPanel from wagtail.admin.panels import FieldPanel
from wagtail.images import get_image_model_string from wagtail.images import get_image_model_string
from wagtail.images.models import Image from wagtail.images.models import Image
@ -12,20 +9,6 @@ from website.common.models import BasePage
from website.contrib.singleton_page.utils import SingletonPageCache from website.contrib.singleton_page.utils import SingletonPageCache
@django_cache_decorator(time=600)
def get_latest_blog_post() -> Optional[Tuple[str, str]]:
from website.blog.models import BlogPostPage
try:
latest_blog_post = (
BlogPostPage.objects.live().public().defer_streamfields().latest("date")
)
except BlogPostPage.DoesNotExist:
return None
return latest_blog_post.title, latest_blog_post.get_url()
class HomePage(BasePage, WagtailImageMetadataMixin): class HomePage(BasePage, WagtailImageMetadataMixin):
max_count = 1 max_count = 1
@ -55,9 +38,22 @@ class HomePage(BasePage, WagtailImageMetadataMixin):
return self.html_title return self.html_title
def get_context(self, request: HttpRequest) -> dict: def get_context(self, request: HttpRequest) -> dict:
from website.blog.models import BlogPostListPage, BlogPostPage
from website.search.models import SearchPage from website.search.models import SearchPage
context = super().get_context(request) context = super().get_context(request)
context["latest_blog_post"] = get_latest_blog_post() context["recent_posts"] = list(
BlogPostPage.objects.live()
.public()
.defer_streamfields()
.order_by("-date")[:7]
)
context["latest_blog_post"] = (
context["recent_posts"].pop(0) if context["recent_posts"] else None
)
context["search_page_url"] = SingletonPageCache.get_url(SearchPage, request) context["search_page_url"] = SingletonPageCache.get_url(SearchPage, request)
context["blog_post_list_url"] = SingletonPageCache.get_url(
BlogPostListPage, request
)
return context return context

View file

@ -4,6 +4,7 @@
{% block main %} {% block main %}
<main {% if page.image %}style="background-image: url({% image_url page.image 'width-1200' %})"{% endif %}> <main {% if page.image %}style="background-image: url({% image_url page.image 'width-1200' %})"{% endif %}>
<div class="top-section">
<div class="heading-wrapper"> <div class="heading-wrapper">
<h1>{{ page.heading }}</h1> <h1>{{ page.heading }}</h1>
{% if search_page_url %} {% if search_page_url %}
@ -14,11 +15,27 @@
</div> </div>
{% if latest_blog_post %} {% if latest_blog_post %}
<div class="box latest"> <div class="box latest is-size-5">
<strong>Latest Post</strong>: <strong>Latest Post</strong>:
<a href="{{ latest_blog_post.1 }}">{{ latest_blog_post.0 }}</a> <a href="{% pageurl latest_blog_post %}">{{ latest_blog_post.title }}</a>
&rarr; &rarr;
</div> </div>
{% endif %} {% endif %}
</div>
<section class="container content recent-posts">
<h2 class="has-text-centered has-text-white is-size-3">Recent Posts</h2>
<div class="columns content-list is-multiline">
{% for page in recent_posts %}
{% include "home/home_page_card.html" %}
{% endfor %}
</div>
{% if blog_post_list_url %}
<div class="box">
<a href="{{ blog_post_list_url }}">View more &rarr;</a>
</div>
{% endif %}
</section>
</main> </main>
{% endblock %} {% endblock %}

View file

@ -0,0 +1,16 @@
{% load wagtail_cache wagtailcore_tags %}
{% wagtailpagecache FRAGMENT_CACHE_TTL "homepage-card" %}
<div class="column is-one-third-widescreen is-half">
<div class="card">
<div class="card-image">
<a href="{% pageurl page %}">
<figure class="image is-16by9">
<img src="{{ page.list_image_url }}" alt="{{ page.hero_image_alt }}" loading="lazy" decoding="async" referrerpolicy="no-referrer" />
<p class="has-text-weight-light">{{ page.title }}</p>
</figure>
</a>
</div>
</div>
</div>
{% endwagtailpagecache %}