Skip to content

Commit

Permalink
Empêcher la création d'un objectif ayant pour slug 'non-classes'
Browse files Browse the repository at this point in the history
Ce slug est utilisé pour afficher les contenus qui n'ont aucun
objectif attribué.
  • Loading branch information
philippemilink authored and Arnaud-D committed Jun 18, 2023
1 parent 3460554 commit 1480410
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
26 changes: 26 additions & 0 deletions zds/tutorialv2/migrations/0038_goals_forbid_unclassified_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.19 on 2023-06-18 11:36

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("tutorialv2", "0037_labels"),
]

operations = [
migrations.AlterField(
model_name="goal",
name="slug",
field=models.SlugField(
max_length=80,
unique=True,
validators=[
django.core.validators.RegexValidator(
"^non-classes$", inverse_match=True, message="Ce slug est réservé."
)
],
),
),
]
12 changes: 11 additions & 1 deletion zds/tutorialv2/models/goals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import gettext_lazy as _


class Goal(models.Model):
Expand All @@ -9,14 +11,22 @@ class Goal(models.Model):
computer science, etc.) or the tags (even more precise).
"""

SLUG_UNCLASSIFIED = "non-classes"

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)
slug = models.SlugField(
max_length=80,
unique=True,
validators=[
RegexValidator("^" + SLUG_UNCLASSIFIED + "$", message=_("Ce slug est réservé."), inverse_match=True)
],
)

def __str__(self):
return self.name
10 changes: 10 additions & 0 deletions zds/tutorialv2/tests/tests_views/tests_viewcontentsbygoal.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ def test_page_content(self):
self.assertContains(response, goal.name)
for content in self.contents:
self.assertContains(response, content.title)

def test_forbid_slug_unclassified(self):
goal = Goal(name="Invalid", slug=Goal.SLUG_UNCLASSIFIED)
self.assertRaises(ValidationError, goal.full_clean)

goal = Goal(name="Almost invalid", slug=Goal.SLUG_UNCLASSIFIED + "z")
goal.full_clean()

goal = Goal(name="Valid", slug="valid-slug")
goal.full_clean()

0 comments on commit 1480410

Please sign in to comment.