aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/models
diff options
context:
space:
mode:
authorGravatar Mark <[email protected]>2021-04-19 00:02:48 -0700
committerGravatar GitHub <[email protected]>2021-04-19 00:02:48 -0700
commit8cd9e3b3bc307dd6f586bbb789d96a13228bf245 (patch)
tree8baba14d03013fa3155ba5a93d8b1c92d868e92d /pydis_site/apps/api/models
parentMerge pull request #472 from python-discord/ks123/ghcr-token-to-github (diff)
parentMerge branch 'main' into doc-validator (diff)
Merge pull request #373 from Numerlor/doc-validator
Add a validator for documentation package names and base urls.
Diffstat (limited to 'pydis_site/apps/api/models')
-rw-r--r--pydis_site/apps/api/models/bot/documentation_link.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/pydis_site/apps/api/models/bot/documentation_link.py b/pydis_site/apps/api/models/bot/documentation_link.py
index 2a0ce751..3dcc71fc 100644
--- a/pydis_site/apps/api/models/bot/documentation_link.py
+++ b/pydis_site/apps/api/models/bot/documentation_link.py
@@ -1,7 +1,20 @@
+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."
+)
+
+
+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."""
@@ -9,13 +22,15 @@ class DocumentationLink(ModelReprMixin, models.Model):
package = models.CharField(
primary_key=True,
max_length=50,
+ 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."