diff mbox series

[v2] models: Validate Project.linkname does not contain forward slash

Message ID 20200908140606.26224-1-t@laumann.xyz
State New
Headers show
Series [v2] models: Validate Project.linkname does not contain forward slash | expand

Commit Message

Thomas Bracht Laumann Jespersen Sept. 8, 2020, 2:06 p.m. UTC
I started by creating a project that contained a forward slash
(importing patches from https://lists.sr.ht/~sircmpwn/sr.ht-dev/) and
it fails to render the "projects" main page.

The specific error reads:

    NoReverseMatch at /

    Reverse for 'patch-list' with keyword arguments
    '{'project_id': 'foo/bar'}' not found. 1 pattern(s) tried:
    ['project/(?P<project_id>[^/]+)/list/$']

which appears to explicitly disallow forward slashes.

So I think it makes sense to validate that project linkname doesn't
contain forward slahes.

Signed-off-by: Thomas Bracht Laumann Jespersen <t@laumann.xyz>
---

I simplified the check for '/' as suggested, and got the pre-commit hook to
work. I changed the formatting in the migration to satisfy the max line length
check.

 .../0044_add_project_linkname_validation.py   | 23 +++++++++++++++++++
 patchwork/models.py                           | 10 +++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 patchwork/migrations/0044_add_project_linkname_validation.py
diff mbox series

Patch

diff --git a/patchwork/migrations/0044_add_project_linkname_validation.py b/patchwork/migrations/0044_add_project_linkname_validation.py
new file mode 100644
index 0000000..8add430
--- /dev/null
+++ b/patchwork/migrations/0044_add_project_linkname_validation.py
@@ -0,0 +1,23 @@ 
+# Generated by Django 3.0.10 on 2020-09-06 22:47
+
+from django.db import migrations, models
+import patchwork.models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('patchwork', '0043_merge_patch_submission'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='project',
+            name='linkname',
+            field=models.CharField(
+                max_length=255,
+                unique=True,
+                validators=[patchwork.models.validate_project_linkname]
+            ),
+        ),
+    ]
diff --git a/patchwork/models.py b/patchwork/models.py
index 77ab924..64d5a10 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -31,6 +31,13 @@  def validate_regex_compiles(regex_string):
         raise ValidationError('Invalid regular expression entered!')
 
 
+def validate_project_linkname(linkname):
+    if '/' in linkname:
+        raise ValidationError(
+            'Invalid project linkname: Value cannot contain forward slash (/)'
+        )
+
+
 class Person(models.Model):
     # properties
 
@@ -56,7 +63,8 @@  class Person(models.Model):
 class Project(models.Model):
     # properties
 
-    linkname = models.CharField(max_length=255, unique=True)
+    linkname = models.CharField(max_length=255, unique=True,
+                                validators=[validate_project_linkname])
     name = models.CharField(max_length=255, unique=True)
     listid = models.CharField(max_length=255)
     listemail = models.CharField(max_length=200)