Example for tags creation

This commit is contained in:
Kuroshini 2019-12-20 20:11:58 +03:00
parent dd07769b9b
commit ee8d133248
2 changed files with 44 additions and 2 deletions

View File

@ -3,6 +3,7 @@ from django.utils.text import slugify
from rest_framework import serializers
from tag.models import Tag
from translation.models import SiteInterfaceDictionary
from transfer.mixins import TransferSerializerMixin
from transfer.models import Cepages
@ -36,8 +37,11 @@ class AssemblageTagSerializer(TransferSerializerMixin):
def create(self, validated_data):
qs = self.Meta.model.objects.filter(**validated_data)
category = validated_data.get('category')
translations = validated_data.pop('label')
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):
if cepage and percent:

View File

@ -2,9 +2,9 @@
from django.contrib.postgres.fields import JSONField
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.apps import apps
from utils.models import ProjectBaseMixin, LocaleManagerMixin
class LanguageQuerySet(models.QuerySet):
"""QuerySet for model Language"""
@ -50,6 +50,44 @@ class Language(models.Model):
class SiteInterfaceDictionaryManager(LocaleManagerMixin):
"""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):
"""Site interface dictionary model."""