From 4a69b83453840a8d0530c89ea2b3189bb929f9be Mon Sep 17 00:00:00 2001 From: Drikus Roor Date: Fri, 27 Dec 2024 14:09:16 +0100 Subject: [PATCH] fix: Fix not being able to delete blocks due to a block without pk not being able to be related to translated content(s) --- backend/experiment/models.py | 47 +++++++++++++----------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/backend/experiment/models.py b/backend/experiment/models.py index 2acb7aeb2..d38c4dffd 100644 --- a/backend/experiment/models.py +++ b/backend/experiment/models.py @@ -6,12 +6,12 @@ from django.utils.translation import gettext_lazy as _, get_language from django.contrib.postgres.fields import ArrayField from django.db.models.query import QuerySet -from typing import List, Dict, Tuple, Any +from typing import List, Any from experiment.standards.iso_languages import ISO_LANGUAGES from theme.models import ThemeConfig from image.models import Image from session.models import Session -from typing import Optional, Union +from typing import Optional from .validators import markdown_html_validator, block_slug_validator, experiment_slug_validator @@ -31,11 +31,9 @@ class Experiment(models.Model): phases (Queryset[Phase]): Queryset of Phase instances """ - slug = models.SlugField(db_index=True, - max_length=64, - unique=True, - null=True, - validators=[experiment_slug_validator]) + 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) @@ -288,13 +286,8 @@ def _export_admin(self) -> dict: "block": { "id": self.id, "name": self.name, - "sessions": [ - session._export_admin() for session in self.session_set.all() - ], - "participants": [ - participant._export_admin() - for participant in self.current_participants() - ], + "sessions": [session._export_admin() for session in self.session_set.all()], + "participants": [participant._export_admin() for participant in self.current_participants()], }, } @@ -388,18 +381,12 @@ def export_table( # convert result json data to csv columns if selected if "convert_result_json" in export_options: if "decision_time" in export_options: - result_data[result_prefix + "decision_time"] = ( - result.json_data.get("decision_time", "") - ) + result_data[result_prefix + "decision_time"] = result.json_data.get("decision_time", "") if "result_config" in export_options: - result_data[result_prefix + "result_config"] = ( - result.json_data.get("config", "") - ) + result_data[result_prefix + "result_config"] = result.json_data.get("config", "") else: if "result_config" in export_options: - result_data[result_prefix + "result_data"] = ( - result.json_data - ) + result_data[result_prefix + "result_data"] = result.json_data this_row.update(result_data) fieldnames.update(result_data.keys()) result_counter += 1 @@ -456,13 +443,17 @@ def add_default_question_series(self): question_series=qs, question=Question.objects.get(pk=question), index=i + 1 ) - def get_fallback_content(self) -> "BlockTranslatedContent": + def get_fallback_content(self) -> "BlockTranslatedContent | None": """Get fallback content for the block Returns: Fallback content """ + # If the block is not saved yet or is being deleted, there is no fallback content + if not self.pk: + return None + if not self.phase or self.phase.experiment: return self.translated_contents.first() @@ -623,9 +614,7 @@ def get_content(self, score: float) -> str: experiment_name = translated_content.name if social_message: - has_placeholders = ( - "{points}" in social_message and "{experiment_name}" in social_message - ) + has_placeholders = "{points}" in social_message and "{experiment_name}" in social_message if not has_placeholders: return social_message @@ -636,9 +625,7 @@ def get_content(self, score: float) -> str: return social_message.format(points=score, experiment_name=experiment_name) if score is None or experiment_name is None: - raise ValueError( - "score and name are required when no social media message is provided" - ) + raise ValueError("score and name are required when no social media message is provided") return _("I scored %(score)d points in %(experiment_name)s") % { "score": score,