transfer tags with translations

This commit is contained in:
Kuroshini 2019-12-30 17:31:02 +03:00
parent ca6a57f012
commit ad21b6eb24
4 changed files with 27 additions and 10 deletions

View File

@ -8,6 +8,7 @@ from gallery.models import Image
from news.models import NewsType, News from news.models import NewsType, News
from rating.models import ViewCount from rating.models import ViewCount
from tag.models import TagCategory, Tag from tag.models import TagCategory, Tag
from translation.models import SiteInterfaceDictionary
from transfer.models import PageTexts, PageCounters, PageMetadata from transfer.models import PageTexts, PageCounters, PageMetadata
from transfer.serializers.news import NewsSerializer from transfer.serializers.news import NewsSerializer
@ -144,7 +145,9 @@ def add_tags():
) )
if created: if created:
text_value = ' '.join(new_tag.value.split('_')) text_value = ' '.join(new_tag.value.split('_'))
new_tag.label = {'en-GB': text_value} translation = SiteInterfaceDictionary(page={'en-GB': text_value}, keywords=f'tag.{new_tag.category}.{new_tag.value}')
translation.save()
new_tag.translation = translation
new_tag.save() new_tag.save()
news = News.objects.filter(old_id=old_id).first() news = News.objects.filter(old_id=old_id).first()

View File

@ -11,6 +11,7 @@ from transfer.mixins import TransferSerializerMixin
from django.conf import settings from django.conf import settings
from functools import reduce from functools import reduce
from gallery.models import Image from gallery.models import Image
from translation.models import SiteInterfaceDictionary
class WineColorSerializer(TransferSerializerMixin): class WineColorSerializer(TransferSerializerMixin):
@ -21,6 +22,7 @@ class WineColorSerializer(TransferSerializerMixin):
name = serializers.CharField(allow_null=True) name = serializers.CharField(allow_null=True)
order_number = serializers.IntegerField(allow_null=True) order_number = serializers.IntegerField(allow_null=True)
class Meta: class Meta:
model = tag_models.Tag model = tag_models.Tag
fields = ( fields = (
@ -31,8 +33,11 @@ class WineColorSerializer(TransferSerializerMixin):
def validate(self, attrs): def validate(self, attrs):
value = attrs.pop('name') value = attrs.pop('name')
translation = SiteInterfaceDictionary(text={settings.FALLBACK_LOCALE: value},
keywords=f'tag.{self.CATEGORY_INDEX_NAME}.{slugify(value)}')
translation.save()
attrs['old_id'] = attrs.pop('id', None) attrs['old_id'] = attrs.pop('id', None)
attrs['label'] = {settings.FALLBACK_LOCALE: value} attrs['translation'] = translation
attrs['value'] = slugify(value) attrs['value'] = slugify(value)
attrs['priority'] = attrs.pop('order_number') attrs['priority'] = attrs.pop('order_number')
attrs['category'] = self.tag_category attrs['category'] = self.tag_category
@ -55,8 +60,11 @@ class WineTypeSerializer(TransferSerializerMixin):
def validate(self, attrs): def validate(self, attrs):
value = attrs.pop('name') value = attrs.pop('name')
translation = SiteInterfaceDictionary(text={settings.FALLBACK_LOCALE: value},
keywords=f'tag.{self.CATEGORY_INDEX_NAME}.{slugify(value)}')
translation.save()
attrs['old_id'] = attrs.pop('id', None) attrs['old_id'] = attrs.pop('id', None)
attrs['label'] = {settings.FALLBACK_LOCALE: value} attrs['translation'] = translation
attrs['value'] = slugify(value) attrs['value'] = slugify(value)
attrs['category'] = self.tag_category attrs['category'] = self.tag_category
return attrs return attrs
@ -76,8 +84,11 @@ class WineBottlesProducedSerializer(TransferSerializerMixin):
def validate(self, attrs): def validate(self, attrs):
value = attrs.pop('bottles_produced') value = attrs.pop('bottles_produced')
translation = SiteInterfaceDictionary(text={settings.FALLBACK_LOCALE: value},
keywords=f'tag.{self.CATEGORY_INDEX_NAME}.{slugify(value)}')
translation.save()
parsed_value = self.parsed_value(value) parsed_value = self.parsed_value(value)
attrs['label'] = {settings.FALLBACK_LOCALE: parsed_value} attrs['translation'] = translation
attrs['value'] = slugify(parsed_value) attrs['value'] = slugify(parsed_value)
attrs['category'] = self.tag_category attrs['category'] = self.tag_category
return attrs return attrs

View File

@ -64,8 +64,11 @@ class CepageTagSerializer(TransferSerializerMixin):
def validate(self, attrs): def validate(self, attrs):
name = attrs.pop('name') name = attrs.pop('name')
value = attrs.pop('bottles_produced')
attrs['label'] = {settings.FALLBACK_LOCALE: name} translation = SiteInterfaceDictionary(text={settings.FALLBACK_LOCALE: value},
keywords=f'tag.{self.CATEGORY_INDEX_NAME}.{slugify(value)}')
translation.save()
attrs['translation'] = translation
attrs['value'] = slugify(name) attrs['value'] = slugify(name)
attrs['category'] = self.tag_category attrs['category'] = self.tag_category
return attrs return attrs

View File

@ -58,12 +58,12 @@ class SiteInterfaceDictionaryManager(LocaleManagerMixin):
if tag.translation: if tag.translation:
tag.translation.text = translations tag.translation.text = translations
tag.translation.page = 'tag' tag.translation.page = 'tag'
tag.translation.keywords = f'tag-{tag.pk}' tag.translation.keywords = f'tag.{tag.category.index_name}.{tag.value}'
else: else:
trans = SiteInterfaceDictionary({ trans = SiteInterfaceDictionary({
'text': translations, 'text': translations,
'page': 'tag', 'page': 'tag',
'keywords': f'tag-{tag.pk}' 'keywords': f'tag.{tag.category.index_name}.{tag.value}'
}) })
trans.save() trans.save()
tag.translation = trans tag.translation = trans
@ -77,12 +77,12 @@ class SiteInterfaceDictionaryManager(LocaleManagerMixin):
if tag_category.translation: if tag_category.translation:
tag_category.translation.text = translations tag_category.translation.text = translations
tag_category.translation.page = 'tag' tag_category.translation.page = 'tag'
tag_category.translation.keywords = f'tag_category-{tag_category.pk}' tag_category.translation.keywords = f'tag.{tag_category.category.index_name}'
else: else:
trans = SiteInterfaceDictionary({ trans = SiteInterfaceDictionary({
'text': translations, 'text': translations,
'page': 'tag', 'page': 'tag',
'keywords': f'tag_category-{tag_category.pk}' 'keywords': f'tag.{tag_category.category.index_name}'
}) })
trans.save() trans.save()
tag_category.translation = trans tag_category.translation = trans