Refactor the Prop(osition) model
This commit is contained in:
parent
dbe54f075e
commit
9b6cc2b486
5 changed files with 87 additions and 42 deletions
43
propositions/migrations/0004_prop_renaming_and_cleaning.py
Normal file
43
propositions/migrations/0004_prop_renaming_and_cleaning.py
Normal file
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -21,12 +21,12 @@
|
|||
{% for p in propositions %}
|
||||
<tr class="prop">
|
||||
<td>
|
||||
{% if p.lien %}<a href={{ p.lien }}>{% endif %}
|
||||
<b>{{ p.nom }}</b>{% if p.artiste %} - {{ p.artiste }}{% endif %}
|
||||
{% if p.lien %}</a>{% endif %}
|
||||
{% if p.link %}<a href={{ p.link }}>{% endif %}
|
||||
<b>{{ p.name }}</b>{% if p.artist %} - {{ p.artist }}{% endif %}
|
||||
{% if p.link %}</a>{% endif %}
|
||||
</td>
|
||||
<td>{{ p.nboui }}</td>
|
||||
<td>{{ p.nbnon }}</td>
|
||||
<td>{{ p.nb_yes }}</td>
|
||||
<td>{{ p.nb_no }}</td>
|
||||
<td><a href="{% url "propositions:oui" p.id %}">Oui</a></td>
|
||||
<td><a href="{% url "propositions:non" p.id %}">Non</a></td>
|
||||
{% getresponse request.user p %}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue