-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Premier jalon pour la classification par objectif des contenus (#6354)
* Ajoute les modèles et l'admin pour la classification par objectifs * Ajoute une modale d'édition pour les objectifs
- Loading branch information
Showing
15 changed files
with
306 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
- model: tutorialv2.Goal | ||
pk: 1 | ||
fields: | ||
name: Comprendre | ||
description: Publications pour comprendre quelque chose à quelque chose d'autre. | ||
position: 0 | ||
slug: comprendre | ||
- model: tutorialv2.Goal | ||
pk: 2 | ||
fields: | ||
name: Apprendre | ||
description: Qui n'apprend rien n'a rien. | ||
position: 1 | ||
slug: apprendre | ||
- model: tutorialv2.Goal | ||
pk: 3 | ||
fields: | ||
name: Exprimer une opinion | ||
description: Ayez un avis sur tout. | ||
position: 2 | ||
slug: exprimer-opinion | ||
- model: tutorialv2.Goal | ||
pk: 4 | ||
fields: | ||
name: Nouvelles de la communauté | ||
description: Nous sommes des communards, après tout. | ||
position: 3 | ||
slug: nouvelles-communaute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generated by Django 3.2.13 on 2022-07-15 09:27 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("tutorialv2", "0033_move_helpwriting"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Goal", | ||
fields=[ | ||
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("name", models.CharField(max_length=80, verbose_name="Nom")), | ||
("description", models.TextField(blank=True, verbose_name="Description")), | ||
("position", models.IntegerField(db_index=True, default=0, verbose_name="Position")), | ||
("slug", models.SlugField(max_length=80, unique=True)), | ||
], | ||
options={ | ||
"verbose_name": "Objectif", | ||
"verbose_name_plural": "Objectifs", | ||
}, | ||
), | ||
migrations.AddField( | ||
model_name="publishablecontent", | ||
name="goals", | ||
field=models.ManyToManyField( | ||
blank=True, db_index=True, to="tutorialv2.Goal", verbose_name="Objectifs du contenu" | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.db import models | ||
|
||
|
||
class Goal(models.Model): | ||
""" | ||
This model represents the categories used for the goal-based classification of publications. | ||
The goals are categories like "understand", "discover", "learn", "give an opinion", etc. | ||
They are thus distinct from the thematic categories and subcategories (physics, | ||
computer science, etc.) or the tags (even more precise). | ||
""" | ||
|
||
class Meta: | ||
verbose_name = "Objectif" | ||
verbose_name_plural = "Objectifs" | ||
|
||
name = models.CharField("Nom", max_length=80) | ||
description = models.TextField("Description", blank=True) | ||
position = models.IntegerField("Position", default=0, db_index=True) | ||
slug = models.SlugField(max_length=80, unique=True) | ||
|
||
def __str__(self): | ||
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from unittest.mock import patch | ||
|
||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from zds.member.tests.factories import ProfileFactory, StaffProfileFactory | ||
from zds.tutorialv2.tests.factories import PublishableContentFactory, GoalFactory | ||
|
||
|
||
class EditGoalsPermissionTests(TestCase): | ||
def setUp(self): | ||
self.user = ProfileFactory().user | ||
self.author = ProfileFactory().user | ||
self.staff = StaffProfileFactory().user | ||
self.content = PublishableContentFactory() | ||
self.content.authors.add(self.author) | ||
self.good_url = reverse("content:edit-goals", kwargs={"pk": self.content.pk}) | ||
self.bad_url = reverse("content:edit-goals", kwargs={"pk": 42}) | ||
self.content_url = reverse("content:view", kwargs={"pk": self.content.pk, "slug": self.content.slug}) | ||
self.success_url = self.content_url | ||
|
||
def test_display(self): | ||
"""We shall display the form only for staff, not for authors.""" | ||
fragment = "Modifier les objectifs" | ||
|
||
self.client.force_login(self.author) | ||
response = self.client.get(self.content_url) | ||
self.assertNotContains(response, fragment) | ||
|
||
self.client.force_login(self.staff) | ||
response = self.client.get(self.content_url) | ||
self.assertContains(response, fragment) | ||
|
||
def test_get_method(self): | ||
""" | ||
GET is forbidden, since the view processes the form but do not display anything. | ||
Actually, all methods except POST are forbidden, but the test is good enough as is. | ||
""" | ||
self.client.force_login(self.staff) | ||
response = self.client.get(self.good_url) | ||
self.assertEqual(response.status_code, 405) | ||
|
||
def test_unauthenticated_not_existing_pk(self): | ||
"""Invalid pks in URL""" | ||
self.client.logout() | ||
response = self.client.post(self.bad_url) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_unauthenticated_redirected(self): | ||
"""As login is required, unauthenticated users shall be redirected to the login page.""" | ||
self.client.logout() | ||
response = self.client.post(self.good_url) | ||
self.login_url = f"{reverse('member-login')}?next={self.good_url}" | ||
self.assertRedirects(response, self.login_url) | ||
|
||
def test_simple_user_forbidden(self): | ||
"""Simple users shall not be able to access to the view.""" | ||
self.client.force_login(self.user) | ||
response = self.client.post(self.good_url) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
def test_staff_authorized(self): | ||
"""Staff shall have access to the view.""" | ||
self.client.force_login(self.staff) | ||
response = self.client.post(self.good_url) | ||
self.assertRedirects(response, self.success_url) | ||
|
||
|
||
class EditGoalsFunctionalTests(TestCase): | ||
def setUp(self): | ||
self.staff = StaffProfileFactory().user | ||
self.content = PublishableContentFactory() | ||
self.content = PublishableContentFactory() | ||
self.url = reverse("content:edit-goals", kwargs={"pk": self.content.pk}) | ||
self.goals = [GoalFactory() for _ in range(3)] | ||
|
||
@patch("zds.tutorialv2.signals.goals_management") | ||
def test_goals_updated(self, goals_management): | ||
self.client.force_login(self.staff) | ||
response = self.client.post(self.url, {"goals": [goal.pk for goal in self.goals]}, follow=True) | ||
self.assertEqual(list(self.content.goals.all()), self.goals) | ||
self.assertContains(response, "alert-box success") | ||
self.assertEqual(goals_management.send.call_count, 1) | ||
|
||
@patch("zds.tutorialv2.signals.goals_management") | ||
def test_invalid_parameters(self, goals_management): | ||
self.client.force_login(self.staff) | ||
response = self.client.post(self.url, {"goals": [42]}, follow=True) | ||
self.assertEqual(list(self.content.goals.all()), []) | ||
self.assertContains(response, "alert-box alert") | ||
self.assertFalse(goals_management.send.called) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.