2020-01-05 00:32:02 +01:00
|
|
|
from django.shortcuts import 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
|
2020-01-05 00:18:31 +01:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-01-05 00:32:02 +01:00
|
|
|
from django.views.generic import CreateView, DeleteView, ListView
|
2015-07-22 22:08:59 +02:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
|
2020-01-05 15:58:16 +01:00
|
|
|
from propositions.models import Answer, Proposition
|
2015-07-22 22:08:59 +02:00
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2020-01-05 00:18:31 +01:00
|
|
|
class PropositionCreate(LoginRequiredMixin, CreateView):
|
|
|
|
template_name = "propositions/create.html"
|
|
|
|
success_url = reverse_lazy("propositions:list")
|
2020-01-05 15:58:16 +01:00
|
|
|
model = Proposition
|
|
|
|
fields = ["name", "artist", "link"]
|
2020-01-05 00:18:31 +01:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
proposition = form.save(commit=False)
|
|
|
|
proposition.user = self.request.user.profile
|
|
|
|
proposition.save()
|
|
|
|
return HttpResponseRedirect(self.success_url)
|
2015-07-22 22:08:59 +02:00
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2020-01-05 00:32:02 +01:00
|
|
|
class PropositionList(LoginRequiredMixin, ListView):
|
|
|
|
template_name = "propositions/liste.html"
|
|
|
|
context_object_name = "propositions"
|
2020-01-05 15:58:16 +01:00
|
|
|
model = Proposition
|
|
|
|
ordering = ["-nb_yes", "nb_no", "name"]
|
2016-07-14 01:58:52 +02:00
|
|
|
|
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
class PropDelete(DeleteView):
|
2020-01-05 15:58:16 +01:00
|
|
|
model = Proposition
|
2016-07-14 01:58:52 +02:00
|
|
|
template_name = "propositions/delete.html"
|
2020-01-04 00:01:16 +01:00
|
|
|
success_url = reverse_lazy("propositions:list")
|
2015-07-22 22:08:59 +02:00
|
|
|
|
|
|
|
@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):
|
2020-01-04 00:01:16 +01:00
|
|
|
return redirect("propositions:list")
|
2015-07-22 22:08:59 +02:00
|
|
|
else:
|
|
|
|
self.object.delete()
|
|
|
|
return HttpResponseRedirect(success_url)
|
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2015-07-22 22:08:59 +02:00
|
|
|
@login_required
|
2020-01-05 15:30:50 +01:00
|
|
|
def answer(request, id, rep):
|
2020-01-05 15:58:16 +01:00
|
|
|
proposition = get_object_or_404(Proposition, id=id)
|
2020-01-05 15:30:50 +01:00
|
|
|
user = request.user
|
2020-01-05 12:02:24 +01:00
|
|
|
|
2020-01-05 15:30:50 +01:00
|
|
|
old_answer = Answer.objects.filter(proposition=proposition, user=user)
|
2020-01-05 12:02:24 +01:00
|
|
|
if old_answer.exists():
|
|
|
|
old_answer = old_answer.get()
|
2020-01-05 15:30:50 +01:00
|
|
|
if old_answer.answer == Answer.YES:
|
2020-01-05 15:58:16 +01:00
|
|
|
proposition.nb_yes -= 1
|
2015-07-22 22:08:59 +02:00
|
|
|
else:
|
2020-01-05 15:58:16 +01:00
|
|
|
proposition.nb_no -= 1
|
2020-01-05 12:02:24 +01:00
|
|
|
old_answer.delete()
|
|
|
|
|
2020-01-05 15:30:50 +01:00
|
|
|
Answer.objects.create(proposition=proposition, user=user, answer=rep)
|
|
|
|
if rep == Answer.YES:
|
2020-01-05 15:58:16 +01:00
|
|
|
proposition.nb_yes += 1
|
2020-01-05 12:02:24 +01:00
|
|
|
else:
|
2020-01-05 15:58:16 +01:00
|
|
|
proposition.nb_no += 1
|
2020-01-05 12:02:24 +01:00
|
|
|
proposition.save()
|
|
|
|
|
2020-01-04 00:01:16 +01:00
|
|
|
return redirect("propositions:list")
|