~launchpad-pqm/launchpad/devel

14435.1.3 by Curtis Hovey
Moved vocabs to lp.soyuz.
1
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the GNU
11573.4.1 by Michael Hudson
move branch vocabularies to lp.code.vocabularies
2
# Affero General Public License version 3 (see the file LICENSE).
1742 by Canonical.com Patch Queue Manager
fix critical bugs in distro side searching. huge code cleanup.
3
14435.1.3 by Curtis Hovey
Moved vocabs to lp.soyuz.
4
"""Translations vocabularies."""
1742 by Canonical.com Patch Queue Manager
fix critical bugs in distro side searching. huge code cleanup.
5
6
__metaclass__ = type
7
2071 by Canonical.com Patch Queue Manager
[trivial] add more __all__ statements, other minor tidyings-up
8
__all__ = [
4796.3.9 by Carlos Perello Marin
Added admin pages for language packs
9
    'FilteredDeltaLanguagePackVocabulary',
10
    'FilteredFullLanguagePackVocabulary',
11
    'FilteredLanguagePackVocabulary',
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
12
    'TranslatableLanguageVocabulary',
2071 by Canonical.com Patch Queue Manager
[trivial] add more __all__ statements, other minor tidyings-up
13
    'TranslationGroupVocabulary',
5084.14.115 by Danilo Šegan
Initial TranslationMessage view.
14
    'TranslationMessageVocabulary',
7272.4.1 by Henning Eggers
Added more validation and explicitness to TranslationnImportEntryView.
15
    'TranslationTemplateVocabulary',
2071 by Canonical.com Patch Queue Manager
[trivial] add more __all__ statements, other minor tidyings-up
16
    ]
17
14550.1.1 by Steve Kowalik
Run format-imports over lib/lp and lib/canonical/launchpad
18
from sqlobject import AND
19
from zope.schema.vocabulary import SimpleTerm
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
20
14606.3.1 by William Grant
Merge canonical.database into lp.services.database.
21
from lp.registry.interfaces.distroseries import IDistroSeries
22
from lp.services.database.sqlbase import sqlvalues
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
23
from lp.services.webapp.vocabulary import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
24
    NamedSQLObjectVocabulary,
25
    SQLObjectVocabularyBase,
26
    )
27
from lp.services.worlddata.interfaces.language import ILanguage
14435.1.9 by Curtis Hovey
Fixed import error.
28
from lp.services.worlddata.vocabularies import LanguageVocabulary
12965.3.1 by Henning Eggers
Moved LanguagePackType to translations.enums.
29
from lp.translations.enums import LanguagePackType
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
30
from lp.translations.model.languagepack import LanguagePack
31
from lp.translations.model.potemplate import POTemplate
32
from lp.translations.model.translationgroup import TranslationGroup
33
from lp.translations.model.translationmessage import TranslationMessage
34
2736.1.22 by Mark Shuttleworth
karma categories in the db, and karma for specs
35
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
36
class TranslatableLanguageVocabulary(LanguageVocabulary):
37
    """All the translatable languages known by Launchpad.
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
38
4761.5.7 by Curtis Hovey
Changes per review.
39
    Messages cannot be translated into English or a non-visible language.
40
    This vocabulary contains all the languages known to Launchpad,
41
    excluding English and non-visible languages.
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
42
    """
11411.7.25 by j.c.sackett
Lint fixes.
43
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
44
    def __contains__(self, language):
4761.5.4 by Curtis Hovey
Changes per review. Extensive changes were made to support
45
        """See `IVocabulary`.
4761.5.7 by Curtis Hovey
Changes per review.
46
4761.5.4 by Curtis Hovey
Changes per review. Extensive changes were made to support
47
        This vocabulary excludes English and languages that are not visible.
48
        """
4761.5.5 by Curtis Hovey
Changes per review.
49
        assert ILanguage.providedBy(language), (
50
            "'in TranslatableLanguageVocabulary' requires ILanguage as "
51
            "left operand, got %s instead." % type(language))
52
        if language.code == 'en':
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
53
            return False
4761.5.7 by Curtis Hovey
Changes per review.
54
        return language.visible == True and super(
55
            TranslatableLanguageVocabulary, self).__contains__(language)
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
56
57
    def __iter__(self):
4761.5.4 by Curtis Hovey
Changes per review. Extensive changes were made to support
58
        """See `IVocabulary`.
4761.5.7 by Curtis Hovey
Changes per review.
59
60
        Iterate languages that are visible and not English.
4761.5.4 by Curtis Hovey
Changes per review. Extensive changes were made to support
61
        """
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
62
        languages = self._table.select(
4761.5.4 by Curtis Hovey
Changes per review. Extensive changes were made to support
63
            "Language.code != 'en' AND Language.visible = True",
64
            orderBy=self._orderBy)
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
65
        for language in languages:
66
            yield self.toTerm(language)
67
68
    def getTermByToken(self, token):
69
        """See `IVocabulary`."""
70
        if token == 'en':
71
            raise LookupError(token)
4761.5.7 by Curtis Hovey
Changes per review.
72
        term = super(TranslatableLanguageVocabulary, self).getTermByToken(
73
            token)
74
        if not term.value.visible:
75
            raise LookupError(token)
76
        return term
4679.2.1 by Curtis Hovey
Added TranslatableLanguagesVocabulary for the alternative
77
78
1818 by Canonical.com Patch Queue Manager
[r=spiv] teams of translators to be organised as groups by language
79
class TranslationGroupVocabulary(NamedSQLObjectVocabulary):
3024.2.3 by Guilherme Salgado
Make use of the ValidPersonOrTeamCache view in the people vocabs and fix https://launchpad.net/products/launchpad/+bug/29782 (SinglePopupWidget shouldn't use the vocabulary's name as title)
80
1818 by Canonical.com Patch Queue Manager
[r=spiv] teams of translators to be organised as groups by language
81
    _table = TranslationGroup
82
83
5084.14.115 by Danilo Šegan
Initial TranslationMessage view.
84
class TranslationMessageVocabulary(SQLObjectVocabularyBase):
85
86
    _table = TranslationMessage
87
    _orderBy = 'date_created'
88
89
    def toTerm(self, obj):
90
        translation = ''
91
        if obj.msgstr0 is not None:
92
            translation = obj.msgstr0.translation
93
        return SimpleTerm(obj, obj.id, translation)
94
95
    def __iter__(self):
96
        for message in self.context.messages:
97
            yield self.toTerm(message)
98
99
7272.4.1 by Henning Eggers
Added more validation and explicitness to TranslationnImportEntryView.
100
class TranslationTemplateVocabulary(SQLObjectVocabularyBase):
101
    """The set of all POTemplates for a given product or package."""
102
103
    _table = POTemplate
104
    _orderBy = 'name'
105
106
    def __init__(self, context):
107
        if context.productseries != None:
7365.4.1 by Henning Eggers
Fixed vocabulary for translation templates to only include current entries.
108
            self._filter = AND(
109
                POTemplate.iscurrent == True,
11411.7.28 by j.c.sackett
Lint fixes.
110
                POTemplate.productseries == context.productseries)
7272.4.1 by Henning Eggers
Added more validation and explicitness to TranslationnImportEntryView.
111
        else:
112
            self._filter = AND(
7365.4.1 by Henning Eggers
Fixed vocabulary for translation templates to only include current entries.
113
                POTemplate.iscurrent == True,
7272.4.1 by Henning Eggers
Added more validation and explicitness to TranslationnImportEntryView.
114
                POTemplate.distroseries == context.distroseries,
11411.7.28 by j.c.sackett
Lint fixes.
115
                POTemplate.sourcepackagename == context.sourcepackagename)
7272.4.1 by Henning Eggers
Added more validation and explicitness to TranslationnImportEntryView.
116
        super(TranslationTemplateVocabulary, self).__init__(context)
117
118
    def toTerm(self, obj):
119
        return SimpleTerm(obj, obj.id, obj.name)
120
121
4796.3.9 by Carlos Perello Marin
Added admin pages for language packs
122
class FilteredLanguagePackVocabularyBase(SQLObjectVocabularyBase):
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
123
    """Base vocabulary class to retrieve language packs for a distroseries."""
124
    _table = LanguagePack
125
    _orderBy = '-date_exported'
126
127
    def toTerm(self, obj):
4796.3.15 by Carlos Perello Marin
Added more tests and links to the new pages
128
        return SimpleTerm(
4796.3.21 by Carlos Perello Marin
Applied review comments
129
            obj, obj.id, '%s' % obj.date_exported.strftime('%F %T %Z'))
130
131
    def _baseQueryList(self):
132
        """Return a list of sentences that defines the specific filtering.
133
134
        That list will be linked with an ' AND '.
135
        """
136
        raise NotImplementedError
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
137
138
    def __iter__(self):
139
        if not IDistroSeries.providedBy(self.context):
140
            # This vocabulary is only useful from a DistroSeries context.
141
            return
4796.3.10 by Carlos Perello Marin
Finished some integration bits + tests
142
4796.3.21 by Carlos Perello Marin
Applied review comments
143
        query = self._baseQueryList()
4796.3.9 by Carlos Perello Marin
Added admin pages for language packs
144
        query.append('distroseries = %s' % sqlvalues(self.context))
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
145
        language_packs = self._table.select(
146
            ' AND '.join(query), orderBy=self._orderBy)
147
148
        for language_pack in language_packs:
149
            yield self.toTerm(language_pack)
150
151
4796.3.9 by Carlos Perello Marin
Added admin pages for language packs
152
class FilteredFullLanguagePackVocabulary(FilteredLanguagePackVocabularyBase):
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
153
    """Full export Language Pack for a distribution series."""
154
    displayname = 'Select a full export language pack'
4796.3.21 by Carlos Perello Marin
Applied review comments
155
156
    def _baseQueryList(self):
157
        """See `FilteredLanguagePackVocabularyBase`."""
158
        return ['type = %s' % sqlvalues(LanguagePackType.FULL)]
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
159
160
4796.3.9 by Carlos Perello Marin
Added admin pages for language packs
161
class FilteredDeltaLanguagePackVocabulary(FilteredLanguagePackVocabularyBase):
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
162
    """Delta export Language Pack for a distribution series."""
163
    displayname = 'Select a delta export language pack'
4796.3.21 by Carlos Perello Marin
Applied review comments
164
165
    def _baseQueryList(self):
166
        """See `FilteredLanguagePackVocabularyBase`."""
167
        return ['(type = %s AND updates = %s)' % sqlvalues(
168
            LanguagePackType.DELTA, self.context.language_pack_base)]
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
169
170
4796.3.9 by Carlos Perello Marin
Added admin pages for language packs
171
class FilteredLanguagePackVocabulary(FilteredLanguagePackVocabularyBase):
4796.3.4 by Carlos Perello Marin
Initial implementation for all supporting classes required for language pack UI handling
172
    displayname = 'Select a language pack'
4796.3.21 by Carlos Perello Marin
Applied review comments
173
174
    def toTerm(self, obj):
175
        return SimpleTerm(
176
            obj, obj.id, '%s (%s)' % (
177
                obj.date_exported.strftime('%F %T %Z'), obj.type.title))
178
179
    def _baseQueryList(self):
180
        """See `FilteredLanguagePackVocabularyBase`."""
181
        # We are interested on any full language pack or language pack
182
        # that is a delta of the current base lanuage pack type,
183
        # except the ones already used.
184
        used_lang_packs = []
185
        if self.context.language_pack_base is not None:
186
            used_lang_packs.append(self.context.language_pack_base.id)
187
        if self.context.language_pack_delta is not None:
188
            used_lang_packs.append(self.context.language_pack_delta.id)
189
        query = []
190
        if used_lang_packs:
191
            query.append('id NOT IN %s' % sqlvalues(used_lang_packs))
192
        query.append('(updates is NULL OR updates = %s)' % sqlvalues(
193
            self.context.language_pack_base))
194
        return query