New implementation + tests for Prop creation
This commit is contained in:
parent
a0b307f829
commit
dc2cd07d93
7 changed files with 87 additions and 36 deletions
|
@ -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"}),
|
|
||||||
}
|
|
18
propositions/migrations/0002_nom_verbose_name.py
Normal file
18
propositions/migrations/0002_nom_verbose_name.py
Normal 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'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -8,7 +8,7 @@ REP = (
|
||||||
|
|
||||||
|
|
||||||
class Prop(models.Model):
|
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)
|
artiste = models.CharField(blank=True, max_length=100)
|
||||||
user = models.ForeignKey(ErnestoUser, on_delete=models.CASCADE, verbose_name="Proposé par")
|
user = models.ForeignKey(ErnestoUser, on_delete=models.CASCADE, verbose_name="Proposé par")
|
||||||
lien = models.URLField(blank=True)
|
lien = models.URLField(blank=True)
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
{% extends "gestion/base.html" %}
|
{% extends "gestion/base.html" %}
|
||||||
|
|
||||||
{%block titre %}Proposition de morceau{% endblock %}
|
{% block titre %}Proposition de morceau{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
<p><a href="{% url "propositions:list" %}">Retour aux propositions</a></p>
|
<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">
|
<form action="{% url "propositions:create" %}" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{ form.as_p }}
|
{{ form.as_p }}
|
||||||
<input type="submit" value="Enregistrer" />
|
<input type="submit" value="Enregistrer" />
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
48
propositions/tests.py
Normal file
48
propositions/tests.py
Normal 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)
|
|
@ -6,7 +6,7 @@ from propositions import views
|
||||||
app_name = "propositions"
|
app_name = "propositions"
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.liste, name="list"),
|
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>/oui", views.repoui, name="oui"),
|
||||||
path("<int:id>/non", views.repnon, name="non"),
|
path("<int:id>/non", views.repnon, name="non"),
|
||||||
path("<int:pk>/supprimer", PropDelete.as_view(), name="delete"),
|
path("<int:pk>/supprimer", PropDelete.as_view(), name="delete"),
|
||||||
|
|
|
@ -1,29 +1,26 @@
|
||||||
from django.shortcuts import render, redirect, get_object_or_404
|
from django.shortcuts import render, redirect, get_object_or_404
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
from django.contrib.auth.decorators import login_required
|
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.utils.decorators import method_decorator
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
|
|
||||||
from propositions.forms import PropForm
|
|
||||||
from propositions.models import Prop
|
from propositions.models import Prop
|
||||||
from propositions.models import Reponses
|
from propositions.models import Reponses
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
class PropositionCreate(LoginRequiredMixin, CreateView):
|
||||||
def create_prop(request):
|
template_name = "propositions/create.html"
|
||||||
if request.method == "POST":
|
success_url = reverse_lazy("propositions:list")
|
||||||
form = PropForm(request.POST)
|
model = Prop
|
||||||
if form.is_valid():
|
fields = ["nom", "artiste", "lien"]
|
||||||
obj = form.save(commit=False)
|
|
||||||
obj.nboui = 0
|
def form_valid(self, form):
|
||||||
obj.nbnon = 0
|
proposition = form.save(commit=False)
|
||||||
obj.user = request.user.profile
|
proposition.user = self.request.user.profile
|
||||||
obj.save()
|
proposition.save()
|
||||||
envoi = True
|
return HttpResponseRedirect(self.success_url)
|
||||||
else:
|
|
||||||
form = PropForm()
|
|
||||||
return render(request, "propositions/create.html", locals())
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
|
Loading…
Reference in a new issue