New implementation + tests for Prop creation

This commit is contained in:
Martin Pépin 2020-01-05 00:18:31 +01:00
parent a0b307f829
commit dc2cd07d93
No known key found for this signature in database
GPG key ID: E7520278B1774448
7 changed files with 87 additions and 36 deletions

View file

@ -1,13 +0,0 @@
from django import forms
from propositions.models import Prop
class PropForm(forms.ModelForm):
class Meta:
model = Prop
fields = ('nom', 'artiste', 'lien')
widgets = {
'lien': forms.TextInput(attrs={"placeholder": "facultatif"}),
'nom': forms.TextInput(attrs={"placeholder": "Nom du morceau"}),
'artiste': forms.TextInput(attrs={"placeholder": "facultatif"}),
}

View file

@ -0,0 +1,18 @@
# Generated by Django 2.2.9 on 2020-01-04 23:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('propositions', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='prop',
name='nom',
field=models.CharField(max_length=100, verbose_name='nom du morceau'),
),
]

View file

@ -8,7 +8,7 @@ REP = (
class Prop(models.Model):
nom = models.CharField(max_length=100)
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)

View file

@ -1,13 +1,14 @@
{% extends "gestion/base.html" %}
{%block titre %}Proposition de morceau{% endblock %}
{% block titre %}Proposition de morceau{% endblock %}
{% block content %}
<p><a href="{% url "propositions:list" %}">Retour aux propositions</a></p>
{% if envoi %}<p>Votre proposition a été enregistrée.{% endif %}
<form action="{% url "propositions:create" %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Enregistrer" />
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Enregistrer" />
</form>
{% endblock %}

48
propositions/tests.py Normal file
View file

@ -0,0 +1,48 @@
from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse_lazy
from gestion.models import ErnestoUser
from propositions.models import Prop
User = get_user_model()
def new_user(username):
u = User.objects.create_user(username=username)
ErnestoUser.objects.create(user=u, slug=username, is_ernesto=True)
return u
class PropositionCreateTest(TestCase):
url = reverse_lazy("propositions:create")
def test_anonymous_get(self):
response = Client().get(self.url)
self.assertRedirects(response, "/login?next={}".format(self.url))
def test_get(self):
user = new_user("toto")
client = Client()
client.force_login(user)
response = client.get(self.url)
self.assertEqual(response.status_code, 200)
def test_post(self):
user = new_user("toto")
client = Client()
client.force_login(user)
data = {"nom": "foo", "artiste": "bar", "lien": "example.com"}
client.post(self.url, data)
proposition = Prop.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.user, user.profile)
self.assertEqual(proposition.nboui, 0)
self.assertEqual(proposition.nbnon, 0)

View file

@ -6,7 +6,7 @@ from propositions import views
app_name = "propositions"
urlpatterns = [
path("", views.liste, name="list"),
path("new", views.create_prop, name="create"),
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:pk>/supprimer", PropDelete.as_view(), name="delete"),

View file

@ -1,29 +1,26 @@
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.views.generic import DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView, DeleteView
from django.utils.decorators import method_decorator
from django.http import HttpResponseRedirect
from propositions.forms import PropForm
from propositions.models import Prop
from propositions.models import Reponses
@login_required
def create_prop(request):
if request.method == "POST":
form = PropForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.nboui = 0
obj.nbnon = 0
obj.user = request.user.profile
obj.save()
envoi = True
else:
form = PropForm()
return render(request, "propositions/create.html", locals())
class PropositionCreate(LoginRequiredMixin, CreateView):
template_name = "propositions/create.html"
success_url = reverse_lazy("propositions:list")
model = Prop
fields = ["nom", "artiste", "lien"]
def form_valid(self, form):
proposition = form.save(commit=False)
proposition.user = self.request.user.profile
proposition.save()
return HttpResponseRedirect(self.success_url)
@login_required