Skip to content

Commit

Permalink
INTR-318 Event listing page updates and filtering (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
CamLamb authored Oct 9, 2024
1 parent 458c769 commit ccd8372
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 25 deletions.
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ opentelemetry-instrumentation-wsgi = "^0.43b0"
opentelemetry-propagator-aws-xray = "^1.0.1"
opentelemetry-sdk-extension-aws = "^2.0.1"
wagtail-orderable = "^1.2.0"
django-filter = "^24.3"
django-csp = "^3.8"

[tool.poetry.group.dev.dependencies]
Expand Down
6 changes: 5 additions & 1 deletion src/core/templates/dwds_content.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
{% block pre_content %}
{% endblock pre_content %}
{% block page_title %}
<h1>{{ page.title }}</h1>
{% if page_title %}
<h1>{{ page_title }}</h1>
{% else %}
<h1>{{ page.title }}</h1>
{% endif %}
{% if page.description %}<p class="text-xlarge">{{ page.description }}</p>{% endif %}
{% endblock page_title %}
{% block bookmark %}
Expand Down
40 changes: 40 additions & 0 deletions src/events/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import django_filters
from django import forms

from events import types
from peoplefinder.services.uk_staff_locations import UkStaffLocationService


EVENT_TYPE_ALL_VALUE = ""
EVENT_TYPE_CHOICES = [
(EVENT_TYPE_ALL_VALUE, "All types"),
(types.EventType.IN_PERSON, "In person"),
(types.EventType.ONLINE, "Online"),
]


class EventsFilters(django_filters.FilterSet):
event_type = django_filters.ChoiceFilter(
method="event_format_filter",
choices=EVENT_TYPE_CHOICES,
widget=forms.widgets.RadioSelect,
empty_label=None,
)

location = django_filters.ChoiceFilter(
field_name="location__city",
choices=[
(c, c) for c in UkStaffLocationService().get_uk_staff_location_cities()
],
lookup_expr="exact",
widget=forms.widgets.Select,
)

def event_format_filter(self, queryset, name, value):
if value == EVENT_TYPE_ALL_VALUE:
return queryset

event_types = [types.EventType.HYBRID]
event_types.append(types.EventType(value))

return queryset.filter(event_type__in=event_types)
76 changes: 72 additions & 4 deletions src/events/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from datetime import timedelta
from datetime import datetime, timedelta

from dateutil.relativedelta import relativedelta
from django.db import models
from django.http import Http404
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils import timezone
from wagtail.admin.panels import FieldPanel, FieldRowPanel, MultiFieldPanel
from wagtail.contrib.routable_page.models import RoutablePageMixin, route

from content.models import BasePage, ContentPage
from core.models import fields
from events import types
from events.utils import get_event_datetime_display_string


class EventsHome(BasePage):
class EventsHome(RoutablePageMixin, BasePage):
template = "events/events_home.html"
show_in_menus = True
is_creatable = False
Expand All @@ -19,11 +24,74 @@ class EventsHome(BasePage):
def get_template(self, request, *args, **kwargs):
return self.template

@route(r"^(?P<year>\d{4})/$", name="month_year")
def year_events(self, request, year):
return redirect(
self.full_url + self.reverse_subpage("month_events", args=(year, 1))
)

@route(r"^(?P<year>\d{4})/(?P<month>\d{1,2})/$", name="month_events")
def month_events(self, request, year, month):
year = int(year)
month = int(month)

if month < 1 or month > 12:
raise Http404

filter_date = datetime(int(year), int(month), 1)

return TemplateResponse(
request,
self.get_template(request),
self.get_context(request, filter_date=filter_date),
)

def get_context(self, request, *args, **kwargs):
from events.filters import EventsFilters

context = super().get_context(request, *args, **kwargs)
events = EventPage.objects.live().public()
context["events"] = events

filter_date = kwargs.get("filter_date", timezone.now()).date()

month_start = filter_date.replace(day=1)

previous_month = month_start - relativedelta(months=1)
next_month = month_start + relativedelta(months=1)

month_end = month_start.replace(month=next_month.month)

events = (
EventPage.objects.live()
.public()
.filter(
event_date__gte=month_start,
event_date__lt=month_end,
)
.order_by("event_date")
)

current_month_start = timezone.now().date().replace(day=1)

page_title_prefix = "What's on in"
if filter_date < current_month_start:
page_title_prefix = "What happened in"

# Filtering events
events_filters = EventsFilters(request.GET, queryset=events)
events = events_filters.qs

context.update(
events_filters=events_filters,
page_title=f"{page_title_prefix} {month_start.strftime('%B %Y')}",
upcoming_events=events.filter(
event_date__gte=timezone.now().date(),
),
past_events=events.filter(
event_date__lt=timezone.now().date(),
),
next_month=next_month,
previous_month=previous_month,
)
return context


Expand Down
33 changes: 19 additions & 14 deletions src/events/templates/events/events_home.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% extends "dwds_content.html" %}
{% load wagtailcore_tags %}
{% load wagtailimages_tags %}
{% load wagtailroutablepage_tags %}

{% block main_content_classes %}
content_page
Expand All @@ -10,18 +9,24 @@
{% endblock bookmark %}

{% block primary_content %}
<ul class="ws-news-items">
{% for event in events %}
<li>
<h2 class="govuk-heading-m">
<a href="{% pageurl event %}">{{ event.title }}</a>
</h2>
<p class="govuk-body-s">{{ event.event_start|date:"d M Y" }}</p>
{% image event.preview_image width-540 %}
<p class="govuk-body">{{ event.description }}</p>
</li>
{% endfor %}
</ul>
<a href="{% routablepageurl page 'month_events' previous_month.year previous_month.month %}">Previous month</a>
<a href="{% routablepageurl page 'month_events' next_month.year next_month.month %}">Next month</a>

<form method="GET">
{{ events_filters.form.as_p }}
<input type="submit" value="Apply" />
</form>

{% if not upcoming_events and not past_events %}<p>There are no events to display.</p>{% endif %}

{% if upcoming_events %}
<h2>Upcoming events</h2>
{% include "events/includes/event_listing_section.html" with events=upcoming_events %}
{% endif %}
{% if past_events %}
<h2>Past events</h2>
{% include "events/includes/event_listing_section.html" with events=past_events %}
{% endif %}
{% endblock primary_content %}

{% block secondary_content %}
Expand Down
15 changes: 15 additions & 0 deletions src/events/templates/events/includes/event_listing_section.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% load wagtailcore_tags %}
{% load wagtailimages_tags %}

<ul>
{% for event in events %}
<li>
<h2>
<a href="{% pageurl event %}">{{ event.title }}</a>
</h2>
<p>{{ event.event_date }}</p>
{% image event.preview_image width-540 %}
<p>{{ event.description }}</p>
</li>
{% endfor %}
</ul>
6 changes: 6 additions & 0 deletions src/peoplefinder/services/uk_staff_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ def ingest_uk_staff_locations(self) -> IngestOutput:
"updated": updated,
"deleted": deleted,
}

def get_uk_staff_location_cities(self) -> list[str]:
return [
c["city"]
for c in UkStaffLocation.objects.values("city").distinct().order_by("city")
]

0 comments on commit ccd8372

Please sign in to comment.