From 1df3dd910b71aa910f18f968441e3db3755565b4 Mon Sep 17 00:00:00 2001 From: Diederik Boeren Date: Wed, 27 Dec 2023 06:47:34 +0000 Subject: [PATCH] Added default translation update on project update --- backend/portfolio42_api/models.py | 66 ++++++++++++++++++------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/backend/portfolio42_api/models.py b/backend/portfolio42_api/models.py index 22ac4d2..0d2250e 100644 --- a/backend/portfolio42_api/models.py +++ b/backend/portfolio42_api/models.py @@ -8,6 +8,35 @@ import logging +# Translations + +class TranslationLanguage(models.Model): + # Validator: `default` or full capitals, 2 sets of letters seperated by a `-` e.g EN-US or EN-AUS + name_short = models.CharField(unique=True, max_length=8, validators=[RegexValidator("([A-Z]+-[A-Z]+)|(default)")]) + name_full = models.CharField(unique=True, max_length=64) # For example: English (United States) + + def __str__(self): + return f"(Translation Language: {self.name_short})" + +class ProjectTranslation(models.Model): + id_project = models.ForeignKey('Project', on_delete=models.CASCADE) + id_language = models.ForeignKey('TranslationLanguage', on_delete=models.CASCADE) + + # The description of the project + description = models.TextField(max_length=2000) + # Description for the bonus part of the project + description_bonus = models.TextField(max_length=2000) + + # If the bonus description appends (true) or replaces (false) + bonus_append = models.BooleanField(default=True) + + updated_at = models.DateTimeField(auto_created=True, auto_now=True) + + def __str__(self): + return f"(Project Translation: {self.id_project.name}:{self.id_language.name_short})" + +# Intra models + class IntraBaseModel(models.Model): intra_id = models.IntegerField(unique=True, db_index=True) updated_at = models.DateTimeField(auto_created=True, auto_now=True) @@ -114,6 +143,16 @@ def update(project): p.solo = False # todo: REMOVE THIS p.save() + # Update default translation + # Would love to remove the fetching of the default translation every time a project updates but do not see how + def_translation, _ = TranslationLanguage.objects.get_or_create(name_short='default') + p_trans, _ = ProjectTranslation.objects.get_or_create(id_language=def_translation, id_project=p) + try: + p_trans.description = project['description'] + except: + p_trans.description = project['slug'] + p_trans.save() + log_update(p, created) return p @@ -282,30 +321,3 @@ def update(cursusUser, cuSkill): return cus - -# Translations - -class TranslationLanguage(models.Model): - # Validator: `default` or full capitals, 2 sets of letters seperated by a `-` e.g EN-US or EN-AUS - name_short = models.CharField(unique=True, max_length=8, validators=[RegexValidator("([A-Z]+-[A-Z]+)|(default)")]) - name_full = models.CharField(unique=True, max_length=64) # For example: English (United States) - - def __str__(self): - return f"(Translation Language: {self.name_short})" - -class ProjectTranslation(models.Model): - id_project = models.ForeignKey('Project', on_delete=models.CASCADE) - id_language = models.ForeignKey('TranslationLanguage', on_delete=models.CASCADE) - - # The description of the project - description = models.TextField(max_length=2000) - # Description for the bonus part of the project - description_bonus = models.TextField(max_length=2000) - - # If the bonus description appends (true) or replaces (false) - bonus_append = models.BooleanField(default=True) - - updated_at = models.DateTimeField(auto_created=True, auto_now=True) - - def __str__(self): - return f"(Project Translation: {self.id_project.name}:{self.id_language.name_short})"