@@ -44,16 +44,26 @@ class EventHomepage(ListView):
4444
4545 def get_queryset (self ) -> Event :
4646 """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" )
4848
4949 def get_context_data (self , ** kwargs : dict ) -> dict :
5050 """Add more ctx, specifically events that are happening now, just missed, and upcoming."""
5151 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
5464 context ["events_now" ] = Event .objects .filter (
5565 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 ]
5767 return context
5868
5969
@@ -79,15 +89,19 @@ def get_context_data(self, **kwargs):
7989class EventList (EventListBase ):
8090
8191 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' )
8493
8594 def get_context_data (self , ** kwargs ):
8695 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 ]
89102 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+
91105 return context
92106
93107
0 commit comments