2015-08-12 15:10:52 +00:00
|
|
|
from django.db import models
|
2015-09-20 15:51:25 +00:00
|
|
|
from django.db.models.fields.related import ForwardManyToOneDescriptor
|
2015-08-12 15:10:52 +00:00
|
|
|
from django.utils.translation import get_language
|
|
|
|
|
|
|
|
|
2015-09-20 15:51:25 +00:00
|
|
|
class ArticleTranslationDescriptor(ForwardManyToOneDescriptor):
|
2015-08-12 15:10:52 +00: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)
|
2016-06-04 23:10:37 +00:00
|
|
|
self.field.set_cached_value(instance, value)
|
2015-08-12 15:10:52 +00:00
|
|
|
if value is not None and not self.field.remote_field.multiple:
|
2016-06-04 23:10:37 +00:00
|
|
|
self.field.remote_field.set_cached_value(value, instance)
|
2015-08-12 15:10:52 +00:00
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class ColConstraint:
|
2015-08-12 15:10:52 +00: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 13:13:44 +00:00
|
|
|
super().contribute_to_class(cls, name)
|
2015-08-12 15:10:52 +00:00
|
|
|
setattr(cls, self.name, ArticleTranslationDescriptor(self))
|
|
|
|
|
|
|
|
|
2016-01-29 09:03:47 +00:00
|
|
|
class ActiveTranslationFieldWithQ(ActiveTranslationField):
|
|
|
|
def get_extra_descriptor_filter(self, instance):
|
|
|
|
return models.Q(lang=get_language())
|
|
|
|
|
|
|
|
|
2015-08-12 15:10:52 +00: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 09:03:47 +00:00
|
|
|
active_translation_q = ActiveTranslationFieldWithQ(
|
|
|
|
'ArticleTranslation',
|
|
|
|
from_fields=['id'],
|
|
|
|
to_fields=['article'],
|
|
|
|
related_name='+',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
)
|
2015-08-12 15:10:52 +00: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()
|
2017-05-10 15:33:46 +00:00
|
|
|
abstract = models.TextField(null=True)
|
2015-08-12 15:10:52 +00:00
|
|
|
|
|
|
|
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)
|