Skip to content

Commit

Permalink
Utilise les slugs des objectifs dans leurs URLs
Browse files Browse the repository at this point in the history
Au lieu de leur id.
  • Loading branch information
philippemilink authored and Arnaud-D committed Jun 18, 2023
1 parent 1480410 commit 878d523
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
4 changes: 2 additions & 2 deletions templates/tutorialv2/goals/mass-edit-goals.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
<h3>Filtrer</h3>
<ul>
<li><a href="{% url "content:mass-edit-goals" %}" class="{% if all %}selected{% endif %}">Toutes ({{ num_all }})</a></li>
<li><a href="{% url "content:mass-edit-goals" %}?non-classes" class="{% if only_not_classified %}selected{% endif %}">Sans objectif ({{ num_not_classified }})</a></li>
<li><a href="{{ url_not_classified }}" class="{% if only_not_classified %}selected{% endif %}">Sans objectif ({{ num_not_classified }})</a></li>
{% for goal in goals %}
<li>
<a href="{% url "content:mass-edit-goals" %}?objectif_{{ goal.id }}" class="{% if current_filter_pk == goal.pk %}selected{% endif %}">{{ goal.name }} ({{ goal.num_contents }})</a>
<a href="{% url "content:mass-edit-goals" %}?{{ goal.slug }}" class="{% if current_filter_pk == goal.pk %}selected{% endif %}">{{ goal.name }} ({{ goal.num_contents }})</a>
</li>
{% endfor %}
</ul>
Expand Down
4 changes: 2 additions & 2 deletions templates/tutorialv2/goals/view-goals.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
<div class="mobile-menu-bloc mobile-all-links" data-title="Filtrer">
<h3>Filtrer</h3>
<ul>
<li><a href="{% url "content:view-goals" %}?non-classes" class="{% if only_not_classified %}selected{% endif %}">Sans objectif ({{ num_not_classified }})</a></li>
<li><a href="{{ url_not_classified }}" class="{% if only_not_classified %}selected{% endif %}">Sans objectif ({{ num_not_classified }})</a></li>
{% for goal in goals %}
<li>
<a href="{% url "content:view-goals" %}?objectif_{{ goal.id }}" class="{% if current_filter_pk == goal.pk %}selected{% endif %}">{{ goal.name }} ({{ goal.num_contents }})</a>
<a href="{% url "content:view-goals" %}?{{ goal.slug }}" class="{% if current_filter_pk == goal.pk %}selected{% endif %}">{{ goal.name }} ({{ goal.num_contents }})</a>
</li>
{% endfor %}
<li><a href="{% url "content:view-goals" %}" class="{% if all %}selected{% endif %}">Toutes ({{ num_all }})</a></li>
Expand Down
2 changes: 1 addition & 1 deletion templates/tutorialv2/includes/goals.part.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
Warning: whitespace in the loop below is crucial to ensure correct rendering.
That's why it is written as a one-liner. Take care when modifying it.
{% endcomment %}
{% for goal in goals %}{% if not forloop.first %}, {% endif %}<a href="{% url "content:view-goals" %}?objectif_{{ goal.id }}">{{ goal }}</a>{% endfor %}
{% for goal in goals %}{% if not forloop.first %}, {% endif %}<a href="{% url "content:view-goals" %}?{{ goal.slug }}">{{ goal }}</a>{% endfor %}
</p>
{% endif %}
5 changes: 3 additions & 2 deletions zds/tutorialv2/tests/tests_views/tests_masseditgoals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.urls import reverse

from zds.member.tests.factories import ProfileFactory, StaffProfileFactory
from zds.tutorialv2.models.goals import Goal
from zds.tutorialv2.tests.factories import PublishableContentFactory, GoalFactory, PublishedContentFactory
from zds.tutorialv2.views.goals import MassEditGoalsForm

Expand Down Expand Up @@ -154,15 +155,15 @@ def test_filter_all(self):

def test_filter_goal(self):
"""The filter for a given goal shall display only contents with this goal."""
response = self.client.get(self.url + f"?objectif_{self.goals[0].id}")
response = self.client.get(self.url + f"?{self.goals[0].slug}")
self.assertNotContains(response, self.contents[0].title)
self.assertContains(response, self.contents[1].title)
self.assertNotContains(response, self.contents[2].title)
self.assertContains(response, self.contents[3].title)

def test_filter_no_goals(self):
"""The filter for no goals shall display only contents with no goals."""
response = self.client.get(self.url + "?non-classes")
response = self.client.get(self.url + "?" + Goal.SLUG_UNCLASSIFIED)
self.assertContains(response, self.contents[0].title)
self.assertNotContains(response, self.contents[1].title)
self.assertNotContains(response, self.contents[2].title)
Expand Down
26 changes: 24 additions & 2 deletions zds/tutorialv2/tests/tests_views/tests_viewcontentsbygoal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.urls import reverse

from zds.tutorialv2.models.goals import Goal
from zds.tutorialv2.tests.factories import GoalFactory, PublishedContentFactory


Expand All @@ -9,15 +11,35 @@ def setUp(self):
self.goals = [GoalFactory(), GoalFactory()]
self.contents = [PublishedContentFactory(), PublishedContentFactory()]

def test_page_content(self):
"""Test roughly that what is expected is in the page."""
self.contents[0].goals.add(self.goals[0])
self.contents[0].save()

def test_display_all_contents(self):
# By default, the URL without parameters display all contents:
response = self.client.get(reverse("content:view-goals"))
self.assertEqual(response.status_code, 200)
for goal in self.goals:
self.assertContains(response, goal.name)
for content in self.contents:
self.assertContains(response, content.title)

def test_display_contents_with_goal(self):
# Display only contents having the goal self.goals[0]:
response = self.client.get(reverse("content:view-goals") + "?" + self.goals[0].slug)
self.assertEqual(response.status_code, 200)
for goal in self.goals:
self.assertContains(response, goal.name)
self.assertContains(response, self.contents[0].title)
self.assertNotContains(response, self.contents[1].title)

def test_display_contents_without_goal(self):
response = self.client.get(reverse("content:view-goals") + "?" + Goal.SLUG_UNCLASSIFIED)
self.assertEqual(response.status_code, 200)
for goal in self.goals:
self.assertContains(response, goal.name)
self.assertNotContains(response, self.contents[0].title)
self.assertContains(response, self.contents[1].title)

def test_forbid_slug_unclassified(self):
goal = Goal(name="Invalid", slug=Goal.SLUG_UNCLASSIFIED)
self.assertRaises(ValidationError, goal.full_clean)
Expand Down
25 changes: 12 additions & 13 deletions zds/tutorialv2/views/goals.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ def get_queryset(self):
queryset_not_classified = self.base_queryset.filter(goals=None)
self.num_not_classified = queryset_not_classified.count()

self.only_not_classified = "non-classes" in self.request.GET
self.only_not_classified = Goal.SLUG_UNCLASSIFIED in self.request.GET
if self.only_not_classified:
return queryset_not_classified
else:
for goal in Goal.objects.all():
if f"objectif_{goal.pk}" in self.request.GET:
self.current_filter_pk = goal.pk
return self.base_queryset.filter(goals__in=[goal])
elif len(self.request.GET) > 0:
slug = list(self.request.GET.keys())[0]
goal = Goal.objects.filter(slug=slug).first()
if goal is not None:
self.current_filter_pk = goal.pk
return self.base_queryset.filter(goals__in=[goal])
return self.base_queryset

def get_context_data(self, **kwargs):
Expand All @@ -154,12 +155,7 @@ class MassEditGoals(LoginRequiredMixin, PermissionRequiredMixin, BaseFormView, C

def get_context_data(self, **kwargs):
context = {
"goals": Goal.objects.all().annotate(num_contents=Count("contents")),
"current_filter_pk": self.current_filter_pk,
"only_not_classified": self.only_not_classified,
"all": self.current_filter_pk is None and not self.only_not_classified,
"num_all": self.num_all,
"num_not_classified": self.num_not_classified,
"url_not_classified": reverse("content:mass-edit-goals") + "?" + Goal.SLUG_UNCLASSIFIED,
}
context.update(kwargs)
return super().get_context_data(**context)
Expand Down Expand Up @@ -192,6 +188,9 @@ def get_context_data(self, **kwargs):
)
else:
headline = _("Toutes les publications")
context = {"headline": headline}
context = {
"headline": headline,
"url_not_classified": reverse("content:view-goals") + "?" + Goal.SLUG_UNCLASSIFIED,
}
context.update(kwargs)
return super().get_context_data(**context)

0 comments on commit 878d523

Please sign in to comment.