2015-08-12 17:10:52 +02:00
|
|
|
from django.db import models
|
2015-09-20 17:51:25 +02:00
|
|
|
from django.db.models.fields.related import ForwardManyToOneDescriptor
|
2015-08-12 17:10:52 +02:00
|
|
|
from django.utils.translation import get_language
|
|
|
|
|
|
|
|
|
2015-09-20 17:51:25 +02:00
|
|
|
class ArticleTranslationDescriptor(ForwardManyToOneDescriptor):
|
2015-08-12 17:10:52 +02:00
|
|
|
"""
|
|
|
|
The set of articletranslation should not set any local fields.
|
|
|
|
"""
|
|
|
|
def __set__(self, instance, value):
|
|
|
|
if instance is None:
|
|
|
|
raise AttributeError("%s must be accessed via instance" % self.field.name)
|
|
|
|
setattr(instance, self.cache_name, value)
|
|
|
|
if value is not None and not self.field.remote_field.multiple:
|
|
|
|
setattr(value, self.field.related.get_cache_name(), instance)
|
|
|
|
|
|
|
|
|
2017-01-19 08:39:46 +01:00
|
|
|
class ColConstraint:
|
2015-08-12 17:10:52 +02:00
|
|
|
# Anything with as_sql() method works in get_extra_restriction().
|
|
|
|
def __init__(self, alias, col, value):
|
|
|
|
self.alias, self.col, self.value = alias, col, value
|
|
|
|
|
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
qn = compiler.quote_name_unless_alias
|
|
|
|
return '%s.%s = %%s' % (qn(self.alias), qn(self.col)), [self.value]
|
|
|
|
|
|
|
|
|
|
|
|
class ActiveTranslationField(models.ForeignObject):
|
|
|
|
"""
|
|
|
|
This field will allow querying and fetching the currently active translation
|
|
|
|
for Article from ArticleTranslation.
|
|
|
|
"""
|
|
|
|
requires_unique_target = False
|
|
|
|
|
|
|
|
def get_extra_restriction(self, where_class, alias, related_alias):
|
|
|
|
return ColConstraint(alias, 'lang', get_language())
|
|
|
|
|
|
|
|
def get_extra_descriptor_filter(self, instance):
|
|
|
|
return {'lang': get_language()}
|
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name):
|
2017-01-21 14:13:44 +01:00
|
|
|
super().contribute_to_class(cls, name)
|
2015-08-12 17:10:52 +02:00
|
|
|
setattr(cls, self.name, ArticleTranslationDescriptor(self))
|
|
|
|
|
|
|
|
|
2016-01-29 10:03:47 +01:00
|
|
|
class ActiveTranslationFieldWithQ(ActiveTranslationField):
|
|
|
|
def get_extra_descriptor_filter(self, instance):
|
|
|
|
return models.Q(lang=get_language())
|
|
|
|
|
|
|
|
|
2015-08-12 17:10:52 +02:00
|
|
|
class Article(models.Model):
|
|
|
|
active_translation = ActiveTranslationField(
|
|
|
|
'ArticleTranslation',
|
|
|
|
from_fields=['id'],
|
|
|
|
to_fields=['article'],
|
|
|
|
related_name='+',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
)
|
2016-01-29 10:03:47 +01:00
|
|
|
active_translation_q = ActiveTranslationFieldWithQ(
|
|
|
|
'ArticleTranslation',
|
|
|
|
from_fields=['id'],
|
|
|
|
to_fields=['article'],
|
|
|
|
related_name='+',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
)
|
2015-08-12 17:10:52 +02:00
|
|
|
pub_date = models.DateField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
try:
|
|
|
|
return self.active_translation.title
|
|
|
|
except ArticleTranslation.DoesNotExist:
|
|
|
|
return '[No translation found]'
|
|
|
|
|
|
|
|
|
|
|
|
class NewsArticle(Article):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ArticleTranslation(models.Model):
|
|
|
|
article = models.ForeignKey(Article, models.CASCADE)
|
|
|
|
lang = models.CharField(max_length=2)
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
body = models.TextField()
|
|
|
|
abstract = models.CharField(max_length=400, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('article', 'lang')
|
|
|
|
ordering = ('active_translation__title',)
|
|
|
|
|
|
|
|
|
|
|
|
class ArticleTag(models.Model):
|
|
|
|
article = models.ForeignKey(
|
|
|
|
Article,
|
|
|
|
models.CASCADE,
|
|
|
|
related_name='tags',
|
|
|
|
related_query_name='tag',
|
|
|
|
)
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
|
|
|
|
class ArticleIdea(models.Model):
|
|
|
|
articles = models.ManyToManyField(
|
|
|
|
Article,
|
|
|
|
related_name='ideas',
|
|
|
|
related_query_name='idea_things',
|
|
|
|
)
|
|
|
|
name = models.CharField(max_length=255)
|