From 6516eff584065ed121109b7874cb080f72c5e3cc Mon Sep 17 00:00:00 2001 From: Numerlor <25886452+Numerlor@users.noreply.github.com> Date: Mon, 3 Aug 2020 22:16:27 +0200 Subject: Add a validator for package names. Package names are used for stats in the bot and are restricted to the a-z_ char set, a validator is added to accommodate this restriction at the site admin side. --- pydis_site/apps/api/models/bot/documentation_link.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'pydis_site/apps/api/models') diff --git a/pydis_site/apps/api/models/bot/documentation_link.py b/pydis_site/apps/api/models/bot/documentation_link.py index 5a46460b..f77d6f38 100644 --- a/pydis_site/apps/api/models/bot/documentation_link.py +++ b/pydis_site/apps/api/models/bot/documentation_link.py @@ -1,3 +1,4 @@ +from django.core.validators import RegexValidator from django.db import models from pydis_site.apps.api.models.mixins import ModelReprMixin @@ -9,6 +10,12 @@ class DocumentationLink(ModelReprMixin, models.Model): package = models.CharField( primary_key=True, max_length=50, + validators=( + RegexValidator( + regex=r"^[a-z_]+$", + message="Package names can only consist of lowercase a-z letters and underscores." + ), + ), help_text="The Python package name that this documentation link belongs to." ) base_url = models.URLField( -- cgit v1.2.3 From b7f302e4e7afefb16a652d3b0524f4cf4ee835e9 Mon Sep 17 00:00:00 2001 From: Numerlor <25886452+Numerlor@users.noreply.github.com> Date: Tue, 4 Aug 2020 11:29:26 +0200 Subject: Add ascii digits to the validator. Some packages can contain them and are good for stats. --- pydis_site/apps/api/models/bot/documentation_link.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/api/models') diff --git a/pydis_site/apps/api/models/bot/documentation_link.py b/pydis_site/apps/api/models/bot/documentation_link.py index f77d6f38..e093af59 100644 --- a/pydis_site/apps/api/models/bot/documentation_link.py +++ b/pydis_site/apps/api/models/bot/documentation_link.py @@ -12,8 +12,8 @@ class DocumentationLink(ModelReprMixin, models.Model): max_length=50, validators=( RegexValidator( - regex=r"^[a-z_]+$", - message="Package names can only consist of lowercase a-z letters and underscores." + regex=r"^[a-z0-9_]+$", + message="Package names can only consist of lowercase a-z letters, digits, and underscores." ), ), help_text="The Python package name that this documentation link belongs to." -- cgit v1.2.3 From b242f6d4893dd47c8f07df7dd7bf97f8f1c6631c Mon Sep 17 00:00:00 2001 From: Numerlor <25886452+Numerlor@users.noreply.github.com> Date: Tue, 4 Aug 2020 12:20:52 +0200 Subject: Move package name validator definition. The move prevents it going through the line limit and deeper nesting of parentheses from splitting up the string. --- pydis_site/apps/api/models/bot/documentation_link.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'pydis_site/apps/api/models') diff --git a/pydis_site/apps/api/models/bot/documentation_link.py b/pydis_site/apps/api/models/bot/documentation_link.py index e093af59..56b47ae6 100644 --- a/pydis_site/apps/api/models/bot/documentation_link.py +++ b/pydis_site/apps/api/models/bot/documentation_link.py @@ -3,6 +3,11 @@ from django.db import models from pydis_site.apps.api.models.mixins import ModelReprMixin +package_name_validator = RegexValidator( + regex=r"^[a-z0-9_]+$", + message="Package names can only consist of lowercase a-z letters, digits, and underscores." +) + class DocumentationLink(ModelReprMixin, models.Model): """A documentation link used by the `!docs` command of the bot.""" @@ -10,12 +15,7 @@ class DocumentationLink(ModelReprMixin, models.Model): package = models.CharField( primary_key=True, max_length=50, - validators=( - RegexValidator( - regex=r"^[a-z0-9_]+$", - message="Package names can only consist of lowercase a-z letters, digits, and underscores." - ), - ), + validators=package_name_validator, help_text="The Python package name that this documentation link belongs to." ) base_url = models.URLField( -- cgit v1.2.3 From 62cf63427e51f2a03eb37d726ca9a6a12fed7374 Mon Sep 17 00:00:00 2001 From: Numerlor <25886452+Numerlor@users.noreply.github.com> Date: Tue, 4 Aug 2020 16:00:43 +0200 Subject: Fix package name validator definition. The validators kwarg expects an iterable of validators, while a validator directly was being supplied. --- pydis_site/apps/api/models/bot/documentation_link.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'pydis_site/apps/api/models') diff --git a/pydis_site/apps/api/models/bot/documentation_link.py b/pydis_site/apps/api/models/bot/documentation_link.py index 56b47ae6..4f2bd2ab 100644 --- a/pydis_site/apps/api/models/bot/documentation_link.py +++ b/pydis_site/apps/api/models/bot/documentation_link.py @@ -3,9 +3,11 @@ from django.db import models from pydis_site.apps.api.models.mixins import ModelReprMixin -package_name_validator = RegexValidator( - regex=r"^[a-z0-9_]+$", - message="Package names can only consist of lowercase a-z letters, digits, and underscores." +package_name_validator = ( + RegexValidator( + regex=r"^[a-z0-9_]+$", + message="Package names can only consist of lowercase a-z letters, digits, and underscores." + ), ) -- cgit v1.2.3 From 163460967916463485a8193dde092eef639c9bea Mon Sep 17 00:00:00 2001 From: Numerlor <25886452+Numerlor@users.noreply.github.com> Date: Fri, 26 Mar 2021 19:30:03 +0100 Subject: Ensure the base url ends with a slash URLs without a trailing slash won't get properly joined by the bot Adds compatibility with python-discord/bot@bc25bfdf42cdaaba924a7ad6de1dc06a9b381285's changes The styling of how the regex decorator is constructed was changed to be consisted with the function validator --- ...0069_documentationlink_packagename_validator.py | 19 ---------------- .../0069_documentationlink_validators.py | 25 ++++++++++++++++++++++ .../apps/api/models/bot/documentation_link.py | 20 +++++++++++------ 3 files changed, 38 insertions(+), 26 deletions(-) delete mode 100644 pydis_site/apps/api/migrations/0069_documentationlink_packagename_validator.py create mode 100644 pydis_site/apps/api/migrations/0069_documentationlink_validators.py (limited to 'pydis_site/apps/api/models') diff --git a/pydis_site/apps/api/migrations/0069_documentationlink_packagename_validator.py b/pydis_site/apps/api/migrations/0069_documentationlink_packagename_validator.py deleted file mode 100644 index 4234e633..00000000 --- a/pydis_site/apps/api/migrations/0069_documentationlink_packagename_validator.py +++ /dev/null @@ -1,19 +0,0 @@ -# Generated by Django 3.0.11 on 2021-03-14 23:22 - -import django.core.validators -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('api', '0068_split_nomination_tables'), - ] - - operations = [ - migrations.AlterField( - model_name='documentationlink', - name='package', - field=models.CharField(help_text='The Python package name that this documentation link belongs to.', max_length=50, primary_key=True, serialize=False, validators=[django.core.validators.RegexValidator(message='Package names can only consist of lowercase a-z letters, digits, and underscores.', regex='^[a-z0-9_]+$')]), - ), - ] diff --git a/pydis_site/apps/api/migrations/0069_documentationlink_validators.py b/pydis_site/apps/api/migrations/0069_documentationlink_validators.py new file mode 100644 index 00000000..347c0e1a --- /dev/null +++ b/pydis_site/apps/api/migrations/0069_documentationlink_validators.py @@ -0,0 +1,25 @@ +# Generated by Django 3.0.11 on 2021-03-26 18:21 + +import django.core.validators +from django.db import migrations, models +import pydis_site.apps.api.models.bot.documentation_link + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0068_split_nomination_tables'), + ] + + operations = [ + migrations.AlterField( + model_name='documentationlink', + name='base_url', + field=models.URLField(help_text='The base URL from which documentation will be available for this project. Used to generate links to various symbols within this package.', validators=[pydis_site.apps.api.models.bot.documentation_link.ends_with_slash_validator]), + ), + migrations.AlterField( + model_name='documentationlink', + name='package', + field=models.CharField(help_text='The Python package name that this documentation link belongs to.', max_length=50, primary_key=True, serialize=False, validators=[django.core.validators.RegexValidator(message='Package names can only consist of lowercase a-z letters, digits, and underscores.', regex='^[a-z0-9_]+$')]), + ), + ] diff --git a/pydis_site/apps/api/models/bot/documentation_link.py b/pydis_site/apps/api/models/bot/documentation_link.py index 529d26d1..3dcc71fc 100644 --- a/pydis_site/apps/api/models/bot/documentation_link.py +++ b/pydis_site/apps/api/models/bot/documentation_link.py @@ -1,30 +1,36 @@ +from django.core.exceptions import ValidationError from django.core.validators import RegexValidator from django.db import models from pydis_site.apps.api.models.mixins import ModelReprMixin -package_name_validator = ( - RegexValidator( - regex=r"^[a-z0-9_]+$", - message="Package names can only consist of lowercase a-z letters, digits, and underscores." - ), +package_name_validator = RegexValidator( + regex=r"^[a-z0-9_]+$", + message="Package names can only consist of lowercase a-z letters, digits, and underscores." ) +def ends_with_slash_validator(string: str) -> None: + """Raise a ValidationError if `string` does not end with a slash.""" + if not string.endswith("/"): + raise ValidationError("The entered URL must end with a slash.") + + class DocumentationLink(ModelReprMixin, models.Model): """A documentation link used by the `!docs` command of the bot.""" package = models.CharField( primary_key=True, max_length=50, - validators=package_name_validator, + validators=(package_name_validator,), help_text="The Python package name that this documentation link belongs to." ) base_url = models.URLField( help_text=( "The base URL from which documentation will be available for this project. " "Used to generate links to various symbols within this package." - ) + ), + validators=(ends_with_slash_validator,) ) inventory_url = models.URLField( help_text="The URL at which the Sphinx inventory is available for this package." -- cgit v1.2.3