@@ -44,16 +44,26 @@ class EventHomepage(ListView):
44
44
45
45
def get_queryset (self ) -> Event :
46
46
"""Queryset to return all events, ordered by START date."""
47
- return Event .objects .all ().order_by ("- occurring_rule__dt_start" )
47
+ return Event .objects .all ().order_by ("occurring_rule__dt_start" )
48
48
49
49
def get_context_data (self , ** kwargs : dict ) -> dict :
50
50
"""Add more ctx, specifically events that are happening now, just missed, and upcoming."""
51
51
context = super ().get_context_data (** kwargs )
52
- context ["events_just_missed" ] = Event .objects .until_datetime (timezone .now ())[:2 ]
53
- context ["upcoming_events" ] = Event .objects .for_datetime (timezone .now ())
52
+
53
+ # past events, most recent first
54
+ past_events = list (Event .objects .until_datetime (timezone .now ()))
55
+ past_events .sort (key = lambda e : e .previous_time .dt_start if e .previous_time else timezone .now (), reverse = True )
56
+ context ["events_just_missed" ] = past_events [:2 ]
57
+
58
+ # upcoming events, soonest first
59
+ upcoming = list (Event .objects .for_datetime (timezone .now ()))
60
+ upcoming .sort (key = lambda e : e .next_time .dt_start if e .next_time else timezone .now ())
61
+ context ["upcoming_events" ] = upcoming
62
+
63
+ # right now, soonest first
54
64
context ["events_now" ] = Event .objects .filter (
55
65
occurring_rule__dt_start__lte = timezone .now (),
56
- occurring_rule__dt_end__gte = timezone .now ())[:2 ]
66
+ occurring_rule__dt_end__gte = timezone .now ()). order_by ( 'occurring_rule__dt_start' ) [:2 ]
57
67
return context
58
68
59
69
@@ -79,15 +89,19 @@ def get_context_data(self, **kwargs):
79
89
class EventList (EventListBase ):
80
90
81
91
def get_queryset (self ):
82
- return Event .objects .for_datetime (timezone .now ()).filter (calendar__slug = self .kwargs ['calendar_slug' ]).order_by (
83
- 'occurring_rule__dt_start' )
92
+ return Event .objects .for_datetime (timezone .now ()).filter (calendar__slug = self .kwargs ['calendar_slug' ]).order_by ('occurring_rule__dt_start' )
84
93
85
94
def get_context_data (self , ** kwargs ):
86
95
context = super ().get_context_data (** kwargs )
87
- context ['events_today' ] = Event .objects .until_datetime (timezone .now ()).filter (
88
- calendar__slug = self .kwargs ['calendar_slug' ])[:2 ]
96
+
97
+ # today's events, most recent first
98
+ today_events = list (Event .objects .until_datetime (timezone .now ()).filter (
99
+ calendar__slug = self .kwargs ['calendar_slug' ]))
100
+ today_events .sort (key = lambda e : e .previous_time .dt_start if e .previous_time else timezone .now (), reverse = True )
101
+ context ['events_today' ] = today_events [:2 ]
89
102
context ['calendar' ] = get_object_or_404 (Calendar , slug = self .kwargs ['calendar_slug' ])
90
- context ['upcoming_events' ] = self .get_queryset ()
103
+ context ['upcoming_events' ] = context ['object_list' ]
104
+
91
105
return context
92
106
93
107
0 commit comments