From 25c4714072b6fe41af35ef22c348b6f4ada229f9 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 9 Aug 2023 09:45:38 +0000 Subject: [PATCH] fix upcoming events --- content/views.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/content/views.py b/content/views.py index 97e5991..256bdc4 100644 --- a/content/views.py +++ b/content/views.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from datetime import datetime +from datetime import datetime, timedelta import re from django.conf import settings @@ -9,6 +9,7 @@ from django.core.exceptions import ObjectDoesNotExist from django.core.mail import EmailMessage from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.db.models import Q, Count +from django.db.models.functions import Coalesce from django.http import HttpResponse, Http404 from django.shortcuts import get_object_or_404, render, redirect from django.urls import reverse @@ -23,14 +24,24 @@ from . import forms ITEMS_PER_PAGE = 30 +def get_events(base): + now = datetime.now() - timedelta(hours=24) + now = datetime.now().date() + upcoming_events = base.filter(datestart__gte=now).order_by('datestart')[:12] + ongoing_events = base.filter(datestart__lt=now, dateend__gte=now)[:12] + past_events = base.filter(Q(dateend__lt=now) | Q(dateend=None, datestart__lt=now)) + #past_events.annotate(datesort=Coalesce('dateend', 'datestart')).order_by('-datesort')[:12] + past_events = past_events.order_by('-datestart')[:12] + #past_events = list(past_events) + #past_events.sort(key=lambda event: event.dateend if event.dateend else event.datestart, reverse=True) + return upcoming_events, ongoing_events, past_events + + def index(request): - now = datetime.now() display_events = ['news', 'events'] base = Content.objects.filter(type__name__in=display_events).order_by('-datestart') base = base.filter(published=True) - upcoming_events = base.filter(datestart__gte=now)[:12] - ongoing_events = base.filter(datestart__lt=now, dateend__gte=now)[:12] - past_events = base.filter(Q(dateend__lt=now) | Q(dateend=None, datestart__lt=now))[:12] + upcoming_events, ongoing_events, past_events = get_events(base) homepage = Content.objects.filter(type__name='homepage').order_by('-datestart')[:1] context = { @@ -73,7 +84,7 @@ def section_index(request, section, max_length=10): def content_years(content): years = [ year['year'].year - for year in content.annotate(year=TruncYear('datestart')).values('year').annotate(count=Count('shortname')).order_by('-year') + for year in content.exclude(datestart=None).annotate(year=TruncYear('datestart')).values('year').annotate(count=Count('shortname')).order_by('-year') ] return years @@ -81,9 +92,10 @@ def filter_by_years(content, year): years = content_years(content) if year.isdigit(): year = int(year) - else: + elif years: year = years[0] - content = content.filter(datestart__gte='%s-01-01' % year, datestart__lt='%s-01-01' % (year+1)) + if year: + content = content.filter(datestart__gte='%04d-01-01' % year, datestart__lt='%04d-01-01' % (year+1)) return { 'content': content, 'year': year, @@ -124,7 +136,6 @@ def section_list(request, section): return render(request, 'results.html', context) def event(request): - now = datetime.now() display_events = ['events', 'news'] featured = Content.objects.filter(type__name='events', featured=True).order_by('-datestart')[:1] base = Content.objects.filter(type__name__in=display_events).order_by('-datestart') @@ -132,9 +143,7 @@ def event(request): if featured: base = base.exclude(pk=featured[0].pk) - upcoming_events = base.filter(datestart__gt=now).order_by('-datestart') - ongoing_events = base.filter(datestart__lt=now, dateend__gte=now).order_by('-datestart') - past_events = base.filter(Q(dateend__lt=now) | Q(dateend=None, datestart__lt=now)).order_by('-datestart')[:10] + upcoming_events, ongoing_events, past_events = get_events(base) context = { 'upcoming_events': upcoming_events,