2019-03-16 19:40:27 +01:00
|
|
|
from collections import defaultdict
|
2017-01-14 10:24:38 +01:00
|
|
|
from datetime import date, datetime
|
2020-09-10 15:06:53 +02:00
|
|
|
import string
|
|
|
|
import random
|
|
|
|
from django.db.models import Q
|
2017-01-14 10:24:38 +01:00
|
|
|
|
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
|
2019-03-16 19:40:27 +01:00
|
|
|
from django.db.models import Count
|
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
|
2018-01-04 23:33:31 +01:00
|
|
|
from django.urls 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
|
2020-09-10 15:06:53 +02:00
|
|
|
from actu.models import Actu
|
2015-04-13 18:56:43 +02:00
|
|
|
from calendrier.calend import EventCalendar
|
|
|
|
from calendar import monthrange
|
2020-09-10 15:06:53 +02:00
|
|
|
from calendrier.forms import ModifEventForm, EventForm, ParticipantsForm, ChangeDoodleName
|
2017-01-14 10:24:38 +01:00
|
|
|
from calendrier.models import Event, Participants
|
|
|
|
from partitions.decorators import chef_required
|
2020-09-10 15:06:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generer(*args):
|
|
|
|
caracteres = string.ascii_letters + string.digits
|
|
|
|
aleatoire = [random.choice(caracteres) for _ in range(6)]
|
|
|
|
return ''.join(aleatoire)
|
|
|
|
|
|
|
|
def liste(request):
|
|
|
|
if request.user.is_authenticated :
|
|
|
|
return redirect('calendrier:home')
|
|
|
|
lToday = datetime.today()
|
|
|
|
events_a_venir = Event.objects.filter(date__gte = lToday).exclude(calendrier__iexact = 'F')
|
|
|
|
events_passe = Event.objects.filter(date__lt = lToday).filter(calendrier__iexact = 'H')
|
|
|
|
return render(request, 'calendrier/agenda.html', {"events_a_venir": events_a_venir,"events_passe":events_passe})
|
2015-07-22 22:08:59 +02:00
|
|
|
|
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')
|
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
@login_required
|
2015-04-13 18:56:43 +02:00
|
|
|
def home(request):
|
|
|
|
lToday = datetime.now()
|
|
|
|
return calendar(request, lToday.year, lToday.month)
|
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
@login_required
|
2015-04-13 18:56:43 +02:00
|
|
|
def calendar(request, pYear, pMonth):
|
2020-09-10 15:06:53 +02:00
|
|
|
actu = Actu.objects.all()
|
|
|
|
lToday = datetime.now()
|
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)
|
2018-01-05 00:12:03 +01: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
|
2020-09-10 15:06:53 +02:00
|
|
|
try:
|
|
|
|
events_a_venir_not_answered = Event.objects.filter(date__gte = lToday).exclude(participants__participant=request.user.profile)
|
|
|
|
events_a_venir_answered = Event.objects.filter(date__gte = lToday).filter(participants__participant=request.user.profile)
|
|
|
|
events_a_venir_answered_yes = Event.objects.filter(date__gte = lToday).filter(Q(participants__participant=request.user.profile )& Q(participants__reponse= 'oui'))
|
|
|
|
events_a_venir_answered_no = Event.objects.filter(date__gte = lToday).filter(Q(participants__participant=request.user.profile) & Q(participants__reponse= 'non'))
|
|
|
|
events_a_venir_answered_pe = Event.objects.filter(date__gte = lToday).filter(Q(participants__participant=request.user.profile)& Q(participants__reponse= 'pe'))
|
|
|
|
except:
|
|
|
|
events_a_venir_not_answered = Event.objects.filter(date__gte = lToday).exclude(participants__participant__user__username=request.user)
|
|
|
|
events_a_venir_answered = Event.objects.filter(date__gte = lToday).filter(participants__participant__user__username=request.user)
|
|
|
|
events_a_venir_answered_yes = Event.objects.filter(date__gte = lToday).filter(Q(participants__participant__user__username=request.user )& Q(participants__reponse= 'oui'))
|
|
|
|
events_a_venir_answered_no = Event.objects.filter(date__gte = lToday).filter(Q(participants__participant__user__username=request.user) & Q(participants__reponse= 'non'))
|
|
|
|
events_a_venir_answered_pe = Event.objects.filter(date__gte = lToday).filter(Q(participants__participant__user__username=request.user)& Q(participants__reponse= 'pe'))
|
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,
|
2020-09-10 15:06:53 +02:00
|
|
|
"events_a_venir_answered_yes": events_a_venir_answered_yes,
|
|
|
|
"events_a_venir_answered_no": events_a_venir_answered_no,
|
|
|
|
"events_a_venir_answered_pe": events_a_venir_answered_pe,
|
|
|
|
"events_a_venir_not_answered": events_a_venir_not_answered,
|
|
|
|
"actu" : actu,
|
2015-04-13 18:56:43 +02:00
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
})
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
@login_required
|
2019-03-16 18:54:29 +01:00
|
|
|
def view_event(request, id):
|
2019-03-16 19:09:12 +01:00
|
|
|
event = get_object_or_404(Event, id=id)
|
2019-03-16 19:40:27 +01:00
|
|
|
participants = event.participants_set.all()
|
2020-09-10 15:06:53 +02:00
|
|
|
multi_instrumentistes = event.participants_set.filter(Q(participant__multi_instrumentiste = 'Oui')& ~Q(reponse = 'non'))
|
2019-03-16 19:09:12 +01:00
|
|
|
|
|
|
|
# Restricted event, only erneso users can see it
|
|
|
|
if not request.user.is_authenticated and not event.calendrier:
|
2018-01-05 13:57:17 +01:00
|
|
|
return redirect(reverse('calendrier:home'))
|
2019-03-16 19:09:12 +01:00
|
|
|
|
2019-03-16 19:40:27 +01:00
|
|
|
# Count the number of occurences of each instrument
|
2020-09-10 15:06:53 +02:00
|
|
|
instrument_count = defaultdict(lambda: (0, 0,[],[],[]))
|
2019-03-16 19:40:27 +01:00
|
|
|
for participant in participants:
|
|
|
|
instrument = participant.participant.instru
|
2020-09-10 15:06:53 +02:00
|
|
|
if (instrument == "Autre"):
|
|
|
|
instrument = participant.participant.instru_autre
|
|
|
|
|
|
|
|
sure, maybe, namesoui, namespe, namesnon = instrument_count[instrument]
|
|
|
|
|
2019-03-16 19:40:27 +01:00
|
|
|
if participant.reponse == "oui":
|
2020-09-10 15:06:53 +02:00
|
|
|
|
|
|
|
namesoui += [participant.participant.get_doodlename()]
|
|
|
|
instrument_count[instrument] = (sure + 1, maybe,namesoui,namespe,namesnon)
|
2019-03-16 19:40:27 +01:00
|
|
|
elif participant.reponse == "pe":
|
2020-09-10 15:06:53 +02:00
|
|
|
namespe += [participant.participant.get_doodlename()]
|
|
|
|
instrument_count[instrument] = (sure, maybe + 1,namesoui,namespe,namesnon)
|
|
|
|
else:
|
|
|
|
namesnon += [participant.participant.get_doodlename()]
|
|
|
|
instrument_count[instrument] = (sure, maybe,namesoui,namespe,namesnon)
|
|
|
|
|
2019-03-16 19:40:27 +01:00
|
|
|
instrument_count = [
|
2020-09-10 15:06:53 +02:00
|
|
|
(instrument, sure, maybe,namesoui, namespe ,namesnon)
|
|
|
|
for instrument, (sure, maybe,namesoui,namespe,namesnon) in instrument_count.items()
|
2019-03-16 19:40:27 +01:00
|
|
|
]
|
|
|
|
|
2019-03-16 19:09:12 +01:00
|
|
|
context = {
|
|
|
|
"event": event,
|
2019-03-16 19:40:27 +01:00
|
|
|
"instrument_count": instrument_count,
|
2019-03-16 19:09:12 +01:00
|
|
|
"participants": participants,
|
|
|
|
"nboui": len(participants.filter(reponse="oui")),
|
|
|
|
"nbpe": len(participants.filter(reponse="pe")),
|
|
|
|
"nbnon": len(participants.filter(reponse="non")),
|
2020-09-10 15:06:53 +02:00
|
|
|
"multi_instrumentistes":multi_instrumentistes,
|
2019-03-16 19:09:12 +01:00
|
|
|
}
|
|
|
|
return render(request, 'calendrier/view_event.html', context=context)
|
2015-04-13 18:56:43 +02:00
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
|
|
|
|
2016-06-21 00:33:32 +02:00
|
|
|
@login_required
|
2020-09-10 15:06:53 +02:00
|
|
|
def changename(request,id):
|
2016-06-21 00:33:32 +02:00
|
|
|
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
|
2020-09-10 15:06:53 +02:00
|
|
|
return redirect('calendrier:view-event',id=id)
|
2016-06-21 00:33:32 +02:00
|
|
|
else:
|
|
|
|
form = ChangeDoodleName(instance=request.user)
|
2020-09-10 15:06:53 +02:00
|
|
|
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
|
|
|
nom = form.cleaned_data["nom"]
|
|
|
|
date = form.cleaned_data["date"]
|
|
|
|
date = date.strftime('%d/%m/%Y')
|
|
|
|
debut = form.cleaned_data["debut"]
|
|
|
|
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
|
2020-09-10 15:06:53 +02:00
|
|
|
return redirect('calendrier:view-event',id=id)
|
|
|
|
|
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
|
|
|
|
2020-09-10 15:06:53 +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
|
2020-09-10 15:06:53 +02:00
|
|
|
return redirect('calendrier:view-event',id=id)
|
2015-07-22 22:08:59 +02:00
|
|
|
else:
|
|
|
|
form = ParticipantsForm()
|
|
|
|
return render(request, "calendrier/reponse.html", locals())
|
|
|
|
|
2016-07-13 19:37:55 +02:00
|
|
|
|
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
|
2020-09-10 15:06:53 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
ctx = super(EventUpdate, self).get_context_data(**kwargs)
|
|
|
|
ctx['id'] = self.get_object().id
|
|
|
|
return ctx
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('calendrier:view-event', kwargs={'id' : self.get_object().id})
|
2015-04-17 18:38:44 +02:00
|
|
|
|
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"
|
2020-09-10 15:06:53 +02:00
|
|
|
success_url = reverse_lazy("calendrier:home")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
ctx = super(EventDelete, self).get_context_data(**kwargs)
|
|
|
|
ctx['id'] = self.get_object().id
|
|
|
|
return ctx
|
2015-07-22 22:08:59 +02:00
|
|
|
|
|
|
|
@method_decorator(chef_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
2020-09-10 15:06:53 +02:00
|
|
|
return super(EventDelete,self).dispatch(*args, **kwargs)
|