Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: Add translated_content model and update Experiment model #1208

Merged
merged 14 commits into from
Aug 5, 2024

Conversation

drikusroor
Copy link
Contributor

@drikusroor drikusroor commented Aug 2, 2024

This PR adds the ExperimentTranslatedContent model to the application, along with its related functionality. It has the following properties:

Model

  • experiment - FK to the Experiment model
  • language - The language code for the content's language
  • index - Sorting property, the content with the lowest index will be considered the fallback language/content (for now)
  • title - This was originally the Experiment's title
  • description - This was originally the Experiment's description
  • consent - This was originally the Experiment's consent
  • about_content - This was originally the Experiment's about_content

The PR also adds some helper methods to the Experiment model, get_fallback_content and get_translated_content. These are used to get the appropriate content for the given context.

def get_fallback_content(self):
"""Get primary content for the experiment"""
return self.translated_content.order_by("index").first()
def get_translated_content(self, language: str, fallback: bool = True):
"""Get content for a specific language"""
content = self.translated_content.filter(language=language).first()
if not content and fallback:
fallback_content = self.get_fallback_content()
if not fallback_content:
raise ValueError("No primary content found for experiment")
return fallback_content
if not content:
raise ValueError(f"No content found for language {language}")
return content

Serialisation & active language handling

The languages of the configured ExperimentTranslatedContents for an Experiment determine the possible languages in which an experiment can be used. These languages will also determine the languages used at the SocialMediaConfig, ThemeConfig, and Block level. Right now, what happens is that the user has a preferred language that is used to get the appropriate content and fall back to the experiment's fallback language if it is not available. Django has the get_language helper function to determine the user's preferred language. This language is used to find the appropriate ExperimentTranslatedContent. If it cannot find it, it will take the experiment's translated content with the lowest index number. If that doesn't exist either, it will throw an error as an Experiment needs a content in order to work. (We might want to require at least one translated content per experiment during creation and updating.)

The language of the ExperimentTranslatedContent (preferred or fallback) will be used as the active language for the whole experiment, its related entities, and the non-experiment related content using Django activate(language) function (which was used earlier at the Block level depending on the configured language there.

language_code = get_language()[0:2]
translated_content = experiment.get_translated_content(language_code)
if not translated_content:
raise ValueError("No translated content found for this experiment")
experiment_language = translated_content.language
activate(experiment_language)

Consent

The retrieval of the consent form works slightly different. If a consent form cannot be found in the preferred language, but does exist at the fallback level, the consent form from the fallback language will be used. This gives the user flexibility in configuring consent form: Configure consent forms in all languages, configure consent forms in only one or a limited selection of the experiment languages, or configure no consent forms at all.

Things to do after merging this PR

The migrations copy the Experiment's content to a newly created ExperimentLanguageContent, with the language set to English. This means that we have to go over all experiments after the migration and set the language to Dutch or Chinese if that's the Experiment's original language.

Related to #1204

The commit removes the `name`, `description`, `consent`, and `about_content` fields from the `Experiment` model and updates the admin forms accordingly.
This commit adds a `fallback` parameter to the `get_translated_content` method in the `Experiment` model. When set to `True`, if no content is found for the specified language, it will return the primary content instead. If `fallback` is set to `False` and no content is found, a `ValueError` will be raised.

Refactor the `get_content` method to `get_translated_content` to better reflect its purpose.
@drikusroor drikusroor self-assigned this Aug 2, 2024
This commit modifies the migration file `0050_migrate_experiment_content_to_translatedmakemigrations.py` to delete all experiment translated content using the `ExperimentTranslatedContent.objects.all().delete()` method. This change is necessary to clean up the database and ensure that no translated content exists anymore after running the reverse migration.
@drikusroor drikusroor merged commit 577d107 into develop Aug 5, 2024
13 checks passed
@drikusroor drikusroor deleted the feat/translated-content branch August 5, 2024 07:48
Copy link

sentry-io bot commented Aug 9, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ ValueError: Rules do not exist (anymore): VISUAL_MATCHING_PAIRS for block VMP (vmp) /server/experiment/block/{slug}/ View Issue

Did you find this useful? React with a 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants