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()
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
class Prop(models.Model):
|
class Proposition(models.Model):
|
||||||
nom = models.CharField(max_length=100, verbose_name="nom du morceau")
|
name = models.CharField(max_length=100, verbose_name="nom du morceau")
|
||||||
artiste = models.CharField(blank=True, max_length=100)
|
artist = models.CharField(blank=True, max_length=100)
|
||||||
user = models.ForeignKey(ErnestoUser, on_delete=models.CASCADE, verbose_name="Proposé par")
|
user = models.ForeignKey(
|
||||||
lien = models.URLField(blank=True)
|
ErnestoUser, on_delete=models.CASCADE, verbose_name="Proposé par"
|
||||||
nboui = models.IntegerField(default=0, verbose_name="oui")
|
)
|
||||||
nbnon = models.IntegerField(default=0, verbose_name="non")
|
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):
|
def __str__(self):
|
||||||
return self.nom
|
return self.name
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = "Proposition"
|
verbose_name = "Proposition de morceau"
|
||||||
|
verbose_name_plural = "Propositions de morceaux"
|
||||||
|
|
||||||
|
|
||||||
class Answer(models.Model):
|
class Answer(models.Model):
|
||||||
|
@ -29,7 +32,7 @@ class Answer(models.Model):
|
||||||
(NO, "Non")
|
(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)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
answer = models.CharField("Réponse", max_length=3, choices=REP_CHOICES)
|
answer = models.CharField("Réponse", max_length=3, choices=REP_CHOICES)
|
||||||
|
|
||||||
|
|
|
@ -21,12 +21,12 @@
|
||||||
{% for p in propositions %}
|
{% for p in propositions %}
|
||||||
<tr class="prop">
|
<tr class="prop">
|
||||||
<td>
|
<td>
|
||||||
{% if p.lien %}<a href={{ p.lien }}>{% endif %}
|
{% if p.link %}<a href={{ p.link }}>{% endif %}
|
||||||
<b>{{ p.nom }}</b>{% if p.artiste %} - {{ p.artiste }}{% endif %}
|
<b>{{ p.name }}</b>{% if p.artist %} - {{ p.artist }}{% endif %}
|
||||||
{% if p.lien %}</a>{% endif %}
|
{% if p.link %}</a>{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>{{ p.nboui }}</td>
|
<td>{{ p.nb_yes }}</td>
|
||||||
<td>{{ p.nbnon }}</td>
|
<td>{{ p.nb_no }}</td>
|
||||||
<td><a href="{% url "propositions:oui" p.id %}">Oui</a></td>
|
<td><a href="{% url "propositions:oui" p.id %}">Oui</a></td>
|
||||||
<td><a href="{% url "propositions:non" p.id %}">Non</a></td>
|
<td><a href="{% url "propositions:non" p.id %}">Non</a></td>
|
||||||
{% getresponse request.user p %}
|
{% getresponse request.user p %}
|
||||||
|
|
|
@ -3,7 +3,7 @@ from django.test import Client, TestCase
|
||||||
from django.urls import reverse_lazy, reverse
|
from django.urls import reverse_lazy, reverse
|
||||||
|
|
||||||
from gestion.models import ErnestoUser
|
from gestion.models import ErnestoUser
|
||||||
from propositions.models import Answer, Prop
|
from propositions.models import Answer, Proposition
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
@ -34,18 +34,18 @@ class PropositionCreateTest(TestCase):
|
||||||
client = Client()
|
client = Client()
|
||||||
client.force_login(user)
|
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)
|
client.post(self.url, data)
|
||||||
proposition = Prop.objects.all()
|
proposition = Proposition.objects.all()
|
||||||
self.assertEqual(1, proposition.count())
|
self.assertEqual(1, proposition.count())
|
||||||
|
|
||||||
proposition = proposition.get()
|
proposition = proposition.get()
|
||||||
self.assertEqual(proposition.nom, "foo")
|
self.assertEqual(proposition.name, "foo")
|
||||||
self.assertEqual(proposition.artiste, "bar")
|
self.assertEqual(proposition.artist, "bar")
|
||||||
self.assertEqual(proposition.lien, "http://example.com")
|
self.assertEqual(proposition.link, "http://example.com")
|
||||||
self.assertEqual(proposition.user, user.profile)
|
self.assertEqual(proposition.user, user.profile)
|
||||||
self.assertEqual(proposition.nboui, 0)
|
self.assertEqual(proposition.nb_yes, 0)
|
||||||
self.assertEqual(proposition.nbnon, 0)
|
self.assertEqual(proposition.nb_no, 0)
|
||||||
|
|
||||||
|
|
||||||
class PropositionListTest(TestCase):
|
class PropositionListTest(TestCase):
|
||||||
|
@ -54,10 +54,10 @@ class PropositionListTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.user = new_user("toto")
|
self.user = new_user("toto")
|
||||||
for name in ["foo", "bar", "baz"]:
|
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)
|
Answer.objects.create(proposition=p, user=self.user, answer=Answer.YES)
|
||||||
for name in ["oof", "rab", "zab"]:
|
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)
|
Answer.objects.create(proposition=p, user=self.user, answer=Answer.NO)
|
||||||
|
|
||||||
def test_anonymous_get(self):
|
def test_anonymous_get(self):
|
||||||
|
@ -75,7 +75,7 @@ class PropositionListTest(TestCase):
|
||||||
class ReponseTest(TestCase):
|
class ReponseTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.user = new_user("toto")
|
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):
|
def _url(self, rep):
|
||||||
assert rep in ["oui", "non"]
|
assert rep in ["oui", "non"]
|
||||||
|
@ -99,11 +99,11 @@ class ReponseTest(TestCase):
|
||||||
# Oui
|
# Oui
|
||||||
client.get(reverse("propositions:oui", args=(self.prop.id,)))
|
client.get(reverse("propositions:oui", args=(self.prop.id,)))
|
||||||
self.prop.refresh_from_db()
|
self.prop.refresh_from_db()
|
||||||
self.assertEqual(self.prop.nboui, 1)
|
self.assertEqual(self.prop.nb_yes, 1)
|
||||||
self.assertEqual(self.prop.nbnon, 0)
|
self.assertEqual(self.prop.nb_no, 0)
|
||||||
|
|
||||||
# Oui
|
# Oui
|
||||||
client.get(reverse("propositions:non", args=(self.prop.id,)))
|
client.get(reverse("propositions:non", args=(self.prop.id,)))
|
||||||
self.prop.refresh_from_db()
|
self.prop.refresh_from_db()
|
||||||
self.assertEqual(self.prop.nboui, 0)
|
self.assertEqual(self.prop.nb_yes, 0)
|
||||||
self.assertEqual(self.prop.nbnon, 1)
|
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.utils.decorators import method_decorator
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
|
|
||||||
from propositions.models import Prop
|
from propositions.models import Answer, Proposition
|
||||||
from propositions.models import Answer
|
|
||||||
|
|
||||||
|
|
||||||
class PropositionCreate(LoginRequiredMixin, CreateView):
|
class PropositionCreate(LoginRequiredMixin, CreateView):
|
||||||
template_name = "propositions/create.html"
|
template_name = "propositions/create.html"
|
||||||
success_url = reverse_lazy("propositions:list")
|
success_url = reverse_lazy("propositions:list")
|
||||||
model = Prop
|
model = Proposition
|
||||||
fields = ["nom", "artiste", "lien"]
|
fields = ["name", "artist", "link"]
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
proposition = form.save(commit=False)
|
proposition = form.save(commit=False)
|
||||||
|
@ -26,12 +25,12 @@ class PropositionCreate(LoginRequiredMixin, CreateView):
|
||||||
class PropositionList(LoginRequiredMixin, ListView):
|
class PropositionList(LoginRequiredMixin, ListView):
|
||||||
template_name = "propositions/liste.html"
|
template_name = "propositions/liste.html"
|
||||||
context_object_name = "propositions"
|
context_object_name = "propositions"
|
||||||
model = Prop
|
model = Proposition
|
||||||
ordering = ["-nboui", "nbnon", "nom"]
|
ordering = ["-nb_yes", "nb_no", "name"]
|
||||||
|
|
||||||
|
|
||||||
class PropDelete(DeleteView):
|
class PropDelete(DeleteView):
|
||||||
model = Prop
|
model = Proposition
|
||||||
template_name = "propositions/delete.html"
|
template_name = "propositions/delete.html"
|
||||||
success_url = reverse_lazy("propositions:list")
|
success_url = reverse_lazy("propositions:list")
|
||||||
|
|
||||||
|
@ -52,23 +51,23 @@ class PropDelete(DeleteView):
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def answer(request, id, rep):
|
def answer(request, id, rep):
|
||||||
proposition = get_object_or_404(Prop, id=id)
|
proposition = get_object_or_404(Proposition, id=id)
|
||||||
user = request.user
|
user = request.user
|
||||||
|
|
||||||
old_answer = Answer.objects.filter(proposition=proposition, user=user)
|
old_answer = Answer.objects.filter(proposition=proposition, user=user)
|
||||||
if old_answer.exists():
|
if old_answer.exists():
|
||||||
old_answer = old_answer.get()
|
old_answer = old_answer.get()
|
||||||
if old_answer.answer == Answer.YES:
|
if old_answer.answer == Answer.YES:
|
||||||
proposition.nboui -= 1
|
proposition.nb_yes -= 1
|
||||||
else:
|
else:
|
||||||
proposition.nbnon -= 1
|
proposition.nb_no -= 1
|
||||||
old_answer.delete()
|
old_answer.delete()
|
||||||
|
|
||||||
Answer.objects.create(proposition=proposition, user=user, answer=rep)
|
Answer.objects.create(proposition=proposition, user=user, answer=rep)
|
||||||
if rep == Answer.YES:
|
if rep == Answer.YES:
|
||||||
proposition.nboui += 1
|
proposition.nb_yes += 1
|
||||||
else:
|
else:
|
||||||
proposition.nbnon += 1
|
proposition.nb_no += 1
|
||||||
proposition.save()
|
proposition.save()
|
||||||
|
|
||||||
return redirect("propositions:list")
|
return redirect("propositions:list")
|
||||||
|
|
Loading…
Add table
Reference in a new issue