2017-01-14 10:24:38 +01:00
|
|
|
from datetime import date, datetime
|
|
|
|
|
2016-09-24 00:55:01 +02:00
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
2016-07-13 23:34:54 +02:00
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2015-04-13 18:56:43 +02:00
|
|
|
from django.utils.safestring import mark_safe
|
2015-07-22 22:08:59 +02:00
|
|
|
from django.views.generic import UpdateView, DeleteView
|
2017-01-14 10:24:38 +01:00
|
|
|
from django.core.urlresolvers import reverse, reverse_lazy
|
2015-07-22 22:08:59 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.utils.decorators import method_decorator
|
2015-09-23 08:08:26 +02:00
|
|
|
from django.core.mail import send_mail
|
2017-01-14 10:24:38 +01:00
|
|
|
from django.http import Http404
|
2015-07-22 22:08:59 +02:00
|
|
|
|
2017-01-14 10:24:38 +01:00
|
|
|
from gestion.models import ErnestoUser
|
2015-04-13 18:56:43 +02:00
|
|
|
from calendrier.calend import EventCalendar
|
|
|
|
from calendar import monthrange
|
2017-01-14 10:24:38 +01:00
|
|
|
from calendrier.forms import ModifEventForm, EventForm, ParticipantsForm, \
|
|
|
|
SendMail, ChangeDoodleName
|
|
|
|
from calendrier.models import Event, Participants
|
|
|
|
from partitions.decorators import chef_required
|
2015-07-22 22:08:59 +02:00
|
|
|
from propositions.utils import generer
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-04-13 18:56:43 +02:00
|
|
|
def named_month(pMonthNumber):
|
|
|
|
return date(1900, pMonthNumber, 1).strftime('%B')
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-04-13 18:56:43 +02:00
|
|
|
def home(request):
|
|
|
|
lToday = datetime.now()
|
|
|
|
return calendar(request, lToday.year, lToday.month)
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-04-13 18:56:43 +02:00
|
|
|
def calendar(request, pYear, pMonth):
|
2016-07-13 19:37:55 +02:00
|
|
|
lYear = int(pYear)
|
2015-04-13 18:56:43 +02:00
|
|
|
lMonth = int(pMonth)
|
|
|
|
lCalendarFromMonth = datetime(lYear, lMonth, 1)
|
|
|
|
lCalendarToMonth = datetime(lYear, lMonth, monthrange(lYear, lMonth)[1])
|
2016-07-13 19:37:55 +02:00
|
|
|
lEvents = Event.objects.filter(date__gte=lCalendarFromMonth,
|
|
|
|
date__lte=lCalendarToMonth,
|
|
|
|
calendrier=True)
|
2015-04-17 18:38:44 +02:00
|
|
|
if request.user.is_authenticated():
|
2016-07-13 19:37:55 +02:00
|
|
|
lEvents = Event.objects.filter(date__gte=lCalendarFromMonth,
|
|
|
|
date__lte=lCalendarToMonth)
|
2015-04-13 18:56:43 +02:00
|
|
|
lCalendar = EventCalendar(lEvents).formatmonth(lYear, lMonth)
|
|
|
|
lPreviousYear = lYear
|
|
|
|
lPreviousMonth = lMonth - 1
|
|
|
|
if lPreviousMonth == 0:
|
|
|
|
lPreviousMonth = 12
|
2018-01-05 15:13:13 +01:00
|
|
|
lPreviousYear -= 1
|
2015-04-13 18:56:43 +02:00
|
|
|
lNextYear = lYear
|
|
|
|
lNextMonth = lMonth + 1
|
|
|
|
if lNextMonth == 13:
|
|
|
|
lNextMonth = 1
|
|
|
|
lNextYear = lYear + 1
|
|
|
|
lYearAfterThis = lYear + 1
|
|
|
|
lYearBeforeThis = lYear - 1
|
2016-07-13 19:37:55 +02:00
|
|
|
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,
|
2015-04-13 18:56:43 +02:00
|
|
|
})
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-04-13 18:56:43 +02:00
|
|
|
def view_event(request, pYear, pMonth, id):
|
2016-09-24 00:55:01 +02:00
|
|
|
ev = get_object_or_404(Event, id=id)
|
2016-06-21 00:33:32 +02:00
|
|
|
if not request.user.is_authenticated() and not ev.calendrier:
|
2016-07-13 19:37:55 +02:00
|
|
|
return redirect('calendrier.views.home')
|
2015-04-13 18:56:43 +02:00
|
|
|
nom = ev.nom.capitalize
|
|
|
|
fin = False
|
|
|
|
desc = False
|
2015-07-22 22:08:59 +02:00
|
|
|
part = ev.participants_set.all()
|
|
|
|
nboui = len(part.filter(reponse="oui"))
|
|
|
|
nbpe = len(part.filter(reponse="pe"))
|
|
|
|
nbnon = len(part.filter(reponse="non"))
|
2015-04-13 18:56:43 +02:00
|
|
|
if ev.fin:
|
|
|
|
fin = True
|
|
|
|
if ev.description:
|
2016-07-13 19:37:55 +02:00
|
|
|
desc = True
|
|
|
|
return render(request, 'calendrier/view_event.html', locals())
|
|
|
|
|
2015-04-13 18:56:43 +02:00
|
|
|
|
|
|
|
def view_eventbis(request, id):
|
2016-09-24 00:55:01 +02:00
|
|
|
ev = get_object_or_404(Event, id=id)
|
2016-06-21 00:33:32 +02:00
|
|
|
if not request.user.is_authenticated() and not ev.calendrier:
|
2016-07-13 19:37:55 +02:00
|
|
|
return redirect('calendrier.views.home')
|
2015-07-22 22:08:59 +02:00
|
|
|
part = ev.participants_set.all()
|
2015-04-13 18:56:43 +02:00
|
|
|
nom = ev.nom.capitalize
|
|
|
|
fin = False
|
|
|
|
desc = False
|
2015-07-22 22:08:59 +02:00
|
|
|
nboui = len(part.filter(reponse="oui"))
|
|
|
|
nbpe = len(part.filter(reponse="pe"))
|
|
|
|
nbnon = len(part.filter(reponse="non"))
|
2015-04-13 18:56:43 +02:00
|
|
|
if ev.fin:
|
|
|
|
fin = True
|
|
|
|
if ev.description:
|
|
|
|
desc = True
|
|
|
|
return render(request, 'calendrier/view_event.html', locals())
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-09-23 08:08:26 +02:00
|
|
|
@chef_required
|
|
|
|
def resend(request, id):
|
2016-07-13 19:37:55 +02:00
|
|
|
if request.method == "POST":
|
2015-09-23 08:08:26 +02:00
|
|
|
form = SendMail(request.POST)
|
|
|
|
if form.is_valid():
|
2016-09-24 00:55:01 +02:00
|
|
|
ev = get_object_or_404(Event, id=id)
|
2015-09-23 08:08:26 +02:00
|
|
|
users = ErnestoUser.objects.filter(is_ernesto=True)
|
|
|
|
message = form.cleaned_data["message"]
|
|
|
|
try:
|
|
|
|
for user in users:
|
2016-07-13 19:37:55 +02:00
|
|
|
if user.mails:
|
2016-07-13 20:12:11 +02:00
|
|
|
Subject = "[Pouet-Pouet] %s, le %s" % (
|
|
|
|
ev.nom, ev.date.strftime('%d/%m/%Y'))
|
2016-07-13 19:37:55 +02:00
|
|
|
sender = 'fanfare@ens.fr'
|
|
|
|
receivers = [user.user.email]
|
|
|
|
if message != '':
|
2016-07-19 23:14:00 +02:00
|
|
|
text = "%s\n\n" % message
|
2016-07-13 20:12:11 +02:00
|
|
|
else:
|
|
|
|
text = "Bonjour, un évémenent a été créé.\n"
|
2016-07-13 23:34:54 +02:00
|
|
|
domain = get_current_site(request).domain
|
|
|
|
text += (
|
|
|
|
'Pour voir l\'événement, allez sur %s%s\n'
|
|
|
|
'Pour répondre oui allez sur %s%s\n'
|
|
|
|
'Pour répondre non, allez sur %s%s\n'
|
|
|
|
'Pour répondre peut-être, allez sur %s%s\n') \
|
|
|
|
% (domain,
|
2017-01-14 10:24:38 +01:00
|
|
|
reverse_lazy('view-event', args=[obj.id]),
|
2016-07-13 23:34:54 +02:00
|
|
|
domain,
|
2017-01-14 10:24:38 +01:00
|
|
|
reverse_lazy(
|
|
|
|
'calendrier.reponse_event',
|
|
|
|
args=[user.slug, obj.slug, "oui"]),
|
2016-07-13 23:34:54 +02:00
|
|
|
domain,
|
2017-01-14 10:24:38 +01:00
|
|
|
reverse_lazy(
|
|
|
|
'calendrier.reponse_event',
|
|
|
|
args=[user.slug, obj.slug, "non"]),
|
2016-07-13 23:34:54 +02:00
|
|
|
domain,
|
2017-01-14 10:24:38 +01:00
|
|
|
reverse_lazy(
|
|
|
|
'calendrier.reponse_event',
|
|
|
|
args=[user.slug, obj.slug, "pe"]))
|
2016-07-13 19:37:55 +02:00
|
|
|
send_mail(Subject, text, sender, receivers)
|
2015-09-23 08:08:26 +02:00
|
|
|
return redirect("calendrier.views.view_eventbis", id)
|
|
|
|
except:
|
2016-07-13 19:37:55 +02:00
|
|
|
erreur = "Une erreur est survenue, le mail n'a pas pu être " \
|
|
|
|
+ "envoyé."
|
2015-09-23 08:08:26 +02:00
|
|
|
else:
|
|
|
|
form = SendMail()
|
|
|
|
return render(request, "calendrier/resend.html", locals())
|
2016-07-13 19:37:55 +02:00
|
|
|
|
|
|
|
|
2016-06-21 00:33:32 +02:00
|
|
|
@login_required
|
|
|
|
def changename(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
requbis = request.POST.copy()
|
|
|
|
form = ChangeDoodleName(requbis, instance=request.user)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2016-07-13 19:37:55 +02:00
|
|
|
success = True
|
2016-06-21 00:33:32 +02:00
|
|
|
else:
|
|
|
|
form = ChangeDoodleName(instance=request.user)
|
|
|
|
return render(request, 'calendrier/changename.html', locals())
|
2015-09-23 08:08:26 +02:00
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-04-13 18:56:43 +02:00
|
|
|
@chef_required
|
|
|
|
def create_event(request):
|
|
|
|
if request.method == "POST":
|
|
|
|
form = EventForm(request.POST)
|
|
|
|
if form.is_valid():
|
2015-07-22 22:08:59 +02:00
|
|
|
temp = True
|
|
|
|
while temp:
|
|
|
|
code = generer()
|
|
|
|
try:
|
|
|
|
Event.objects.get(slug=code)
|
|
|
|
except:
|
2016-07-13 19:37:55 +02:00
|
|
|
temp = False
|
2015-07-22 22:08:59 +02:00
|
|
|
sendmail = form.cleaned_data["sendmail"]
|
|
|
|
nom = form.cleaned_data["nom"]
|
|
|
|
date = form.cleaned_data["date"]
|
|
|
|
date = date.strftime('%d/%m/%Y')
|
|
|
|
debut = form.cleaned_data["debut"]
|
|
|
|
message = form.cleaned_data["message"]
|
|
|
|
obj = form.save(commit=False)
|
|
|
|
obj.slug = code
|
|
|
|
obj.save()
|
2016-07-13 19:37:55 +02:00
|
|
|
id = obj.id
|
2015-04-13 18:56:43 +02:00
|
|
|
envoi = True
|
2015-07-22 22:08:59 +02:00
|
|
|
if sendmail:
|
2016-07-13 19:37:55 +02:00
|
|
|
users = ErnestoUser.objects.filter(is_ernesto=True)
|
|
|
|
for user in users:
|
|
|
|
if user.mails:
|
|
|
|
try:
|
2016-07-13 20:12:11 +02:00
|
|
|
Subject = "[Pouet-Pouet] %s, le %s" % (nom, date)
|
2016-07-13 19:37:55 +02:00
|
|
|
sender = 'fanfare@ens.fr'
|
|
|
|
receivers = [user.user.email]
|
|
|
|
if message != '':
|
2016-07-19 23:14:00 +02:00
|
|
|
text = "%s\n\n" % message
|
2016-07-13 20:12:11 +02:00
|
|
|
else:
|
|
|
|
text = "Bonjour, un évémenent a été créé.\n"
|
2016-07-13 23:34:54 +02:00
|
|
|
domain = get_current_site(request).domain
|
|
|
|
text += (
|
|
|
|
"Pour voir l'événement, allez sur %s%s"
|
|
|
|
'\nPour répondre oui allez sur %s%s\n'
|
|
|
|
'Pour répondre non, allez sur %s%s\n'
|
|
|
|
'Pour répondre peut-être, allez sur %s%s\n') \
|
|
|
|
% (domain,
|
2017-01-14 10:24:38 +01:00
|
|
|
reverse('view-event', args=[obj.id]),
|
2016-07-13 23:34:54 +02:00
|
|
|
domain,
|
2017-01-14 10:24:38 +01:00
|
|
|
reverse(
|
|
|
|
'calendrier.reponse_event',
|
|
|
|
args=[user.slug, obj.slug, "oui"]),
|
2016-07-13 23:34:54 +02:00
|
|
|
domain,
|
2017-01-14 10:24:38 +01:00
|
|
|
reverse(
|
|
|
|
'calendrier.reponse_event',
|
|
|
|
args=[user.slug, obj.slug, "non"]),
|
2016-07-13 23:34:54 +02:00
|
|
|
domain,
|
2017-01-14 10:24:38 +01:00
|
|
|
reverse(
|
|
|
|
'calendrier.reponse_event',
|
|
|
|
args=[user.slug, obj.slug, "pe"]))
|
2016-07-13 19:37:55 +02:00
|
|
|
send_mail(Subject, text, sender, receivers)
|
|
|
|
except:
|
|
|
|
erreur = "Une erreur est survenue, le mail n'a " \
|
|
|
|
+ "pas pu être envoyé."
|
2015-04-13 18:56:43 +02:00
|
|
|
else:
|
|
|
|
form = EventForm()
|
|
|
|
|
|
|
|
return render(request, "calendrier/create.html", locals())
|
2015-04-17 18:38:44 +02:00
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
@login_required
|
|
|
|
def reponse(request, id):
|
|
|
|
part = request.user.profile
|
2016-09-24 00:55:01 +02:00
|
|
|
ev = get_object_or_404(Event, id=id)
|
2015-07-22 22:08:59 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
form = ParticipantsForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
try:
|
|
|
|
p = Participants.objects.get(event=ev, participant=part)
|
|
|
|
p.delete()
|
|
|
|
except Participants.DoesNotExist:
|
|
|
|
pass
|
|
|
|
obj = form.save(commit=False)
|
|
|
|
obj.event = ev
|
|
|
|
obj.participant = part
|
|
|
|
obj.save()
|
|
|
|
envoi = True
|
|
|
|
else:
|
|
|
|
form = ParticipantsForm()
|
|
|
|
return render(request, "calendrier/reponse.html", locals())
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2017-01-14 10:24:38 +01:00
|
|
|
def reponse_event(request, codeus, codeev, coderep):
|
|
|
|
"""
|
|
|
|
Inscriptions aux événements via les liens envoyés par mail.
|
|
|
|
"""
|
|
|
|
# On vérifie qu'on a reçu une réponse valide
|
|
|
|
if coderep not in ['oui', 'non', 'pe']:
|
|
|
|
raise Http404
|
2016-09-24 00:55:01 +02:00
|
|
|
part = get_object_or_404(ErnestoUser, slug=codeus)
|
|
|
|
ev = get_object_or_404(Event, slug=codeev)
|
2017-01-14 10:24:38 +01:00
|
|
|
# On supprime la précédente réponse si elle existe
|
|
|
|
Participants.objects.filter(event=ev, participant=part).delete()
|
|
|
|
# Et on écrit la nouvelle inscription
|
|
|
|
Participants.objects.create(participant=part, event=ev, reponse=coderep)
|
2015-09-23 08:08:26 +02:00
|
|
|
return redirect('calendrier.views.home')
|
2015-07-22 22:08:59 +02:00
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-04-17 18:38:44 +02:00
|
|
|
class EventUpdate(UpdateView):
|
|
|
|
model = Event
|
|
|
|
template_name = "calendrier/update.html"
|
2015-09-23 08:08:26 +02:00
|
|
|
form_class = ModifEventForm
|
2015-04-17 18:38:44 +02:00
|
|
|
success_url = reverse_lazy(home)
|
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
@method_decorator(chef_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(EventUpdate, self).dispatch(*args, **kwargs)
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
class EventDelete(DeleteView):
|
|
|
|
model = Event
|
|
|
|
template_name = "calendrier/delete.html"
|
|
|
|
success_url = reverse_lazy(home)
|
|
|
|
|
|
|
|
@method_decorator(chef_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(EventDelete, self).dispatch(*args, **kwargs)
|