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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# Copyright 2010 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Implementation class for objects that `POTemplate`s can belong to."""
__metaclass__ = type
__all__ = [
'HasTranslationTemplatesMixin',
]
from storm.expr import (
Count,
Desc,
)
from zope.interface import implements
from canonical.launchpad import helpers
from lp.translations.interfaces.hastranslationtemplates import (
IHasTranslationTemplates,
)
from lp.translations.model.pofile import POFile
from lp.translations.model.potemplate import POTemplate
class HasTranslationTemplatesMixin:
"""Helper class for implementing `IHasTranslationTemplates`."""
implements(IHasTranslationTemplates)
def getTemplatesCollection(self):
"""See `IHasTranslationTemplates`.
To be provided by derived classes.
"""
raise NotImplementedError(
"Child class must provide getTemplatesCollection.")
def getSharingPartner(self):
"""See `IHasTranslationTemplates`.
To be provided by derived classes.
"""
raise NotImplementedError(
"Child class must provide getSharingPartner.")
def _orderTemplates(self, result):
"""Apply the conventional ordering to a result set of templates."""
return result.order_by(Desc(POTemplate.priority), POTemplate.name)
def getCurrentTemplatesCollection(self, current_value=True):
"""See `IHasTranslationTemplates`."""
return self.getTemplatesCollection().restrictCurrent(current_value)
def getCurrentTranslationTemplates(self,
just_ids=False,
current_value=True):
"""See `IHasTranslationTemplates`."""
if just_ids:
selection = POTemplate.id
else:
selection = POTemplate
collection = self.getCurrentTemplatesCollection(current_value)
return self._orderTemplates(collection.select(selection))
@property
def has_translation_templates(self):
"""See `IHasTranslationTemplates`."""
return bool(self.getTranslationTemplates().any())
@property
def has_current_translation_templates(self):
"""See `IHasTranslationTemplates`."""
return bool(
self.getCurrentTranslationTemplates(just_ids=True).any())
@property
def has_obsolete_translation_templates(self):
"""See `IHasTranslationTemplates`."""
return bool(
self.getCurrentTranslationTemplates(
just_ids=True, current_value=False).any())
@property
def has_sharing_translation_templates(self):
"""See `IHasTranslationTemplates`."""
other_side_obj = self.getSharingPartner()
if other_side_obj is None:
return False
return other_side_obj.has_current_translation_templates
def getCurrentTranslationFiles(self, just_ids=False):
"""See `IHasTranslationTemplates`."""
if just_ids:
selection = POFile.id
else:
selection = POFile
collection = self.getCurrentTemplatesCollection()
return collection.joinPOFile().select(selection)
@property
def has_translation_files(self):
"""See `IHasTranslationTemplates`."""
return bool(
self.getCurrentTranslationFiles(just_ids=True).any())
def getTranslationTemplates(self):
"""See `IHasTranslationTemplates`."""
return self._orderTemplates(self.getTemplatesCollection().select())
def getTranslationTemplateByName(self, name):
"""See `IHasTranslationTemplates`."""
return self.getTemplatesCollection().restrictName(name).select().one()
def getTranslationTemplateFormats(self):
"""See `IHasTranslationTemplates`."""
formats_query = self.getCurrentTranslationTemplates().order_by(
'source_file_format').config(distinct=True)
return helpers.shortlist(
formats_query.values(POTemplate.source_file_format), 10)
def getTemplatesAndLanguageCounts(self):
"""See `IHasTranslationTemplates`."""
join = self.getTemplatesCollection().joinOuterPOFile()
result = join.select(POTemplate, Count(POFile.id))
return result.group_by(POTemplate)
|