Skip to content

Commit

Permalink
Add models and serializers to translations. Also add on Django Admin
Browse files Browse the repository at this point in the history
  • Loading branch information
ericosta-dev committed Sep 13, 2024
1 parent 006db04 commit 89dfa98
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 33 deletions.
73 changes: 68 additions & 5 deletions connect/api/v2/template_projects/serializers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
from rest_framework.serializers import ModelSerializer
from rest_framework.serializers import ModelSerializer, SerializerMethodField
from connect.template_projects.models import TemplateType, TemplateFeature, TemplateSuggestion
from rest_framework import serializers


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()
features = SerializerMethodField()

class Meta:
model = TemplateType
Expand All @@ -22,16 +46,55 @@ class Meta:
'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_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']

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 TemplateSuggestionSerializer(ModelSerializer):

Expand Down
2 changes: 1 addition & 1 deletion connect/api/v2/template_projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TemplateTypeViewSet(ModelViewSet):
permission_classes = [IsAdminOrReadOnly]

def get_queryset(self):
queryset = self.queryset.filter(language=self.request.user.language)
queryset = self.queryset
id = self.request.query_params.get('id', None)
name = self.request.query_params.get('name', None)
category = self.request.query_params.get('category', None)
Expand Down
32 changes: 29 additions & 3 deletions connect/template_projects/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
from django.contrib import admin
from connect.template_projects.models import TemplateType, TemplateFeature, TemplateSuggestion
from connect.template_projects.models import (
TemplateType,
TemplateFeature,
TemplateSuggestion,
TemplateTypeTranslation,
TemplateFeatureTranslation,
)

# Register your models here.
admin.site.register(TemplateType)
admin.site.register(TemplateFeature)
admin.site.register(TemplateSuggestion)


class TemplateTypeTranslationStackedInline(admin.StackedInline):
model = TemplateTypeTranslation
extra = 1


class TemplateFeatureTranslationStackedInline(admin.StackedInline):
model = TemplateFeatureTranslation
extra = 1


class TemplateTypeAdmin(admin.ModelAdmin):
inlines = [TemplateTypeTranslationStackedInline]


class TemplateFeatureAdmin(admin.ModelAdmin):
inlines = [TemplateFeatureTranslationStackedInline]


admin.site.register(TemplateType, TemplateTypeAdmin)
admin.site.register(TemplateFeature, TemplateFeatureAdmin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.2.23 on 2024-09-13 02:08

import connect.template_projects.storage
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('template_projects', '0007_auto_20230926_1231'),
]

operations = [
migrations.CreateModel(
name='TemplateTypeTranslation',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('language', models.CharField(choices=[('en-us', 'English'), ('pt-br', 'Portuguese'), ('es', 'Spanish')], default='en-us', max_length=10, verbose_name='language')),
('description', models.TextField(blank=True, null=True)),
('name', models.CharField(blank=True, max_length=255, null=True)),
('setup', models.JSONField(blank=True, null=True)),
('photo', models.ImageField(blank=True, null=True, storage=connect.template_projects.storage.TemplateTypeImageStorage(), upload_to='')),
('photo_description', models.TextField(blank=True, null=True)),
('template_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='translations', to='template_projects.templatetype')),
],
),
migrations.CreateModel(
name='TemplateFeatureTranslation',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('language', models.CharField(choices=[('en-us', 'English'), ('pt-br', 'Portuguese'), ('es', 'Spanish')], default='en-us', max_length=10, verbose_name='language')),
('description', models.TextField()),
('name', models.CharField(max_length=255)),
('template_feature', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='translations', to='template_projects.templatefeature')),
],
),
]
18 changes: 0 additions & 18 deletions connect/template_projects/migrations/0008_templatetype_language.py

This file was deleted.

39 changes: 33 additions & 6 deletions connect/template_projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@


class TemplateType(models.Model):
language = models.CharField(
"language",
max_length=10,
choices=settings.LANGUAGES,
default='pt-br',
)
level_field = [("low", 1), ("medium", 2), ("high", 3)]
uuid = models.UUIDField(
"UUID", default=uuid4.uuid4
Expand Down Expand Up @@ -45,6 +39,24 @@ def get_template_code(self, template_name):
return mapping_template.get(template_name, "blank")


class TemplateTypeTranslation(models.Model):
class Meta:
unique_together = ["language", "template_type"]

template_type = models.ForeignKey(TemplateType, on_delete=models.CASCADE, related_name='translations')
language = models.CharField(
"language",
max_length=10,
choices=settings.LANGUAGES,
default=settings.DEFAULT_LANGUAGE
)
description = models.TextField(blank=True, null=True)
name = models.CharField(max_length=255, null=True, blank=True)
setup = models.JSONField(blank=True, null=True)
photo = models.ImageField(storage=TemplateTypeImageStorage(), blank=True, null=True)
photo_description = models.TextField(blank=True, null=True)


class TemplateFeature(models.Model):
features_types = [
("Flows", "Flows"),
Expand All @@ -62,6 +74,21 @@ def __str__(self):
return self.name


class TemplateFeatureTranslation(models.Model):
class Meta:
unique_together = ["language", "template_feature"]

template_feature = models.ForeignKey(TemplateFeature, on_delete=models.CASCADE, related_name='translations')
language = models.CharField(
"language",
max_length=10,
choices=settings.LANGUAGES,
default=settings.DEFAULT_LANGUAGE
)
description = models.TextField()
name = models.CharField(max_length=255)


class TemplateSuggestion(models.Model):

suggestion = models.TextField()
Expand Down

0 comments on commit 89dfa98

Please sign in to comment.