Skip to content

Commit

Permalink
fix: Fix not being able to delete blocks due to a block without pk no…
Browse files Browse the repository at this point in the history
…t being able to be related to translated content(s)
  • Loading branch information
drikusroor committed Jan 6, 2025
1 parent 8a7dcf2 commit 4a69b83
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions backend/experiment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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()],
},
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 4a69b83

Please sign in to comment.