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.
|
|
|
|
"""
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2015-08-12 17:10:52 +02:00
|
|
|
def __set__(self, instance, value):
|
|
|
|
if instance is None:
|
|
|
|
raise AttributeError("%s must be accessed via instance" % self.field.name)
|
2016-06-05 01:10:37 +02:00
|
|
|
self.field.set_cached_value(instance, value)
|
2015-08-12 17:10:52 +02:00
|
|
|
if value is not None and not self.field.remote_field.multiple:
|
2016-06-05 01:10:37 +02:00
|
|
|
self.field.remote_field.set_cached_value(value, instance)
|
2015-08-12 17:10:52 +02:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2015-08-12 17:10:52 +02:00
|
|
|
requires_unique_target = False
|
|
|
|
|
2021-07-20 21:38:17 +02:00
|
|
|
def get_extra_restriction(self, alias, related_alias):
|
2015-08-12 17:10:52 +02: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 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()
|
2017-05-10 17:33:46 +02:00
|
|
|
abstract = models.TextField(null=True)
|
2015-08-12 17:10:52 +02: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)
|