mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 14:54:22 +00:00
🔧 Limit the allowed slug values
This commit is contained in:
parent
f2be69a107
commit
a5d492520f
3 changed files with 48 additions and 0 deletions
|
@ -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'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -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 client as docker_client
|
||||||
from sophon.notebooks.docker import sleep_until_container_has_started
|
from sophon.notebooks.docker import sleep_until_container_has_started
|
||||||
from sophon.notebooks.jupyter import generate_secure_token
|
from sophon.notebooks.jupyter import generate_secure_token
|
||||||
|
from sophon.notebooks.validators import DisallowedValuesValidator
|
||||||
from sophon.projects.models import ResearchProject
|
from sophon.projects.models import ResearchProject
|
||||||
|
|
||||||
module_name = __name__
|
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>!",
|
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,
|
max_length=64,
|
||||||
primary_key=True,
|
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(
|
project = models.ForeignKey(
|
||||||
|
|
16
backend/sophon/notebooks/validators.py
Normal file
16
backend/sophon/notebooks/validators.py
Normal 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},
|
||||||
|
)
|
Loading…
Reference in a new issue