1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-10-16 07:07:26 +00:00

🔧 Limit the allowed slug values

This commit is contained in:
Steffo 2021-10-18 20:03:23 +02:00
parent f2be69a107
commit a5d492520f
3 changed files with 48 additions and 0 deletions

View file

@ -0,0 +1,22 @@
# Generated by Django 3.2.7 on 2021-10-18 18:02
from django.db import migrations, models
import sophon.notebooks.validators
class Migration(migrations.Migration):
dependencies = [
('notebooks', '0008_alter_notebook_container_image'),
]
operations = [
migrations.AlterField(
model_name='notebook',
name='slug',
field=models.SlugField(
help_text='Unique alphanumeric string which identifies the project. Changing this once the container has been created <strong>will break Docker</strong>!',
max_length=64, primary_key=True, serialize=False,
validators=[sophon.notebooks.validators.DisallowedValuesValidator(['api', 'proxy', 'backend', 'frontend', 'src'])], verbose_name='Slug'),
),
]

View file

@ -18,6 +18,7 @@ from sophon.notebooks.apache import get_ephemeral_port, base_domain, http_protoc
from sophon.notebooks.docker import client as docker_client
from sophon.notebooks.docker import sleep_until_container_has_started
from sophon.notebooks.jupyter import generate_secure_token
from sophon.notebooks.validators import DisallowedValuesValidator
from sophon.projects.models import ResearchProject
module_name = __name__
@ -42,6 +43,15 @@ class Notebook(SophonGroupModel):
help_text="Unique alphanumeric string which identifies the project. Changing this once the container has been created <strong>will break Docker</strong>!",
max_length=64,
primary_key=True,
validators=[
DisallowedValuesValidator([
"api", # reserved for docker deployment
"proxy", # reserved for future use
"backend", # reserved for future use
"frontend", # reserved for future use
"src", # reserved for future use
])
]
)
project = models.ForeignKey(

View file

@ -0,0 +1,16 @@
from django.core.validators import ValidationError
from django.utils.deconstruct import deconstructible
from django.utils.translation import gettext_lazy as _
@deconstructible
class DisallowedValuesValidator:
def __init__(self, values):
self.values = values
def __call__(self, value):
if value in self.values:
raise ValidationError(
_("%(value)s is a disallowed value."),
params={"value": value},
)