Skip to content

Commit

Permalink
Fixed serializer to use translations
Browse files Browse the repository at this point in the history
  • Loading branch information
PukieDiederik committed Dec 27, 2023
1 parent 1df3dd9 commit 9356e35
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions backend/portfolio42_api/serializers/ProjectSerializer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers
from portfolio42_api.models import Project, User, Cursus
from portfolio42_api.models import Project, User, Cursus, ProjectTranslation
import re

class CursusSerializer(serializers.ModelSerializer):
class Meta():
Expand All @@ -25,9 +26,28 @@ class ProjectSerializer(serializers.ModelSerializer):
class Meta():
model = Project
fields = ['name',
'description',
'exam',
'solo',
'intra_id',
'cursus',
'users']
'users']

def to_representation(self, instance):
instance_data = super().to_representation(instance)
lang_raw = self.context['request'].GET.get('lang','default')
langs = []
# lang_raw validation
if (re.fullmatch('^\[(?:[A-Z]+-[A-Z]+|default)(?:,(?:[A-Z]+-[A-Z]+|default))*\]$', lang_raw)):
langs = lang_raw[1:-1].split(',')
langs.append('default')
for lang in langs:
try:
pt = ProjectTranslation.objects.get(id_language__name_short=lang,id_project=instance)
print(f"found correct ${lang}")
instance_data['description'] = pt.description
instance_data['description_bonus'] = pt.description_bonus
instance_data['append_bonus'] = pt.bonus_append
break
except ProjectTranslation.DoesNotExist:
pass
return instance_data

0 comments on commit 9356e35

Please sign in to comment.