diff --git a/propositions/forms.py b/propositions/forms.py deleted file mode 100644 index d3cff7c..0000000 --- a/propositions/forms.py +++ /dev/null @@ -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"}), - } diff --git a/propositions/migrations/0002_nom_verbose_name.py b/propositions/migrations/0002_nom_verbose_name.py new file mode 100644 index 0000000..02d1ef9 --- /dev/null +++ b/propositions/migrations/0002_nom_verbose_name.py @@ -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'), + ), + ] diff --git a/propositions/models.py b/propositions/models.py index e1c350f..8e61373 100644 --- a/propositions/models.py +++ b/propositions/models.py @@ -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) diff --git a/propositions/templates/propositions/create.html b/propositions/templates/propositions/create.html index 103be34..0d72e5d 100644 --- a/propositions/templates/propositions/create.html +++ b/propositions/templates/propositions/create.html @@ -1,13 +1,14 @@ {% extends "gestion/base.html" %} -{%block titre %}Proposition de morceau{% endblock %} +{% block titre %}Proposition de morceau{% endblock %} {% block content %} +

Retour aux propositions

-{% if envoi %}

Votre proposition a été enregistrée.{% endif %}

- {% csrf_token %} - {{ form.as_p }} - + {% csrf_token %} + {{ form.as_p }} +
+ {% endblock %} diff --git a/propositions/tests.py b/propositions/tests.py new file mode 100644 index 0000000..5bba748 --- /dev/null +++ b/propositions/tests.py @@ -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) diff --git a/propositions/urls.py b/propositions/urls.py index 60d5258..a598fdf 100644 --- a/propositions/urls.py +++ b/propositions/urls.py @@ -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("/oui", views.repoui, name="oui"), path("/non", views.repnon, name="non"), path("/supprimer", PropDelete.as_view(), name="delete"), diff --git a/propositions/views.py b/propositions/views.py index 483ddab..b084d8f 100644 --- a/propositions/views.py +++ b/propositions/views.py @@ -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