2017-08-20 00:39:19 +02:00
|
|
|
from datetime import timedelta, date
|
2017-08-19 01:32:26 +02:00
|
|
|
from django import template
|
|
|
|
from django.conf import settings
|
2017-08-20 00:39:19 +02:00
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from ..models import COFActuEventPage
|
2017-08-19 01:32:26 +02:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
|
|
|
|
@register.filter()
|
|
|
|
def obfuscate_mail(value):
|
2017-08-22 00:58:18 +02:00
|
|
|
val = value.replace('', '-').replace('@', 'arbre').replace('.', 'pont')[1:]
|
2017-08-19 01:32:26 +02:00
|
|
|
return val
|
2017-08-20 00:39:19 +02:00
|
|
|
|
|
|
|
@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)
|
2017-08-22 00:58:18 +02:00
|
|
|
month_prestart = month_start - timedelta(days=month_start.weekday())
|
2017-08-20 00:39:19 +02:00
|
|
|
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}
|
2017-08-22 00:58:18 +02:00
|
|
|
|
|
|
|
@register.inclusion_tag("cofcms/mini_calendar.html")
|
|
|
|
def mini_calendar(event):
|
|
|
|
days = []
|
|
|
|
today = timezone.now().date()
|
|
|
|
date_start = event.date_start.date()
|
|
|
|
date_end = event.date_end.date() if event.date_end else date_start
|
|
|
|
week_start = date_start - timedelta(days=date_start.weekday())
|
|
|
|
curday = week_start
|
|
|
|
for i in range(7):
|
|
|
|
days.append({'day': curday.day,
|
|
|
|
'hasevent': curday >= date_start and curday <= date_end,
|
|
|
|
'today': curday == today})
|
|
|
|
curday += timedelta(days=1)
|
|
|
|
return {"days": days}
|