117 lines
3.9 KiB
Python
117 lines
3.9 KiB
Python
"""Translation app models."""
|
|
from django.apps import apps
|
|
from django.contrib.postgres.fields import JSONField
|
|
from django.contrib.postgres.indexes import GinIndex
|
|
from django.db import models
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from utils.models import ProjectBaseMixin, LocaleManagerMixin
|
|
|
|
|
|
class LanguageQuerySet(models.QuerySet):
|
|
"""QuerySet for model Language"""
|
|
|
|
def active(self, switcher=True):
|
|
"""Filter only active users."""
|
|
return self.filter(is_active=switcher)
|
|
|
|
def by_locale(self, locale: str) -> models.QuerySet:
|
|
"""Filter by locale"""
|
|
return self.filter(locale=locale)
|
|
|
|
def by_title(self, title: str) -> models.QuerySet:
|
|
"""Filter by title"""
|
|
return self.filter(title=title)
|
|
|
|
|
|
class Language(models.Model):
|
|
"""Language model."""
|
|
|
|
title = models.CharField(max_length=255,
|
|
verbose_name=_('Language title'))
|
|
locale = models.CharField(max_length=10,
|
|
verbose_name=_('Locale identifier'))
|
|
|
|
old_id = models.IntegerField(null=True, blank=True, default=None)
|
|
|
|
is_active = models.BooleanField(_('is active'), default=True)
|
|
|
|
objects = LanguageQuerySet.as_manager()
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
verbose_name = _('Language')
|
|
verbose_name_plural = _('Languages')
|
|
unique_together = ('title', 'locale')
|
|
|
|
def __str__(self):
|
|
"""String method"""
|
|
return f'{self.title} ({self.locale})'
|
|
|
|
|
|
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.category.index_name}.{tag.value}'
|
|
else:
|
|
trans = SiteInterfaceDictionary({
|
|
'text': translations,
|
|
'page': 'tag',
|
|
'keywords': f'tag.{tag.category.index_name}.{tag.value}'
|
|
})
|
|
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.{tag_category.category.index_name}'
|
|
else:
|
|
trans = SiteInterfaceDictionary({
|
|
'text': translations,
|
|
'page': 'tag',
|
|
'keywords': f'tag.{tag_category.category.index_name}'
|
|
})
|
|
trans.save()
|
|
tag_category.translation = trans
|
|
tag_category.save()
|
|
|
|
|
|
class SiteInterfaceDictionary(ProjectBaseMixin):
|
|
"""Site interface dictionary model."""
|
|
|
|
page = models.CharField(max_length=255, db_index=True,
|
|
verbose_name=_('Page'))
|
|
keywords = models.CharField(max_length=255, verbose_name='Keywords')
|
|
text = JSONField(_('Text'), null=True, blank=True,
|
|
default=None, help_text='{"en-GB":"some text"}')
|
|
|
|
objects = SiteInterfaceDictionaryManager()
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
verbose_name = _('Site interface dictionary')
|
|
verbose_name_plural = _('Site interface dictionary')
|
|
indexes = [
|
|
GinIndex(fields=['text'])
|
|
]
|
|
|
|
def __str__(self):
|
|
return f'{self.page}: {self.keywords}'
|