/
abstract_base_page_translation.py
93 lines (76 loc) · 3 KB
/
abstract_base_page_translation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations
from typing import TYPE_CHECKING
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
if TYPE_CHECKING:
from typing import Literal
from .abstract_base_page import AbstractBasePage
from ..abstract_content_translation import AbstractContentTranslation
class AbstractBasePageTranslation(AbstractContentTranslation):
"""
Data model representing a page or imprint page translation
"""
@cached_property
def page(self) -> AbstractBasePage:
"""
The page the translation belongs to
To be implemented in the inheriting model
"""
raise NotImplementedError
@cached_property
def short_url(self) -> str:
"""
This property calculates the short url dynamically
To be implemented in the inheriting model
"""
raise NotImplementedError
@staticmethod
def foreign_field() -> Literal["page"]:
"""
Returns the string "page" which ist the field name of the reference to the page which the translation belongs to
:return: The foreign field name
"""
return "page"
@cached_property
def foreign_object(self) -> AbstractBasePage:
"""
This property is an alias of the page foreign key and is needed to generalize the :mod:`~integreat_cms.cms.utils.slug_utils`
for all content types
:return: The page to which the translation belongs
"""
return self.page
@cached_property
def readable_title(self) -> str:
"""
Get the title of a page translation including the title in the best translation
:return: The readable title of the page translation
"""
# Build readable page translation title
best_translation = self.page.best_translation
best_translation_title = (
f' {best_translation.language}: "{best_translation.title}"'
)
# Check whether page translation has title
if self.title:
# Start with translation title if exists
readable_title = f'"{self.title}"'
if (
best_translation.title != self.title
and best_translation.language != self.language
):
readable_title += " (" + _("Title in") + best_translation_title + ")"
else:
# Start directly with the title of the best translation
readable_title = _("with the title in") + best_translation_title
return readable_title
class Meta:
#: The verbose name of the model
verbose_name = _("page translation")
#: The plural verbose name of the model
verbose_name_plural = _("page translations")
#: The fields which are used to sort the returned objects of a QuerySet
ordering = ["page", "-version"]
#: The default permissions for this model
default_permissions = ()
#: This model is an abstract base class
abstract = True