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