262 lines
9.4 KiB
Python
262 lines
9.4 KiB
Python
from django.shortcuts import render, redirect
|
|
from calendrier.forms import ModifEventForm, EventForm, ParticipantsForm, SendMail
|
|
from calendrier.models import Event, Participants
|
|
from django.utils.safestring import mark_safe
|
|
from django.views.generic import UpdateView, DeleteView
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from partitions.decorators import chef_required
|
|
from django.contrib.auth.decorators import login_required
|
|
from gestion.models import ErnestoUser
|
|
from django.utils.decorators import method_decorator
|
|
from django.core.mail import send_mail
|
|
|
|
import smtplib
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
|
|
from calendrier.calend import EventCalendar
|
|
from calendar import monthrange
|
|
from datetime import *
|
|
|
|
from propositions.utils import generer
|
|
|
|
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
|
|
part = ev.participants_set.all()
|
|
nboui = len(part.filter(reponse="oui"))
|
|
nbpe = len(part.filter(reponse="pe"))
|
|
nbnon = len(part.filter(reponse="non"))
|
|
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)
|
|
part = ev.participants_set.all()
|
|
nom = ev.nom.capitalize
|
|
fin = False
|
|
desc = False
|
|
nboui = len(part.filter(reponse="oui"))
|
|
nbpe = len(part.filter(reponse="pe"))
|
|
nbnon = len(part.filter(reponse="non"))
|
|
if ev.fin:
|
|
fin = True
|
|
if ev.description:
|
|
desc = True
|
|
return render(request, 'calendrier/view_event.html', locals())
|
|
|
|
@chef_required
|
|
def resend(request, id):
|
|
if request.method=="POST":
|
|
form = SendMail(request.POST)
|
|
if form.is_valid():
|
|
ev = Event.objects.get(id=id)
|
|
users = ErnestoUser.objects.filter(is_ernesto=True)
|
|
message = form.cleaned_data["message"]
|
|
try:
|
|
for user in users:
|
|
Subject = ev.nom + ", le " + ev.date.strftime('%d/%m/%Y')
|
|
sender = 'fanfare@ens.fr'
|
|
receivers = [user.user.email]
|
|
text = "Bonjour, un évémenent a été créé."
|
|
if message != '':
|
|
text = message
|
|
text+= '\nPour répondre oui, allez sur http://www.ernestophone.ens.fr/calendar/' + user.slug + '/' + ev.slug + '/oui'
|
|
text+= '\nPour répondre non, allez sur http://www.ernestophone.ens.fr/calendar/' + user.slug + '/' + ev.slug + '/non'
|
|
text+= '\nPour répondre peut-être, allez sur http://www.ernestophone.ens.fr/calendar/' + user.slug + '/' + ev.slug + '/pe'
|
|
send_mail(Subject, text, sender, receivers)
|
|
return redirect("calendrier.views.view_eventbis", id)
|
|
except:
|
|
erreur = "Une erreur est survenue, le mail n'a pas pu être envoyé."
|
|
else:
|
|
form = SendMail()
|
|
return render(request, "calendrier/resend.html", locals())
|
|
|
|
|
|
@chef_required
|
|
def create_event(request):
|
|
if request.method == "POST":
|
|
form = EventForm(request.POST)
|
|
if form.is_valid():
|
|
temp = True
|
|
while temp:
|
|
code = generer()
|
|
try:
|
|
Event.objects.get(slug=code)
|
|
except:
|
|
temp=False
|
|
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()
|
|
id= obj.id
|
|
envoi = True
|
|
if sendmail:
|
|
users = ErnestoUser.objects.filter(is_ernesto=True)
|
|
for user in users:
|
|
try:
|
|
Subject = nom + ", le " + date
|
|
sender = 'fanfare@ens.fr'
|
|
receivers = [user.user.email]
|
|
text = "Bonjour, un évémenent a été créé."
|
|
if message != '':
|
|
text = message
|
|
text+= '\nPour répondre oui, allez sur http://www.ernestophone.ens.fr/calendar/' + user.slug + '/' + obj.slug + '/oui'
|
|
text+= '\nPour répondre non, allez sur http://www.ernestophone.ens.fr/calendar/' + user.slug + '/' + obj.slug + '/non'
|
|
text+= '\nPour répondre peut-être, allez sur http://www.ernestophone.ens.fr/calendar/' + user.slug + '/' + obj.slug + '/pe'
|
|
send_mail(Subject, text, sender, receivers)
|
|
# smtpObj = smtplib.SMTP('clipper.ens.fr', 25)
|
|
# msg = MIMEMultipart("alternative")
|
|
# msg["From"] = sender
|
|
# msg["To"] = receivers
|
|
# text = MIMEText(text, 'plain')
|
|
# msg.attach(text)
|
|
# smtpObj.sendmail(sender, receivers, msg.as_string())
|
|
except:
|
|
erreur = "Une erreur est survenue, le mail n'a pas pu être envoyé."
|
|
else:
|
|
form = EventForm()
|
|
|
|
return render(request, "calendrier/create.html", locals())
|
|
|
|
@login_required
|
|
def reponse(request, id):
|
|
part = request.user.profile
|
|
ev = Event.objects.get(id=id)
|
|
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())
|
|
|
|
def repouidir(request, codeus, codeev):
|
|
part = ErnestoUser.objects.get(slug=codeus)
|
|
ev = Event.objects.get(slug=codeev)
|
|
try:
|
|
p = Participants.objects.get(event=ev, participant=part)
|
|
p.delete()
|
|
except Participants.DoesNotExist:
|
|
pass
|
|
obj = Participants()
|
|
obj.participant = part
|
|
obj.event = ev
|
|
obj.reponse="oui"
|
|
obj.save()
|
|
envoi = True
|
|
return redirect('calendrier.views.home')
|
|
|
|
|
|
def reppedir(request, codeus, codeev):
|
|
part = ErnestoUser.objects.get(slug=codeus)
|
|
ev = Event.objects.get(slug=codeev)
|
|
try:
|
|
p = Participants.objects.get(event=ev, participant=part)
|
|
p.delete()
|
|
except Participants.DoesNotExist:
|
|
pass
|
|
obj = Participants()
|
|
obj.participant = part
|
|
obj.event = ev
|
|
obj.reponse="pe"
|
|
obj.save()
|
|
envoi = True
|
|
|
|
def repnondir(request, codeus, codeev):
|
|
part = ErnestoUser.objects.get(slug=codeus)
|
|
ev = Event.objects.get(slug=codeev)
|
|
try:
|
|
p = Participants.objects.get(event=ev, participant=part)
|
|
p.delete()
|
|
except Participants.DoesNotExist:
|
|
pass
|
|
obj = Participants()
|
|
obj.participant = part
|
|
obj.event = ev
|
|
obj.reponse="non"
|
|
obj.save()
|
|
envoi = True
|
|
return redirect('calendrier.views.home')
|
|
|
|
class EventUpdate(UpdateView):
|
|
model = Event
|
|
template_name = "calendrier/update.html"
|
|
form_class = ModifEventForm
|
|
success_url = reverse_lazy(home)
|
|
|
|
@method_decorator(chef_required)
|
|
def dispatch(self, *args, **kwargs):
|
|
return super(EventUpdate, self).dispatch(*args, **kwargs)
|
|
|
|
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)
|
|
|
|
# Create your views here.
|