ernestophone.ens.fr/propositions/views.py

74 lines
2.4 KiB
Python
Raw Normal View History

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
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
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"]
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
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"]
2015-07-22 22:08:59 +02:00
class PropDelete(DeleteView):
2020-01-05 15:58:16 +01:00
model = Proposition
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):
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()
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)
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 15:30:50 +01:00
old_answer = Answer.objects.filter(proposition=proposition, user=user)
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
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
else:
2020-01-05 15:58:16 +01:00
proposition.nb_no += 1
proposition.save()
2020-01-04 00:01:16 +01:00
return redirect("propositions:list")