Factorize the code for propositions' answers

This commit is contained in:
Martin Pépin 2020-01-05 12:02:24 +01:00
parent 022583f0a9
commit 88f62a56be
No known key found for this signature in database
GPG key ID: E7520278B1774448
3 changed files with 58 additions and 43 deletions

View file

@ -1,6 +1,6 @@
from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse_lazy
from django.urls import reverse_lazy, reverse
from gestion.models import ErnestoUser
from propositions.models import Prop
@ -62,3 +62,40 @@ class PropositionListTest(TestCase):
response = client.get(self.url)
self.assertEqual(response.status_code, 200)
class ReponseTest(TestCase):
def setUp(self):
self.user = new_user("toto")
self.prop = Prop.objects.create(nom="foo", user=self.user.profile)
def _url(self, rep):
assert rep in ["oui", "non"]
return reverse("propositions:{}".format(rep), args=(self.prop.id,))
def test_anonymous_get(self):
client = Client()
url = reverse("propositions:oui", args=(self.prop.id,))
response = client.get(url)
self.assertRedirects(response, "/login?next={}".format(url))
url = reverse("propositions:non", args=(self.prop.id,))
response = client.get(url)
self.assertRedirects(response, "/login?next={}".format(url))
def test_get(self):
client = Client()
client.force_login(self.user)
# Oui
client.get(reverse("propositions:oui", args=(self.prop.id,)))
self.prop.refresh_from_db()
self.assertEqual(self.prop.nboui, 1)
self.assertEqual(self.prop.nbnon, 0)
# Oui
client.get(reverse("propositions:non", args=(self.prop.id,)))
self.prop.refresh_from_db()
self.assertEqual(self.prop.nboui, 0)
self.assertEqual(self.prop.nbnon, 1)

View file

@ -7,7 +7,7 @@ app_name = "propositions"
urlpatterns = [
path("", views.PropositionList.as_view(), name="list"),
path("new", views.PropositionCreate.as_view(), name="create"),
path("<int:id>/oui", views.repoui, name="oui"),
path("<int:id>/non", views.repnon, name="non"),
path("<int:id>/oui", views.reponse, {"rep": "oui"}, name="oui"),
path("<int:id>/non", views.reponse, {"rep": "non"}, name="non"),
path("<int:pk>/supprimer", PropDelete.as_view(), name="delete"),
]

View file

@ -30,29 +30,6 @@ class PropositionList(LoginRequiredMixin, ListView):
ordering = ["-nboui", "nbnon", "nom"]
@login_required
def repoui(request, id):
prop = get_object_or_404(Prop, id=id)
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 = "oui"
rep.save()
prop.nboui += 1
prop.save()
return redirect("propositions:list")
class PropDelete(DeleteView):
model = Prop
template_name = "propositions/delete.html"
@ -74,23 +51,24 @@ class PropDelete(DeleteView):
@login_required
def repnon(request, id):
prop = get_object_or_404(Prop, id=id)
def reponse(request, id, rep):
proposition = get_object_or_404(Prop, id=id)
participant = request.user.profile
try:
p = Reponses.objects.get(prop=prop, part=participant)
if p.reponse == "oui":
prop.nboui -= 1
old_answer = Reponses.objects.filter(prop=proposition, part=participant)
if old_answer.exists():
old_answer = old_answer.get()
if old_answer.reponse == "oui":
proposition.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()
proposition.nbnon -= 1
old_answer.delete()
Reponses.objects.create(prop=proposition, part=participant, reponse=rep)
if rep == "oui":
proposition.nboui += 1
else:
proposition.nbnon += 1
proposition.save()
return redirect("propositions:list")