Example for tags creation
This commit is contained in:
parent
dd07769b9b
commit
ee8d133248
|
|
@ -3,6 +3,7 @@ from django.utils.text import slugify
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from tag.models import Tag
|
from tag.models import Tag
|
||||||
|
from translation.models import SiteInterfaceDictionary
|
||||||
from transfer.mixins import TransferSerializerMixin
|
from transfer.mixins import TransferSerializerMixin
|
||||||
from transfer.models import Cepages
|
from transfer.models import Cepages
|
||||||
|
|
||||||
|
|
@ -36,8 +37,11 @@ class AssemblageTagSerializer(TransferSerializerMixin):
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
qs = self.Meta.model.objects.filter(**validated_data)
|
qs = self.Meta.model.objects.filter(**validated_data)
|
||||||
category = validated_data.get('category')
|
category = validated_data.get('category')
|
||||||
|
translations = validated_data.pop('label')
|
||||||
if not qs.exists() and category:
|
if not qs.exists() and category:
|
||||||
return super().create(validated_data)
|
instance = super().create(validated_data)
|
||||||
|
SiteInterfaceDictionary.objects.update_or_create_for_tag(instance, translations)
|
||||||
|
return instance
|
||||||
|
|
||||||
def get_tag_value(self, cepage, percent):
|
def get_tag_value(self, cepage, percent):
|
||||||
if cepage and percent:
|
if cepage and percent:
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@
|
||||||
from django.contrib.postgres.fields import JSONField
|
from django.contrib.postgres.fields import JSONField
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from django.apps import apps
|
||||||
from utils.models import ProjectBaseMixin, LocaleManagerMixin
|
from utils.models import ProjectBaseMixin, LocaleManagerMixin
|
||||||
|
|
||||||
|
|
||||||
class LanguageQuerySet(models.QuerySet):
|
class LanguageQuerySet(models.QuerySet):
|
||||||
"""QuerySet for model Language"""
|
"""QuerySet for model Language"""
|
||||||
|
|
||||||
|
|
@ -50,6 +50,44 @@ class Language(models.Model):
|
||||||
class SiteInterfaceDictionaryManager(LocaleManagerMixin):
|
class SiteInterfaceDictionaryManager(LocaleManagerMixin):
|
||||||
"""Extended manager for SiteInterfaceDictionary model."""
|
"""Extended manager for SiteInterfaceDictionary model."""
|
||||||
|
|
||||||
|
def update_or_create_for_tag(self, tag, translations: dict):
|
||||||
|
Tag = apps.get_model('tag', 'Tag')
|
||||||
|
"""Creates or updates translation for EXISTING in DB Tag"""
|
||||||
|
if not tag.pk or not isinstance(tag, Tag):
|
||||||
|
raise NotImplementedError
|
||||||
|
if tag.translation:
|
||||||
|
tag.translation.text = translations
|
||||||
|
tag.translation.page = 'tag'
|
||||||
|
tag.translation.keywords = f'tag-{tag.pk}'
|
||||||
|
else:
|
||||||
|
trans = SiteInterfaceDictionary({
|
||||||
|
'text': translations,
|
||||||
|
'page': 'tag',
|
||||||
|
'keywords': f'tag-{tag.pk}'
|
||||||
|
})
|
||||||
|
trans.save()
|
||||||
|
tag.translation = trans
|
||||||
|
tag.save()
|
||||||
|
|
||||||
|
def update_or_create_for_tag_category(self, tag_category, translations: dict):
|
||||||
|
"""Creates or updates translation for EXISTING in DB TagCategory"""
|
||||||
|
TagCategory = apps.get_model('tag', 'TagCategory')
|
||||||
|
if not tag_category.pk or not isinstance(tag_category, TagCategory):
|
||||||
|
raise NotImplementedError
|
||||||
|
if tag_category.translation:
|
||||||
|
tag_category.translation.text = translations
|
||||||
|
tag_category.translation.page = 'tag'
|
||||||
|
tag_category.translation.keywords = f'tag_category-{tag_category.pk}'
|
||||||
|
else:
|
||||||
|
trans = SiteInterfaceDictionary({
|
||||||
|
'text': translations,
|
||||||
|
'page': 'tag',
|
||||||
|
'keywords': f'tag_category-{tag_category.pk}'
|
||||||
|
})
|
||||||
|
trans.save()
|
||||||
|
tag_category.translation = trans
|
||||||
|
tag_category.save()
|
||||||
|
|
||||||
|
|
||||||
class SiteInterfaceDictionary(ProjectBaseMixin):
|
class SiteInterfaceDictionary(ProjectBaseMixin):
|
||||||
"""Site interface dictionary model."""
|
"""Site interface dictionary model."""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user