-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1331 from Amsterdam-Music-Lab/bugfix/final-social
Provide fallback for required variables for social media content
- Loading branch information
Showing
13 changed files
with
233 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from django.test import TestCase | ||
from django.utils.translation import activate | ||
|
||
from experiment.models import ( | ||
Block, | ||
BlockTranslatedContent, | ||
Experiment, | ||
ExperimentTranslatedContent, | ||
Phase, | ||
SocialMediaConfig, | ||
) | ||
from participant.models import Participant | ||
from result.models import Result | ||
from session.models import Session | ||
|
||
from experiment.actions import Final | ||
|
||
|
||
class FinalTest(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
cls.experiment = Experiment.objects.create( | ||
slug="final_countdown", | ||
) | ||
ExperimentTranslatedContent.objects.create( | ||
experiment=cls.experiment, name="Final Countdown", language="en", index=1 | ||
) | ||
ExperimentTranslatedContent.objects.create( | ||
experiment=cls.experiment, | ||
name="Laatste Telaf", | ||
social_media_message="Ik heb {points} punten gescoord op {experiment_name}. Kan jij het beter?", | ||
language="nl", | ||
index=0, | ||
) | ||
phase = Phase.objects.create(experiment=cls.experiment) | ||
block = Block.objects.create(phase=phase, rules="HOOKED", rounds=6) | ||
BlockTranslatedContent.objects.create( | ||
block=block, name="Test block", language="en" | ||
) | ||
participant = Participant.objects.create() | ||
cls.session = Session.objects.create(block=block, participant=participant) | ||
Result.objects.create(session=cls.session, score=28) | ||
Result.objects.create(session=cls.session, score=14) | ||
|
||
def test_final_action_without_social(self): | ||
final = Final(self.session) | ||
serialized = final.action() | ||
self.assertEqual(serialized.get("title"), "Final score") | ||
self.assertIsNone(serialized.get("social")) | ||
|
||
def test_final_action_with_social(self): | ||
SocialMediaConfig.objects.create( | ||
experiment=self.experiment, | ||
channels=["Facebook"], | ||
tags=["amazing"], | ||
url="example.com", | ||
) | ||
final = Final(self.session) | ||
serialized = final.action() | ||
social_info = serialized.get("social") | ||
self.assertIsNotNone(social_info) | ||
self.assertEqual(social_info.get("channels"), ["Facebook"]) | ||
self.assertEqual(social_info.get("url"), "example.com") | ||
self.assertEqual(social_info.get("tags"), ["amazing"]) | ||
self.assertEqual( | ||
social_info.get("content"), | ||
"Ik heb 42.0 punten gescoord op Laatste Telaf. Kan jij het beter?", | ||
) | ||
activate("en") | ||
final = Final(self.session) | ||
serialized = final.action() | ||
social_info = serialized.get("social") | ||
self.assertEqual( | ||
social_info.get("content"), "I scored 42.0 points in Final Countdown!" | ||
) |
56 changes: 56 additions & 0 deletions
56
backend/experiment/migrations/0059_add_social_media_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Generated by Django 4.2.16 on 2024-10-28 16:06 | ||
|
||
from django.db import migrations | ||
from django.conf import settings | ||
from django.utils import translation | ||
|
||
|
||
def add_social_media_config(apps, schema_editor): | ||
"""add information from rules files to database""" | ||
Block = apps.get_model("experiment", "Block") | ||
ExperimentTranslatedContent = apps.get_model( | ||
"experiment", "ExperimentTranslatedContent" | ||
) | ||
SocialMediaConfig = apps.get_model("experiment", "SocialMediaConfig") | ||
blocks = Block.objects.all() | ||
for block in blocks: | ||
if block.rules in [ | ||
"HOOKED", | ||
"EUROVISION_2020", | ||
"KUIPER_2020", | ||
"HUANG_2022", | ||
"MUSICAL_PREFERENCES", | ||
"MATCHING_PAIRS", | ||
]: | ||
channels = ["facebook", "twitter"] | ||
if block.rules == "MATCHING_PAIRS": | ||
channels.append("clipboard") | ||
elif block.rules == "MUSICAL_PREFERENCES": | ||
channels = ["weibo", "share"] | ||
if not SocialMediaConfig.objects.filter( | ||
experiment=block.phase.experiment | ||
).exists(): | ||
SocialMediaConfig.objects.create( | ||
experiment=block.phase.experiment, | ||
tags=["amsterdammusiclab", "citizenscience"], | ||
url=f"{settings.RELOAD_PARTICIPANT_TARGET}/{block.slug}", | ||
channels=channels, | ||
) | ||
if not ExperimentTranslatedContent.objects.filter( | ||
experiment=block.phase.experiment | ||
).exists(): | ||
ExperimentTranslatedContent.objects.create( | ||
experiment=block.phase.experiment, | ||
language="en", | ||
name=block.phase.experiment.slug, | ||
) | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("experiment", "0058_remove_socialmediaconfig_content_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(add_social_media_config, migrations.RunPython.noop) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.