Skip to content

Commit

Permalink
Merge pull request #1156 from Metro-Records/feature/1138-events
Browse files Browse the repository at this point in the history
Don't load all events on default events page
  • Loading branch information
antidipyramid authored Sep 5, 2024
2 parents 9d9c30e + 5668383 commit fe1d70b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 91 deletions.
2 changes: 2 additions & 0 deletions councilmatic/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
DEBUG_TOOLBAR_PANELS = [
"debug_toolbar.panels.sql.SQLPanel",
"debug_toolbar.panels.cache.CachePanel",
"template_profiler_panel.panels.template.TemplateProfilerPanel",
]


Expand All @@ -121,6 +122,7 @@
"councilmatic_core",
"adv_cache_tag",
"debug_toolbar",
"template_profiler_panel",
"captcha",
"markdownify.apps.MarkdownifyConfig",
)
Expand Down
64 changes: 13 additions & 51 deletions lametro/templates/events/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h4>Hello, {{ user.username }}!</h4>
{% endif %}

{% if future_events %}
<h2><span>Upcoming {{ CITY_VOCAB.EVENTS }}</span>
<h2 id="upcoming-meetings"><span>Upcoming {{ CITY_VOCAB.EVENTS }}</span>
<br class="non-desktop-only"/>
<small>
<a href="rss/" title="RSS feed for Upcoming and Recent Events">
Expand All @@ -76,10 +76,13 @@ <h2><span>Upcoming {{ CITY_VOCAB.EVENTS }}</span>
</div>
{% endfor %}

<a href="" class="btn btn-salmon" id="more-upcoming-events"><i class="fa fa-fw fa-chevron-down"></i>Show all upcoming meetings</a>
<a href="" class="btn btn-salmon" id="fewer-upcoming-events"><i class="fa fa-fw fa-chevron-up"></i>Show fewer upcoming meetings</a>
{% if 'show' in request.GET and request.GET.show == 'future' %}
<a href="{% url 'events' %}" class="btn btn-salmon" id="fewer-upcoming-events"><i class="fa fa-fw fa-chevron-up"></i>Show fewer upcoming meetings</a>
{% else %}
<a href="{% url 'events' %}?show=future#upcoming-meetings" class="btn btn-salmon" id="more-upcoming-events"><i class="fa fa-fw fa-chevron-down"></i>Show all upcoming meetings</a>
{% endif %}

<h2 style="margin-top: 40px;"><span>Past {{ CITY_VOCAB.EVENTS }}</span>
<h2 id="past-meetings" style="margin-top: 40px;"><span>Past {{ CITY_VOCAB.EVENTS }}</span>
<br class="non-desktop-only"/>
<small><a href="rss/" title="RSS feed for Upcoming and Recent Events"><i class="fa fa-rss-square" aria-hidden="true"></i></a></small>
</h2><br class="non-mobile-only"/>
Expand All @@ -94,8 +97,11 @@ <h2 style="margin-top: 40px;"><span>Past {{ CITY_VOCAB.EVENTS }}</span>
</div>
{% endfor %}

<a href="" class="btn btn-salmon" id="more-events"><i class="fa fa-fw fa-chevron-down"></i>Show all past meetings</a>
<a href="" class="btn btn-salmon" id="fewer-events"><i class="fa fa-fw fa-chevron-up"></i>Show fewer past meetings</a>
{% if 'show' in request.GET and request.GET.show == 'past' %}
<a href="{% url 'events' %}" class="btn btn-salmon" id="fewer-events"><i class="fa fa-fw fa-chevron-up"></i>Show fewer past meetings</a>
{% else %}
<a href="{% url 'events' %}?show=past#past-meetings" class="btn btn-salmon" id="more-events"><i class="fa fa-fw fa-chevron-down"></i>Show all past meetings</a>
{% endif %}

{% elif past_events %}
<h2 style="margin-top: 40px;"><span>Past {{ CITY_VOCAB.EVENTS }}</span>
Expand All @@ -115,6 +121,7 @@ <h2 style="margin-top: 40px;"><span>Past {{ CITY_VOCAB.EVENTS }}</span>

<a href="" class="btn btn-salmon" id="more-events"><i class="fa fa-fw fa-chevron-down"></i>Show all past meetings</a>
<a href="" class="btn btn-salmon" id="fewer-events"><i class="fa fa-fw fa-chevron-up"></i>Show fewer past meetings</a>

{% elif select_events %}
<h2><span>{{ CITY_VOCAB.EVENTS }} from {{ start_date }} to {{ end_date }}</span>
<br class="non-desktop-only"/>
Expand Down Expand Up @@ -174,53 +181,8 @@ <h2><span>All {{ CITY_VOCAB.EVENTS }}</span>
<script type="{% static 'js/lib/bootstrap-datetimepicker.js' %}"></script>
<script>
$(document).ready(function() {

$('#from').datepicker();
$('#to').datepicker();

function collapseUpcomingEvents(){
$(".event-upcoming-listing:gt(2)").hide();
$("#more-upcoming-events").show();
$("#fewer-upcoming-events").hide();
}
function expandUpcomingEvents(){
$(".event-upcoming-listing:gt(2)").show();
$("#more-upcoming-events").hide();
$("#fewer-upcoming-events").show();
}
function collapseEvents(){
$(".event-listing:gt(4)").hide();
$("#more-events").show();
$("#fewer-events").hide();
}
function expandEvents(){
$(".event-listing:gt(4)").show();
$("#more-events").hide();
$("#fewer-events").show();
}

collapseEvents();
collapseUpcomingEvents();

$("#more-upcoming-events").click(function() {
expandUpcomingEvents();
return false;
});

$("#fewer-upcoming-events").click(function() {
collapseUpcomingEvents();
return false;
});

$("#more-events").click(function() {
expandEvents();
return false;
});

$("#fewer-events").click(function() {
collapseEvents();
return false;
});
});
</script>
{% endblock %}
92 changes: 52 additions & 40 deletions lametro/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from operator import attrgetter
import itertools
import urllib
from datetime import date, datetime
from datetime import datetime
from dateutil import parser
import requests
import os
Expand All @@ -18,7 +17,7 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.db.models.functions import Lower, Now, Cast
from django.db.models import Max, Prefetch, Case, When, Value, IntegerField, Q
from django.db.models import Max, Prefetch, Case, When, Value, IntegerField, Q, F
from django.urls import reverse
from django.utils import timezone
from django.views.generic import (
Expand Down Expand Up @@ -344,16 +343,12 @@ class LAMetroEventsView(EventsView):
def get_context_data(self, **kwargs):
context = {}

# Did the user set date boundaries?
start_date_str = self.request.GET.get("from")
end_date_str = self.request.GET.get("to")

def day_grouper(x):
return (
return datetime(
x.local_start_time.year,
x.local_start_time.month,
x.local_start_time.day,
)
).date()

# We only want to display approved minutes
minutes_queryset = EventDocument.objects.filter(
Expand All @@ -363,6 +358,10 @@ def day_grouper(x):
# A base queryset for non-test objects with media
media_events = LAMetroEvent.objects.with_media().exclude(name__icontains="test")

# Did the user set date boundaries?
start_date_str = self.request.GET.get("from")
end_date_str = self.request.GET.get("to")

# If yes...
if start_date_str and end_date_str:
context["start_date"] = start_date_str
Expand All @@ -379,54 +378,67 @@ def day_grouper(x):
Prefetch("documents", minutes_queryset, to_attr="minutes")
).prefetch_related("minutes__links")

org_select_events = []

for event_date, events in itertools.groupby(select_events, key=day_grouper):
events = sorted(events, key=attrgetter("start_time"))
org_select_events.append([date(*event_date), events])

context["select_events"] = org_select_events
org_select_events = itertools.groupby(select_events, key=day_grouper)
context["select_events"] = [
(d, list(events)) for d, events in org_select_events
]

# If all meetings
elif self.request.GET.get("show"):
# Did user request all events?
elif self.request.GET.get("show") == "all":
all_events = media_events.order_by("-start_time")
org_all_events = []
org_all_events = itertools.groupby(all_events, key=day_grouper)

for event_date, events in itertools.groupby(all_events, key=day_grouper):
events = sorted(events, key=attrgetter("start_time"))
org_all_events.append([date(*event_date), events])
context["all_events"] = [(d, list(events)) for d, events in org_all_events]

context["all_events"] = org_all_events
# If no...
else:
# Upcoming events
past_events = media_events.filter(start_time__lt=timezone.now()).order_by(
"-start_time"
)
future_events = media_events.filter(start_time__gt=timezone.now()).order_by(
"start_time"
)
org_future_events = []

for event_date, events in itertools.groupby(future_events, key=day_grouper):
events = sorted(events, key=attrgetter("start_time"))
org_future_events.append([date(*event_date), events])
# Limit past events shown unless user requested all
if not self.request.GET.get("show") == "past":
last_three_meeting_days = (
past_events.annotate(date=F("start_time__date"))
.order_by("-date")
.values_list("date", flat=True)
.distinct()[:3]
)

context["future_events"] = org_future_events
past_events = past_events.filter(
start_time__date__in=last_three_meeting_days
).order_by("-start_time")

# Limit future events shown unless user requested all
if not self.request.GET.get("show") == "future":
next_three_meeting_days = (
future_events.annotate(date=F("start_time__date"))
.order_by("date")
.values_list("date", flat=True)
.distinct()[:3]
)

# Past events
past_events = media_events.filter(start_time__lt=timezone.now()).order_by(
"-start_time"
)
future_events = media_events.filter(
start_time__date__in=next_three_meeting_days
).order_by("start_time")

# Prefetch documents for past events
past_events = past_events.prefetch_related(
Prefetch("documents", minutes_queryset, to_attr="minutes")
).prefetch_related("minutes__links")

org_past_events = []
# Group meetings by day
org_future_events = itertools.groupby(future_events, key=day_grouper)
org_past_events = itertools.groupby(past_events, key=day_grouper)

for event_date, events in itertools.groupby(past_events, key=day_grouper):
events = sorted(events, key=attrgetter("start_time"))
org_past_events.append([date(*event_date), events])

context["past_events"] = org_past_events
context["future_events"] = [
(d, list(events)) for d, events in org_future_events
]
context["past_events"] = [
(d, list(events)) for d, events in org_past_events
]

context["user_subscribed"] = False
if self.request.user.is_authenticated:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ django-markdownify==0.9.3

# Development
django-debug-toolbar
django-debug-toolbar-template-profiler
pytest
pytest-django
pytest-mock==1.6.3
Expand Down

0 comments on commit fe1d70b

Please sign in to comment.