2016-09-24 00:55:01 +02:00
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
2018-01-04 23:33:31 +01:00
|
|
|
from django.urls import reverse_lazy
|
2015-07-22 22:08:59 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.views.generic import DeleteView
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
|
|
|
|
from propositions.forms import PropForm
|
|
|
|
from propositions.models import Prop
|
|
|
|
from propositions.models import Reponses
|
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
@login_required
|
|
|
|
def create_prop(request):
|
|
|
|
if request.method == "POST":
|
|
|
|
form = PropForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
obj = form.save(commit=False)
|
|
|
|
obj.nboui = 0
|
|
|
|
obj.nbnon = 0
|
|
|
|
obj.user = request.user.profile
|
|
|
|
obj.save()
|
|
|
|
envoi = True
|
|
|
|
else:
|
|
|
|
form = PropForm()
|
|
|
|
return render(request, "propositions/create.html", locals())
|
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
@login_required
|
|
|
|
def liste(request):
|
|
|
|
props = Prop.objects.all().order_by('-nboui', 'nbnon', 'nom')
|
|
|
|
n = len(props)
|
|
|
|
return render(request, 'propositions/liste.html', locals())
|
2016-07-14 01:58:52 +02:00
|
|
|
|
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
@login_required
|
|
|
|
def repoui(request, id):
|
2016-09-24 00:55:01 +02:00
|
|
|
prop = get_object_or_404(Prop, id=id)
|
2015-07-22 22:08:59 +02:00
|
|
|
participant = request.user.profile
|
|
|
|
try:
|
|
|
|
p = Reponses.objects.get(prop=prop, part=participant)
|
|
|
|
if p.reponse == "oui":
|
|
|
|
prop.nboui -= 1
|
|
|
|
else:
|
2016-07-14 01:58:52 +02:00
|
|
|
prop.nbnon -= 1
|
2015-07-22 22:08:59 +02:00
|
|
|
p.delete()
|
|
|
|
except Reponses.DoesNotExist:
|
|
|
|
pass
|
|
|
|
rep = Reponses()
|
|
|
|
rep.prop = prop
|
|
|
|
rep.part = participant
|
|
|
|
rep.reponse = "oui"
|
|
|
|
rep.save()
|
|
|
|
prop.nboui += 1
|
|
|
|
prop.save()
|
2016-07-14 01:58:52 +02:00
|
|
|
return redirect('propositions.views.liste')
|
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
|
|
|
|
class PropDelete(DeleteView):
|
|
|
|
model = Prop
|
2016-07-14 01:58:52 +02:00
|
|
|
template_name = "propositions/delete.html"
|
2015-07-22 22:08:59 +02:00
|
|
|
success_url = reverse_lazy(liste)
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
2016-07-14 01:58:52 +02:00
|
|
|
return super(PropDelete, self).dispatch(*args, **kwargs)
|
2015-07-22 22:08:59 +02:00
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
success_url = self.get_success_url()
|
2016-07-14 01:58:52 +02:00
|
|
|
if not ((self.object.user == self.request.user.profile)
|
|
|
|
or self.request.user.profile.is_chef):
|
2015-07-22 22:08:59 +02:00
|
|
|
return redirect('propositions.views.liste')
|
|
|
|
else:
|
|
|
|
self.object.delete()
|
|
|
|
return HttpResponseRedirect(success_url)
|
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2016-06-21 00:33:32 +02:00
|
|
|
@login_required
|
|
|
|
def test(request, pk):
|
|
|
|
error = "Connard"
|
|
|
|
if True:
|
|
|
|
return redirect(PropDelete.as_view())
|
|
|
|
return render(request, "propositions/liste.html", locals())
|
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
@login_required
|
|
|
|
def repnon(request, id):
|
2016-09-24 00:55:01 +02:00
|
|
|
prop = get_object_or_404(Prop, id=id)
|
2015-07-22 22:08:59 +02:00
|
|
|
participant = request.user.profile
|
|
|
|
try:
|
|
|
|
p = Reponses.objects.get(prop=prop, part=participant)
|
|
|
|
if p.reponse == "oui":
|
|
|
|
prop.nboui -= 1
|
|
|
|
else:
|
|
|
|
prop.nbnon -= 1
|
|
|
|
p.delete()
|
|
|
|
except Reponses.DoesNotExist:
|
|
|
|
pass
|
|
|
|
rep = Reponses()
|
|
|
|
rep.prop = prop
|
|
|
|
rep.part = participant
|
|
|
|
rep.reponse = "non"
|
|
|
|
rep.save()
|
|
|
|
prop.nbnon += 1
|
|
|
|
prop.save()
|
|
|
|
return redirect('propositions.views.liste')
|