diff options
author | 2021-03-26 19:30:03 +0100 | |
---|---|---|
committer | 2021-03-26 19:39:35 +0100 | |
commit | 163460967916463485a8193dde092eef639c9bea (patch) | |
tree | 32912010bb99fc605116a15edf1a9acf88e33217 /pydis_site/apps/api/models | |
parent | Merge branch 'main' into doc-validator (diff) |
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
Diffstat (limited to 'pydis_site/apps/api/models')
-rw-r--r-- | pydis_site/apps/api/models/bot/documentation_link.py | 20 |
1 files changed, 13 insertions, 7 deletions
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." |