Skip to content

Commit

Permalink
Merge pull request python-discord#373 from Numerlor/doc-validator
Browse files Browse the repository at this point in the history
Add a validator for documentation package names and base urls.
  • Loading branch information
MarkKoz authored Apr 19, 2021
2 parents 93be3f4 + fc4a9e2 commit 8cd9e3b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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_]+$')]),
),
]
17 changes: 16 additions & 1 deletion pydis_site/apps/api/models/bot/documentation_link.py
Original file line number Diff line number Diff line change
@@ -1,21 +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."
)


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,),
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."
Expand Down
15 changes: 13 additions & 2 deletions pydis_site/apps/api/tests/test_documentation_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DetailLookupDocumentationLinkAPITests(APISubdomainTestCase):
def setUpTestData(cls):
cls.doc_link = DocumentationLink.objects.create(
package='testpackage',
base_url='https://example.com',
base_url='https://example.com/',
inventory_url='https://example.com'
)

Expand Down Expand Up @@ -108,14 +108,25 @@ def test_create_invalid_url_returns_400(self):

self.assertEqual(response.status_code, 400)

def test_create_invalid_package_name_returns_400(self):
test_cases = ("InvalidPackage", "invalid package", "i\u0150valid")
for case in test_cases:
with self.subTest(package_name=case):
body = self.doc_json.copy()
body['package'] = case
url = reverse('bot:documentationlink-list', host='api')
response = self.client.post(url, data=body)

self.assertEqual(response.status_code, 400)


class DocumentationLinkCreationTests(APISubdomainTestCase):
def setUp(self):
super().setUp()

self.body = {
'package': 'example',
'base_url': 'https://example.com',
'base_url': 'https://example.com/',
'inventory_url': 'https://docs.example.com'
}

Expand Down

0 comments on commit 8cd9e3b

Please sign in to comment.