diff --git a/propositions/migrations/0004_prop_renaming_and_cleaning.py b/propositions/migrations/0004_prop_renaming_and_cleaning.py new file mode 100644 index 0000000..0a4efea --- /dev/null +++ b/propositions/migrations/0004_prop_renaming_and_cleaning.py @@ -0,0 +1,43 @@ +# Generated by Django 2.2.9 on 2020-01-05 14:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("propositions", "0003_reponse_renaming_and_cleaning"), + ] + + operations = [ + migrations.AlterModelOptions( + name="prop", + options={ + "verbose_name": "Proposition de morceau", + "verbose_name_plural": "Propositions de morceaux", + }, + ), + migrations.RenameField( + model_name="prop", old_name="artiste", new_name="artist", + ), + migrations.RenameField(model_name="prop", old_name="lien", new_name="link"), + migrations.RenameField(model_name="prop", old_name="nom", new_name="name"), + migrations.RenameField(model_name="prop", old_name="nbnon", new_name="nb_no"), + migrations.RenameField(model_name="prop", old_name="nboui", new_name="nb_yes"), + migrations.AlterField( + model_name="prop", + name="nb_no", + field=models.IntegerField(default=0, verbose_name="nombre de réponses non"), + ), + migrations.AlterField( + model_name="prop", + name="nb_yes", + field=models.IntegerField(default=0, verbose_name="nombre de réponses oui"), + ), + migrations.RenameModel(old_name="prop", new_name="proposition"), + migrations.AlterField( + model_name='answer', + name='proposition', + field=models.ForeignKey(on_delete=models.deletion.CASCADE, to='propositions.Proposition'), + ), + ] diff --git a/propositions/models.py b/propositions/models.py index 4453cab..7f4eda0 100644 --- a/propositions/models.py +++ b/propositions/models.py @@ -5,19 +5,22 @@ from gestion.models import ErnestoUser User = get_user_model() -class Prop(models.Model): - nom = models.CharField(max_length=100, verbose_name="nom du morceau") - artiste = models.CharField(blank=True, max_length=100) - user = models.ForeignKey(ErnestoUser, on_delete=models.CASCADE, verbose_name="Proposé par") - lien = models.URLField(blank=True) - nboui = models.IntegerField(default=0, verbose_name="oui") - nbnon = models.IntegerField(default=0, verbose_name="non") +class Proposition(models.Model): + name = models.CharField(max_length=100, verbose_name="nom du morceau") + artist = models.CharField(blank=True, max_length=100) + user = models.ForeignKey( + ErnestoUser, on_delete=models.CASCADE, verbose_name="Proposé par" + ) + link = models.URLField(blank=True) + nb_yes = models.IntegerField(default=0, verbose_name="nombre de réponses oui") + nb_no = models.IntegerField(default=0, verbose_name="nombre de réponses non") def __str__(self): - return self.nom + return self.name class Meta: - verbose_name = "Proposition" + verbose_name = "Proposition de morceau" + verbose_name_plural = "Propositions de morceaux" class Answer(models.Model): @@ -29,7 +32,7 @@ class Answer(models.Model): (NO, "Non") ] - proposition = models.ForeignKey(Prop, on_delete=models.CASCADE) + proposition = models.ForeignKey(Proposition, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE) answer = models.CharField("Réponse", max_length=3, choices=REP_CHOICES) diff --git a/propositions/templates/propositions/liste.html b/propositions/templates/propositions/liste.html index 4209350..62b74eb 100644 --- a/propositions/templates/propositions/liste.html +++ b/propositions/templates/propositions/liste.html @@ -21,12 +21,12 @@ {% for p in propositions %} - {% if p.lien %}{% endif %} - {{ p.nom }}{% if p.artiste %} - {{ p.artiste }}{% endif %} - {% if p.lien %}{% endif %} + {% if p.link %}{% endif %} + {{ p.name }}{% if p.artist %} - {{ p.artist }}{% endif %} + {% if p.link %}{% endif %} - {{ p.nboui }} - {{ p.nbnon }} + {{ p.nb_yes }} + {{ p.nb_no }} Oui Non {% getresponse request.user p %} diff --git a/propositions/tests.py b/propositions/tests.py index aa96060..e78a053 100644 --- a/propositions/tests.py +++ b/propositions/tests.py @@ -3,7 +3,7 @@ from django.test import Client, TestCase from django.urls import reverse_lazy, reverse from gestion.models import ErnestoUser -from propositions.models import Answer, Prop +from propositions.models import Answer, Proposition User = get_user_model() @@ -34,18 +34,18 @@ class PropositionCreateTest(TestCase): client = Client() client.force_login(user) - data = {"nom": "foo", "artiste": "bar", "lien": "example.com"} + data = {"name": "foo", "artist": "bar", "link": "example.com"} client.post(self.url, data) - proposition = Prop.objects.all() + proposition = Proposition.objects.all() self.assertEqual(1, proposition.count()) proposition = proposition.get() - self.assertEqual(proposition.nom, "foo") - self.assertEqual(proposition.artiste, "bar") - self.assertEqual(proposition.lien, "http://example.com") + self.assertEqual(proposition.name, "foo") + self.assertEqual(proposition.artist, "bar") + self.assertEqual(proposition.link, "http://example.com") self.assertEqual(proposition.user, user.profile) - self.assertEqual(proposition.nboui, 0) - self.assertEqual(proposition.nbnon, 0) + self.assertEqual(proposition.nb_yes, 0) + self.assertEqual(proposition.nb_no, 0) class PropositionListTest(TestCase): @@ -54,10 +54,10 @@ class PropositionListTest(TestCase): def setUp(self): self.user = new_user("toto") for name in ["foo", "bar", "baz"]: - p = Prop.objects.create(nom=name, user=self.user.profile) + p = Proposition.objects.create(name=name, user=self.user.profile) Answer.objects.create(proposition=p, user=self.user, answer=Answer.YES) for name in ["oof", "rab", "zab"]: - p = Prop.objects.create(nom=name, user=self.user.profile) + p = Proposition.objects.create(name=name, user=self.user.profile) Answer.objects.create(proposition=p, user=self.user, answer=Answer.NO) def test_anonymous_get(self): @@ -75,7 +75,7 @@ class PropositionListTest(TestCase): class ReponseTest(TestCase): def setUp(self): self.user = new_user("toto") - self.prop = Prop.objects.create(nom="foo", user=self.user.profile) + self.prop = Proposition.objects.create(name="foo", user=self.user.profile) def _url(self, rep): assert rep in ["oui", "non"] @@ -99,11 +99,11 @@ class ReponseTest(TestCase): # 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) + self.assertEqual(self.prop.nb_yes, 1) + self.assertEqual(self.prop.nb_no, 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) + self.assertEqual(self.prop.nb_yes, 0) + self.assertEqual(self.prop.nb_no, 1) diff --git a/propositions/views.py b/propositions/views.py index 021903f..b7e8506 100644 --- a/propositions/views.py +++ b/propositions/views.py @@ -6,15 +6,14 @@ from django.views.generic import CreateView, DeleteView, ListView from django.utils.decorators import method_decorator from django.http import HttpResponseRedirect -from propositions.models import Prop -from propositions.models import Answer +from propositions.models import Answer, Proposition class PropositionCreate(LoginRequiredMixin, CreateView): template_name = "propositions/create.html" success_url = reverse_lazy("propositions:list") - model = Prop - fields = ["nom", "artiste", "lien"] + model = Proposition + fields = ["name", "artist", "link"] def form_valid(self, form): proposition = form.save(commit=False) @@ -26,12 +25,12 @@ class PropositionCreate(LoginRequiredMixin, CreateView): class PropositionList(LoginRequiredMixin, ListView): template_name = "propositions/liste.html" context_object_name = "propositions" - model = Prop - ordering = ["-nboui", "nbnon", "nom"] + model = Proposition + ordering = ["-nb_yes", "nb_no", "name"] class PropDelete(DeleteView): - model = Prop + model = Proposition template_name = "propositions/delete.html" success_url = reverse_lazy("propositions:list") @@ -52,23 +51,23 @@ class PropDelete(DeleteView): @login_required def answer(request, id, rep): - proposition = get_object_or_404(Prop, id=id) + proposition = get_object_or_404(Proposition, id=id) user = request.user old_answer = Answer.objects.filter(proposition=proposition, user=user) if old_answer.exists(): old_answer = old_answer.get() if old_answer.answer == Answer.YES: - proposition.nboui -= 1 + proposition.nb_yes -= 1 else: - proposition.nbnon -= 1 + proposition.nb_no -= 1 old_answer.delete() Answer.objects.create(proposition=proposition, user=user, answer=rep) if rep == Answer.YES: - proposition.nboui += 1 + proposition.nb_yes += 1 else: - proposition.nbnon += 1 + proposition.nb_no += 1 proposition.save() return redirect("propositions:list")