Skip to content

Commit

Permalink
Merge pull request #1366 from Amsterdam-Music-Lab/bugfix-er/make-expe…
Browse files Browse the repository at this point in the history
…riment-slug-unique

Bugfix er/make experiment slug unique
  • Loading branch information
Evert-R authored Nov 21, 2024
2 parents 2c9bc6e + 586a953 commit 2e3bf5d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
19 changes: 19 additions & 0 deletions backend/experiment/migrations/0060_experiment_slug_temp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.16 on 2024-11-15 09:00

from django.db import migrations, models
import experiment.validators


class Migration(migrations.Migration):

dependencies = [
('experiment', '0059_add_social_media_config'),
]

operations = [
migrations.AddField(
model_name='experiment',
name='slug_temp',
field=models.SlugField(null=True, max_length=64, unique=True, validators=[experiment.validators.block_slug_validator]),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.16 on 2024-11-15 09:01

from django.db import migrations


class Migration(migrations.Migration):

def forwards_func(apps, schema_editor):
Experiment = apps.get_model('experiment', 'Experiment')
for experiment in Experiment.objects.all():
experiment.slug_temp = experiment.slug
experiment.save()

def reverse_func(apps, schema_editor):
Experiment = apps.get_model('experiment', 'Experiment')
for experiment in Experiment.objects.all():
experiment.slug = experiment.slug_temp
experiment.save()

dependencies = [
('experiment', '0060_experiment_slug_temp'),
]

operations = [
migrations.RunPython(forwards_func, reverse_func),
]
27 changes: 27 additions & 0 deletions backend/experiment/migrations/0062_rename_slug_temp_to_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.16 on 2024-11-15 09:09

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('experiment', '0061_migrate_slugs_to_unique_temp_field'),
]

operations = [
migrations.RenameField(
model_name='Experiment',
old_name='slug',
new_name='old_slug'
),
migrations.RenameField(
model_name='Experiment',
old_name='slug_temp',
new_name='slug'
),
migrations.RemoveField(
model_name='Experiment',
name='old_slug',
),
]
8 changes: 6 additions & 2 deletions backend/experiment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from session.models import Session
from typing import Optional, Union

from .validators import markdown_html_validator, block_slug_validator
from .validators import markdown_html_validator, block_slug_validator, experiment_slug_validator

language_choices = [(key, ISO_LANGUAGES[key]) for key in ISO_LANGUAGES.keys()]
language_choices[0] = ("", "Unset")
Expand All @@ -31,7 +31,11 @@ class Experiment(models.Model):
phases (Queryset[Phase]): Queryset of Phase instances
"""

slug = models.SlugField(max_length=64, default="")
slug = models.SlugField(db_index=True,
max_length=64,
unique=True,
null=True,
validators=[experiment_slug_validator])
translated_content = models.QuerySet["ExperimentTranslatedContent"]
theme_config = models.ForeignKey("theme.ThemeConfig", blank=True, null=True, on_delete=models.SET_NULL)
active = models.BooleanField(default=True)
Expand Down

0 comments on commit 2e3bf5d

Please sign in to comment.