diff --git a/propositions/templates/propositions/liste.html b/propositions/templates/propositions/liste.html index 33a9ec2..4bf8d8f 100644 --- a/propositions/templates/propositions/liste.html +++ b/propositions/templates/propositions/liste.html @@ -5,13 +5,10 @@ {% block content %}

Liste des propositions

- {% if error %} -

{{ error }}

- {% endif %}

Proposer un morceau

- {% if n > 0 %} + {% if propositions.exists %} @@ -21,7 +18,7 @@ - {% for p in props %} + {% for p in propositions %} diff --git a/propositions/tests.py b/propositions/tests.py index 5bba748..74fd464 100644 --- a/propositions/tests.py +++ b/propositions/tests.py @@ -46,3 +46,19 @@ class PropositionCreateTest(TestCase): self.assertEqual(proposition.user, user.profile) self.assertEqual(proposition.nboui, 0) self.assertEqual(proposition.nbnon, 0) + + +class PropositionListTest(TestCase): + url = reverse_lazy("propositions:list") + + 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) diff --git a/propositions/urls.py b/propositions/urls.py index a598fdf..e7c260b 100644 --- a/propositions/urls.py +++ b/propositions/urls.py @@ -5,7 +5,7 @@ from propositions import views app_name = "propositions" urlpatterns = [ - path("", views.liste, name="list"), + path("", views.PropositionList.as_view(), name="list"), path("new", views.PropositionCreate.as_view(), name="create"), path("/oui", views.repoui, name="oui"), path("/non", views.repnon, name="non"), diff --git a/propositions/views.py b/propositions/views.py index b084d8f..ad66b84 100644 --- a/propositions/views.py +++ b/propositions/views.py @@ -1,8 +1,8 @@ -from django.shortcuts import render, redirect, get_object_or_404 +from django.shortcuts import redirect, get_object_or_404 from django.urls import reverse_lazy from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin -from django.views.generic import CreateView, DeleteView +from django.views.generic import CreateView, DeleteView, ListView from django.utils.decorators import method_decorator from django.http import HttpResponseRedirect @@ -23,11 +23,11 @@ class PropositionCreate(LoginRequiredMixin, CreateView): return HttpResponseRedirect(self.success_url) -@login_required -def liste(request): - props = Prop.objects.all().order_by('-nboui', 'nbnon', 'nom') - n = len(props) - return render(request, 'propositions/liste.html', locals()) +class PropositionList(LoginRequiredMixin, ListView): + template_name = "propositions/liste.html" + context_object_name = "propositions" + model = Prop + ordering = ["-nboui", "nbnon", "nom"] @login_required
{% if p.lien %}{% endif %}{{ p.nom }}{% if p.artiste %} - {{ p.artiste }}{% endif %}{% if p.lien %}{% endif %}