-
Notifications
You must be signed in to change notification settings - Fork 7
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 #895 from weni-ai/feature/templatetype-language
Feature: Translation on TemplateType feature
- Loading branch information
Showing
13 changed files
with
398 additions
and
112 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 |
---|---|---|
@@ -1,40 +1,121 @@ | ||
from rest_framework.serializers import ModelSerializer | ||
from connect.template_projects.models import TemplateType, TemplateFeature, TemplateSuggestion | ||
from rest_framework import serializers | ||
from rest_framework.serializers import ModelSerializer, SerializerMethodField | ||
|
||
from connect.template_projects.models import ( | ||
TemplateFeature, | ||
TemplateSuggestion, | ||
TemplateType, | ||
) | ||
|
||
|
||
class TemplateFeatureSerializer(ModelSerializer): | ||
description = SerializerMethodField() | ||
name = SerializerMethodField() | ||
|
||
class Meta: | ||
model = TemplateFeature | ||
fields = "__all__" | ||
fields = [ | ||
"description", | ||
"name", | ||
"type", | ||
"feature_identifier", | ||
"template_type", | ||
] | ||
|
||
def _get_language(self): | ||
request = self.context.get("request") | ||
return request.user.language | ||
|
||
def get_description(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).description | ||
|
||
def get_name(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).name | ||
|
||
|
||
class TemplateTypeSerializer(ModelSerializer): | ||
|
||
features = serializers.SerializerMethodField() | ||
name = SerializerMethodField() | ||
description = SerializerMethodField() | ||
setup = SerializerMethodField() | ||
photo = SerializerMethodField() | ||
photo_description = SerializerMethodField() | ||
category = SerializerMethodField() | ||
features = SerializerMethodField() | ||
|
||
class Meta: | ||
model = TemplateType | ||
fields = [ | ||
'uuid', 'category', 'description', 'name', | ||
'level', 'setup', 'photo', 'features', | ||
'photo_description', 'base_project_uuid' | ||
"uuid", | ||
"category", | ||
"description", | ||
"name", | ||
"level", | ||
"setup", | ||
"photo", | ||
"features", | ||
"photo_description", | ||
"base_project_uuid", | ||
] | ||
|
||
def _get_language(self): | ||
request = self.context.get("request") | ||
return request.user.language | ||
|
||
def get_name(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).name | ||
|
||
def get_description(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).description | ||
|
||
def get_setup(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).setup | ||
|
||
def get_photo(self, obj): | ||
lang = self._get_language() | ||
translation = obj.translations.get(language=lang) | ||
return translation.photo.url if translation.photo else None | ||
|
||
def get_photo_description(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).photo_description | ||
|
||
def get_category(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).category | ||
|
||
def get_features(self, obj): | ||
return TemplateFeatureSerializer(obj.template_features.all(), many=True).data | ||
return TemplateFeatureSerializer( | ||
obj.template_features.all(), many=True, context=self.context | ||
).data | ||
|
||
|
||
class RetrieveTemplateSerializer(ModelSerializer): | ||
description = SerializerMethodField() | ||
name = SerializerMethodField() | ||
|
||
class Meta: | ||
model = TemplateType | ||
fields = ['uuid', 'description', 'name'] | ||
fields = ["uuid", "description", "name"] | ||
|
||
def _get_language(self): | ||
request = self.context.get("request") | ||
return request.user.language | ||
|
||
class TemplateSuggestionSerializer(ModelSerializer): | ||
def get_description(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).description | ||
|
||
def get_name(self, obj): | ||
lang = self._get_language() | ||
return obj.translations.get(language=lang).name | ||
|
||
|
||
class TemplateSuggestionSerializer(ModelSerializer): | ||
class Meta: | ||
model = TemplateSuggestion | ||
fields = ['suggestion', 'created_at', 'status'] | ||
fields = ["suggestion", "created_at", "status"] |
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.