kpsul/gestioncof/cms/templatetags/cofcms_tags.py

54 lines
2 KiB
Python
Raw Normal View History

from datetime import timedelta, date
from django import template
from django.conf import settings
from django.utils import timezone
from ..models import COFActuEventPage
import re
register = template.Library()
@register.filter()
def obfuscate_mail(value):
val = value.replace('', '/').replace('@', 'arbse').replace('.', 'pnt')
return val
@register.inclusion_tag("cofcms/calendar.html")
def calendar():
now = timezone.now()
month_start = date(now.year, now.month, 1)
next_month = month_start + timedelta(days=32)
next_month = date(next_month.year, next_month.month, 1)
month_prestart = month_start - timedelta(days=(month_start.weekday()+7)%7)
month_postend = next_month + timedelta(days=(next_month.weekday()+6)%7)
events = COFActuEventPage.objects.live()\
.filter(date_start__range=[month_prestart,
month_postend])\
.order_by('-date_start')
events = list(events)
weeks = []
curday = month_prestart
deltaday = timedelta(days=1)
while curday < next_month and len(weeks)<10:
week = []
for k in range(7):
curevents = []
for k in range(len(events)-1, -1, -1):
e = events[k]
if e.date_start.date() > curday: break
if (e.date_start if e.date_end is None else e.date_end).date() < curday:
del events[k]
else:
curevents.append(e)
print(curevents)
day = {'day': curday.day,
'class': (('today ' if (curday.day == now.day
and curday.month == now.month) else '')
+ ('in' if curday.month == now.month else 'out')),
'events': curevents}
week.append(day)
curday += deltaday
weeks.append(week)
return {"events": events, "weeks": weeks}