98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
from django.shortcuts import render
|
|
from calendrier.forms import EventForm
|
|
from calendrier.models import Event
|
|
from django.utils.safestring import mark_safe
|
|
from django.views.generic import UpdateView
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from partitions.decorators import chef_required
|
|
|
|
from calendrier.calend import EventCalendar
|
|
from calendar import monthrange
|
|
from datetime import *
|
|
|
|
def named_month(pMonthNumber):
|
|
return date(1900, pMonthNumber, 1).strftime('%B')
|
|
|
|
def home(request):
|
|
lToday = datetime.now()
|
|
return calendar(request, lToday.year, lToday.month)
|
|
|
|
def calendar(request, pYear, pMonth):
|
|
lYear=int(pYear)
|
|
lMonth = int(pMonth)
|
|
lCalendarFromMonth = datetime(lYear, lMonth, 1)
|
|
lCalendarToMonth = datetime(lYear, lMonth, monthrange(lYear, lMonth)[1])
|
|
lEvents = Event.objects.filter(date__gte=lCalendarFromMonth, date__lte=lCalendarToMonth, calendrier=True)
|
|
if request.user.is_authenticated():
|
|
lEvents = Event.objects.filter(date__gte=lCalendarFromMonth, date__lte=lCalendarToMonth)
|
|
lCalendar = EventCalendar(lEvents).formatmonth(lYear, lMonth)
|
|
lPreviousYear = lYear
|
|
lPreviousMonth = lMonth - 1
|
|
if lPreviousMonth == 0:
|
|
lPreviousMonth = 12
|
|
lPreviousYear - 1
|
|
lNextYear = lYear
|
|
lNextMonth = lMonth + 1
|
|
if lNextMonth == 13:
|
|
lNextMonth = 1
|
|
lNextYear = lYear + 1
|
|
lYearAfterThis = lYear + 1
|
|
lYearBeforeThis = lYear - 1
|
|
return render(request, 'calendrier/home.html', {'Calendar': mark_safe(lCalendar),
|
|
'Month': lMonth,
|
|
'MonthName' : named_month(lMonth),
|
|
'Year': lYear,
|
|
'PreviousMonth': lPreviousMonth,
|
|
'PreviousMonthName': named_month(lPreviousMonth),
|
|
'PreviousYear': lPreviousYear,
|
|
'NextMonth': lNextMonth,
|
|
'NextMonthName': named_month(lNextMonth),
|
|
'NextYear': lNextYear,
|
|
'YearBeforeThis': lYearBeforeThis,
|
|
'YearAfterThis': lYearAfterThis,
|
|
})
|
|
|
|
def view_event(request, pYear, pMonth, id):
|
|
ev = Event.objects.get(id=id)
|
|
nom = ev.nom.capitalize
|
|
fin = False
|
|
desc = False
|
|
if ev.fin:
|
|
fin = True
|
|
if ev.description:
|
|
desc = True
|
|
return render(request, 'calendrier/view_event.html', locals())
|
|
|
|
def view_eventbis(request, id):
|
|
ev = Event.objects.get(id=id)
|
|
nom = ev.nom.capitalize
|
|
fin = False
|
|
desc = False
|
|
if ev.fin:
|
|
fin = True
|
|
if ev.description:
|
|
desc = True
|
|
return render(request, 'calendrier/view_event.html', locals())
|
|
|
|
@chef_required
|
|
def create_event(request):
|
|
if request.method == "POST":
|
|
form = EventForm(request.POST)
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
envoi = True
|
|
|
|
else:
|
|
form = EventForm()
|
|
|
|
return render(request, "calendrier/create.html", locals())
|
|
|
|
class EventUpdate(UpdateView):
|
|
model = Event
|
|
template_name = "calendrier/update.html"
|
|
form_class = EventForm
|
|
success_url = reverse_lazy(home)
|
|
|
|
|
|
# Create your views here.
|