~launchpad-pqm/launchpad/devel

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

__metaclass__ = type

__all__ = [
    'RosettaApplicationView',
    'RosettaStatsView',
    'RosettaApplicationNavigation',
    'TranslateRedirectView',
    'TranslationsLanguageBreadcrumb',
    'TranslationsMixin',
    'TranslationsRedirectView',
    'TranslationsVHostBreadcrumb',
    ]

from zope.component import getUtility

from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.registry.interfaces.person import IPersonSet
from lp.registry.interfaces.product import IProductSet
from lp.services.config import config
from lp.services.geoip.interfaces import IRequestPreferredLanguages
from lp.services.propertycache import cachedproperty
from lp.services.webapp import (
    canonical_url,
    LaunchpadView,
    Navigation,
    stepto,
    )
from lp.services.webapp.batching import BatchNavigator
from lp.services.webapp.breadcrumb import Breadcrumb
from lp.services.webapp.interfaces import ILaunchpadRoot
from lp.services.worlddata.helpers import preferred_or_request_languages
from lp.services.worlddata.interfaces.country import ICountry
from lp.translations.interfaces.translations import IRosettaApplication
from lp.translations.publisher import TranslationsLayer


class TranslationsMixin:
    """Provide Translations specific properties."""

    @cachedproperty
    def translatable_languages(self):
        """Return a set of the Person's translatable languages."""
        english = getUtility(ILaunchpadCelebrities).english
        languages = preferred_or_request_languages(self.request)
        if english in languages:
            return [lang for lang in languages if lang != english]
        return languages

    @cachedproperty
    def answers_url(self):
        return canonical_url(
            getUtility(ILaunchpadCelebrities).launchpad,
            rootsite='answers')


class RosettaApplicationView(LaunchpadView, TranslationsMixin):
    """View for various top-level Translations pages."""

    page_title = 'Launchpad Translations'

    @property
    def ubuntu_translationseries(self):
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        series = ubuntu.translation_focus
        if series is None:
            return ubuntu.currentseries
        else:
            return series

    def ubuntu_languages(self):
        langs = []
        series = self.ubuntu_translationseries
        for language in self.languages:
            langs.append(series.getDistroSeriesLanguageOrDummy(language))
        return langs

    def requestCountry(self):
        return ICountry(self.request, None)

    def browserLanguages(self):
        return IRequestPreferredLanguages(
            self.request).getPreferredLanguages()

    @cachedproperty
    def batchnav(self):
        """Return a BatchNavigator for the list of translatable products."""
        products = getUtility(IProductSet)
        return BatchNavigator(products.getTranslatables(),
                              self.request)

    def rosettaAdminEmail(self):
        return config.rosettaadmin.email

    @property
    def launchpad_users_team(self):
        """The url of the launchpad-users team."""
        team = getUtility(IPersonSet).getByName('launchpad-users')
        return canonical_url(team)


class TranslatableProductsView(LaunchpadView):
    """List of translatable products."""
    label = "Projects with translations in Launchpad"
    page_title = label

    @cachedproperty
    def batchnav(self):
        """Navigate the list of translatable products."""
        return BatchNavigator(
            getUtility(IProductSet).getTranslatables(), self.request)


class RosettaStatsView:
    """A view class for objects that support IRosettaStats. This is mainly
    used for the sortable untranslated percentage."""

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def sortable_untranslated(self):
        return '%06.2f' % self.context.untranslatedPercentage()


class RosettaApplicationNavigation(Navigation):

    usedfor = IRosettaApplication

    newlayer = TranslationsLayer

    @stepto('groups')
    def redirect_groups(self):
        """Redirect /translations/+groups to Translations root site."""
        target_url = canonical_url(
            getUtility(ILaunchpadRoot), rootsite='translations')
        return self.redirectSubTree(
            target_url + '+groups', status=301)

    @stepto('imports')
    def redirect_imports(self):
        """Redirect /translations/imports to Translations root site."""
        target_url = canonical_url(
            getUtility(ILaunchpadRoot), rootsite='translations')
        return self.redirectSubTree(
            target_url + '+imports', status=301)

    @stepto('projects')
    def projects(self):
        # DEPRECATED
        return getUtility(IProductSet)

    @stepto('products')
    def products(self):
        # DEPRECATED
        return getUtility(IProductSet)


class PageRedirectView:
    """Redirects to translations site for the given page."""

    def __init__(self, context, request, page):
        self.context = context
        self.request = request
        self.page = page

    def __call__(self):
        """Redirect to self.page in the translations site."""
        self.request.response.redirect(
            '/'.join([
                canonical_url(self.context, rootsite='translations'),
                self.page,
                ]), status=301)


class TranslateRedirectView(PageRedirectView):
    """Redirects to translations site for +translate page."""

    def __init__(self, context, request):
        PageRedirectView.__init__(self, context, request, '+translate')


class TranslationsRedirectView(PageRedirectView):
    """Redirects to translations site for +translations page."""

    def __init__(self, context, request):
        PageRedirectView.__init__(self, context, request, '+translations')


class TranslationsVHostBreadcrumb(Breadcrumb):
    rootsite = 'translations'
    text = 'Translations'


class TranslationsLanguageBreadcrumb(Breadcrumb):
    """Breadcrumb for objects with language."""

    @property
    def text(self):
        return self.context.language.displayname