fix upcoming events
This commit is contained in:
parent
f46154ddfd
commit
25c4714072
|
@ -1,7 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
@ -9,6 +9,7 @@ from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||||
from django.db.models import Q, Count
|
from django.db.models import Q, Count
|
||||||
|
from django.db.models.functions import Coalesce
|
||||||
from django.http import HttpResponse, Http404
|
from django.http import HttpResponse, Http404
|
||||||
from django.shortcuts import get_object_or_404, render, redirect
|
from django.shortcuts import get_object_or_404, render, redirect
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
@ -23,14 +24,24 @@ from . import forms
|
||||||
|
|
||||||
ITEMS_PER_PAGE = 30
|
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):
|
def index(request):
|
||||||
now = datetime.now()
|
|
||||||
display_events = ['news', 'events']
|
display_events = ['news', 'events']
|
||||||
base = Content.objects.filter(type__name__in=display_events).order_by('-datestart')
|
base = Content.objects.filter(type__name__in=display_events).order_by('-datestart')
|
||||||
base = base.filter(published=True)
|
base = base.filter(published=True)
|
||||||
upcoming_events = base.filter(datestart__gte=now)[:12]
|
upcoming_events, ongoing_events, past_events = get_events(base)
|
||||||
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]
|
|
||||||
|
|
||||||
homepage = Content.objects.filter(type__name='homepage').order_by('-datestart')[:1]
|
homepage = Content.objects.filter(type__name='homepage').order_by('-datestart')[:1]
|
||||||
context = {
|
context = {
|
||||||
|
@ -73,7 +84,7 @@ def section_index(request, section, max_length=10):
|
||||||
def content_years(content):
|
def content_years(content):
|
||||||
years = [
|
years = [
|
||||||
year['year'].year
|
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
|
return years
|
||||||
|
|
||||||
|
@ -81,9 +92,10 @@ def filter_by_years(content, year):
|
||||||
years = content_years(content)
|
years = content_years(content)
|
||||||
if year.isdigit():
|
if year.isdigit():
|
||||||
year = int(year)
|
year = int(year)
|
||||||
else:
|
elif years:
|
||||||
year = years[0]
|
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 {
|
return {
|
||||||
'content': content,
|
'content': content,
|
||||||
'year': year,
|
'year': year,
|
||||||
|
@ -124,7 +136,6 @@ def section_list(request, section):
|
||||||
return render(request, 'results.html', context)
|
return render(request, 'results.html', context)
|
||||||
|
|
||||||
def event(request):
|
def event(request):
|
||||||
now = datetime.now()
|
|
||||||
display_events = ['events', 'news']
|
display_events = ['events', 'news']
|
||||||
featured = Content.objects.filter(type__name='events', featured=True).order_by('-datestart')[:1]
|
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')
|
base = Content.objects.filter(type__name__in=display_events).order_by('-datestart')
|
||||||
|
@ -132,9 +143,7 @@ def event(request):
|
||||||
if featured:
|
if featured:
|
||||||
base = base.exclude(pk=featured[0].pk)
|
base = base.exclude(pk=featured[0].pk)
|
||||||
|
|
||||||
upcoming_events = base.filter(datestart__gt=now).order_by('-datestart')
|
upcoming_events, ongoing_events, past_events = get_events(base)
|
||||||
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]
|
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
'upcoming_events': upcoming_events,
|
'upcoming_events': upcoming_events,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user