Skip to content

Commit

Permalink
refactor: Incorporate the migration of #1240
Browse files Browse the repository at this point in the history
  • Loading branch information
drikusroor committed Sep 4, 2024
1 parent bbd686c commit 811821b
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions backend/experiment/migrations/0054_migrate_block_content.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by Django 4.2.14 on 2024-08-07 14:30

from django.db import migrations
from django.core.files.base import File
from pathlib import Path


def migrate_block_content(apps, schema_editor):
Expand All @@ -23,23 +25,28 @@ def migrate_block_content(apps, schema_editor):
if block.phase:
continue

# if block is not associated with a phase and experiment,
# create a new experiment and phase
experiment = Experiment.objects.create(
slug=block.slug,
)
ExperimentTranslatedContent.objects.create(
# Create a new experiment and phase for orphan blocks
experiment = Experiment.objects.create(slug=block.slug)
content = ExperimentTranslatedContent.objects.create(
experiment=experiment,
index=0,
language=language,
name=block.name,
description=block.description,
consent=block.consent if hasattr(block, "consent") and block.consent else "",
)
phase = Phase.objects.create(
experiment=experiment,
index=0,
)

# Attempt to add consent file
rules = block.get_rules()
try:
consent_path = Path("experiment", "templates", rules.default_consent_file)
with consent_path.open(mode="rb") as f:
content.consent = File(f, name=consent_path.name)
content.save()
except Exception:
# If there's an error, we'll just skip adding the consent file
pass

phase = Phase.objects.create(experiment=experiment, index=0, name=f"{block.name}_phase")
block.phase = phase
block.save()

Expand All @@ -49,6 +56,7 @@ def reverse_migrate_block_content(apps, schema_editor):
BlockTranslatedContent = apps.get_model("experiment", "BlockTranslatedContent")
Experiment = apps.get_model("experiment", "Experiment")
ExperimentTranslatedContent = apps.get_model("experiment", "ExperimentTranslatedContent")
Phase = apps.get_model("experiment", "Phase")

for block in Block.objects.all():
block_fallback_content = block.translated_contents.first()
Expand All @@ -74,6 +82,16 @@ def reverse_migrate_block_content(apps, schema_editor):
block.description = block_fallback_content.description if block_fallback_content.description else ""
if experiment_fallback_content and experiment_fallback_content.consent:
block.consent = experiment_fallback_content.consent

# Remove the created phase and experiment if they match the criteria
if block.phase and block.phase.name == f"{block.name}_phase":
phase = Phase.objects.get(pk=block.phase.id)
experiment = Experiment.objects.get(pk=phase.experiment.id)
block.phase = None
block.save()
phase.delete()
experiment.delete()

block.save()

BlockTranslatedContent.objects.all().delete()
Expand Down

0 comments on commit 811821b

Please sign in to comment.