from datetime import timedelta, date from django import template from django.conf import settings from django.utils import timezone import locale from ..models import COFActuPage import re register = template.Library() @register.filter() def obfuscate_mail(value): val = value.replace('', '-').replace('@', 'arbre').replace('.', 'pont')[1:] 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()) month_postend = next_month + timedelta(days=(next_month.weekday()+6)%7) events = COFActuPage.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) 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') + ('hasevent' if len(curevents) > 0 else '')), 'events': curevents} week.append(day) curday += deltaday weeks.append(week) return {"events": events, "weeks": weeks} @register.inclusion_tag("cofcms/mini_calendar.html") def mini_calendar(event, loc): try: locale.setlocale(locale.LC_TIME, loc) except locale.Error: pass 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}