~launchpad-pqm/launchpad/devel

8687.15.18 by Karl Fogel
Add the copyright header block to files under lib/canonical/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
3
4
__metaclass__ = type
5
2450 by Canonical.com Patch Queue Manager
[r=jamesh] rework cve structure, and general polish
6
__all__ = [
7
    'RosettaApplicationView',
8
    'RosettaStatsView',
4346.7.2 by Curtis Hovey
Added a mixin class to provide the Person's translatable_languages.
9
    'RosettaApplicationNavigation',
4910.1.7 by Danilo Šegan
Apply carlos' +translate/+translations redirects and add language packs migration script.
10
    'TranslateRedirectView',
9360.4.1 by Danilo Šegan
Migrate DistroSeriesLanguage page to 3.0 and introduce breadcrumbs.
11
    'TranslationsLanguageBreadcrumb',
4910.1.7 by Danilo Šegan
Apply carlos' +translate/+translations redirects and add language packs migration script.
12
    'TranslationsMixin',
13
    'TranslationsRedirectView',
9360.4.1 by Danilo Šegan
Migrate DistroSeriesLanguage page to 3.0 and introduce breadcrumbs.
14
    'TranslationsVHostBreadcrumb',
2450 by Canonical.com Patch Queue Manager
[r=jamesh] rework cve structure, and general polish
15
    ]
2000 by Canonical.com Patch Queue Manager
[r=kiko] move browser code to proper location, remove dead code, clarify prefs code
16
4268.3.27 by Carlos Perello Marin
Applied review comments
17
from zope.component import getUtility
3691.216.5 by Guilherme Salgado
Create a new page under Launchpad's root to redirect non-logged in users to their +editlanguages page, after they log in. Also redirect from /rosetta/prefs to the new page, to keep old bookmarks working
18
14612.2.1 by William Grant
format-imports on lib/. So many imports.
19
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
20
from lp.registry.interfaces.person import IPersonSet
21
from lp.registry.interfaces.product import IProductSet
14605.1.1 by Curtis Hovey
Moved canonical.config to lp.services.
22
from lp.services.config import config
14612.2.1 by William Grant
format-imports on lib/. So many imports.
23
from lp.services.geoip.interfaces import IRequestPreferredLanguages
24
from lp.services.propertycache import cachedproperty
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
25
from lp.services.webapp import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
26
    canonical_url,
27
    LaunchpadView,
28
    Navigation,
29
    stepto,
30
    )
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
31
from lp.services.webapp.batching import BatchNavigator
32
from lp.services.webapp.breadcrumb import Breadcrumb
33
from lp.services.webapp.interfaces import ILaunchpadRoot
14593.2.14 by Curtis Hovey
Extracted language helpers to lp.services.worlddata.helpers.
34
from lp.services.worlddata.helpers import preferred_or_request_languages
8751.1.1 by Danilo Šegan
Store migration changes so far.
35
from lp.services.worlddata.interfaces.country import ICountry
14612.2.1 by William Grant
format-imports on lib/. So many imports.
36
from lp.translations.interfaces.translations import IRosettaApplication
11128.6.5 by Michael Hudson
translations
37
from lp.translations.publisher import TranslationsLayer
1249 by Canonical.com Patch Queue Manager
allow uploading tar files when creating a PO template
38
4346.7.2 by Curtis Hovey
Added a mixin class to provide the Person's translatable_languages.
39
40
class TranslationsMixin:
7158.1.1 by Henning Eggers
Moved answers_url* properties to Mixin. Now other templates are using these properties, too.
41
    """Provide Translations specific properties."""
4346.7.8 by Curtis Hovey
Revision per review. Removed unused imports.
42
10241.1.1 by Jeroen Vermeulen
Cache translatable_languages, now that we use it so often.
43
    @cachedproperty
4346.7.2 by Curtis Hovey
Added a mixin class to provide the Person's translatable_languages.
44
    def translatable_languages(self):
45
        """Return a set of the Person's translatable languages."""
7117.5.2 by Jeroen Vermeulen
Made English a celebrity.
46
        english = getUtility(ILaunchpadCelebrities).english
14593.2.14 by Curtis Hovey
Extracted language helpers to lp.services.worlddata.helpers.
47
        languages = preferred_or_request_languages(self.request)
4346.7.5 by Curtis Hovey
Added rule to check and remove English from translatable_languages.
48
        if english in languages:
4215.2.27 by Curtis Hovey
Resolved English visibility problem cause by last merge.
49
            return [lang for lang in languages if lang != english]
4346.7.5 by Curtis Hovey
Added rule to check and remove English from translatable_languages.
50
        return languages
1491 by Canonical.com Patch Queue Manager
merging mark's branch.
51
7158.1.1 by Henning Eggers
Moved answers_url* properties to Mixin. Now other templates are using these properties, too.
52
    @cachedproperty
53
    def answers_url(self):
54
        return canonical_url(
12097.3.1 by Danilo Segan
Get rid of the lp_translations celebrity and callsites.
55
            getUtility(ILaunchpadCelebrities).launchpad,
7158.1.1 by Henning Eggers
Moved answers_url* properties to Mixin. Now other templates are using these properties, too.
56
            rootsite='answers')
57
9399.1.1 by Jeroen Vermeulen
Basic 3.0 conversion with minor cleanups.
58
13303.11.10 by Aaron Bentley
Fix pages broken by assuming LaunchpadView.
59
class RosettaApplicationView(LaunchpadView, TranslationsMixin):
9399.1.1 by Jeroen Vermeulen
Basic 3.0 conversion with minor cleanups.
60
    """View for various top-level Translations pages."""
4346.7.2 by Curtis Hovey
Added a mixin class to provide the Person's translatable_languages.
61
14433.2.53 by Curtis Hovey
Moved page_title into view.
62
    page_title = 'Launchpad Translations'
63
2344 by Canonical.com Patch Queue Manager
[not r=kiko] specification tracker
64
    @property
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
65
    def ubuntu_translationseries(self):
3635.2.5 by Carlos Perello Marin
Applied review comments from kiko
66
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
67
        series = ubuntu.translation_focus
68
        if series is None:
69
            return ubuntu.currentseries
3635.2.5 by Carlos Perello Marin
Applied review comments from kiko
70
        else:
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
71
            return series
2344 by Canonical.com Patch Queue Manager
[not r=kiko] specification tracker
72
2296 by Canonical.com Patch Queue Manager
[trivial] more homepage love
73
    def ubuntu_languages(self):
74
        langs = []
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
75
        series = self.ubuntu_translationseries
2296 by Canonical.com Patch Queue Manager
[trivial] more homepage love
76
        for language in self.languages:
4285.2.1 by Mark Shuttleworth
Massive renaming of distrorelease to distroseries
77
            langs.append(series.getDistroSeriesLanguageOrDummy(language))
2296 by Canonical.com Patch Queue Manager
[trivial] more homepage love
78
        return langs
79
1491 by Canonical.com Patch Queue Manager
merging mark's branch.
80
    def requestCountry(self):
1977 by Canonical.com Patch Queue Manager
[r=Bjorn] some clean up of helpers.py
81
        return ICountry(self.request, None)
1491 by Canonical.com Patch Queue Manager
merging mark's branch.
82
83
    def browserLanguages(self):
4346.7.8 by Curtis Hovey
Revision per review. Removed unused imports.
84
        return IRequestPreferredLanguages(
85
            self.request).getPreferredLanguages()
1491 by Canonical.com Patch Queue Manager
merging mark's branch.
86
3755.5.8 by Danilo Šegan
Add a property, rename to batchnav.
87
    @cachedproperty
88
    def batchnav(self):
89
        """Return a BatchNavigator for the list of translatable products."""
3755.5.6 by Danilo Šegan
Add the products page.
90
        products = getUtility(IProductSet)
3755.5.12 by Danilo Šegan
Clean-up test and everything.
91
        return BatchNavigator(products.getTranslatables(),
3755.5.6 by Danilo Šegan
Add the products page.
92
                              self.request)
93
3691.293.7 by Danilo Šegan
Replace hard-coded value in template as well.
94
    def rosettaAdminEmail(self):
6048.1.10 by Curtis Hovey
Restored the rosettaadmin config keys that were accidentally reverted a few days ago. Added a sorted() call to fix random failure of a list in a test.
95
        return config.rosettaadmin.email
3691.293.7 by Danilo Šegan
Replace hard-coded value in template as well.
96
7020.3.1 by Barry Warsaw
Update all references to the launchpad-users@lists.canonical.com to use
97
    @property
98
    def launchpad_users_team(self):
99
        """The url of the launchpad-users team."""
100
        team = getUtility(IPersonSet).getByName('launchpad-users')
101
        return canonical_url(team)
102
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
103
9399.1.1 by Jeroen Vermeulen
Basic 3.0 conversion with minor cleanups.
104
class TranslatableProductsView(LaunchpadView):
105
    """List of translatable products."""
106
    label = "Projects with translations in Launchpad"
14433.2.54 by Curtis Hovey
Moved page_title into view.
107
    page_title = label
9399.1.1 by Jeroen Vermeulen
Basic 3.0 conversion with minor cleanups.
108
109
    @cachedproperty
110
    def batchnav(self):
111
        """Navigate the list of translatable products."""
112
        return BatchNavigator(
113
            getUtility(IProductSet).getTranslatables(), self.request)
114
115
2450 by Canonical.com Patch Queue Manager
[r=jamesh] rework cve structure, and general polish
116
class RosettaStatsView:
117
    """A view class for objects that support IRosettaStats. This is mainly
118
    used for the sortable untranslated percentage."""
119
120
    def __init__(self, context, request):
121
        self.context = context
122
        self.request = request
123
124
    def sortable_untranslated(self):
125
        return '%06.2f' % self.context.untranslatedPercentage()
126
127
2622 by Canonical.com Patch Queue Manager
[trivial] remove all uses of browser:suburl and nuke the suburl directive code.
128
class RosettaApplicationNavigation(Navigation):
129
130
    usedfor = IRosettaApplication
131
4268.3.27 by Carlos Perello Marin
Applied review comments
132
    newlayer = TranslationsLayer
2622 by Canonical.com Patch Queue Manager
[trivial] remove all uses of browser:suburl and nuke the suburl directive code.
133
134
    @stepto('groups')
3691.8.65 by Carlos Perello Marin
Moved Translation groups pages to top translations.launchpad.net url and use the right page templates for it
135
    def redirect_groups(self):
136
        """Redirect /translations/+groups to Translations root site."""
4346.7.8 by Curtis Hovey
Revision per review. Removed unused imports.
137
        target_url = canonical_url(
3691.8.65 by Carlos Perello Marin
Moved Translation groups pages to top translations.launchpad.net url and use the right page templates for it
138
            getUtility(ILaunchpadRoot), rootsite='translations')
139
        return self.redirectSubTree(
3691.8.96 by Carlos Perello Marin
Changed +translation-groups with +groups as agreed with mpt
140
            target_url + '+groups', status=301)
2622 by Canonical.com Patch Queue Manager
[trivial] remove all uses of browser:suburl and nuke the suburl directive code.
141
2570.1.7 by Carlos Perello Marin
Lots of fixes + new code + tests updates
142
    @stepto('imports')
4268.3.23 by Carlos Perello Marin
Ported old import queue page to use the new infrastructure
143
    def redirect_imports(self):
144
        """Redirect /translations/imports to Translations root site."""
145
        target_url = canonical_url(
146
            getUtility(ILaunchpadRoot), rootsite='translations')
147
        return self.redirectSubTree(
148
            target_url + '+imports', status=301)
2570.1.7 by Carlos Perello Marin
Lots of fixes + new code + tests updates
149
2622 by Canonical.com Patch Queue Manager
[trivial] remove all uses of browser:suburl and nuke the suburl directive code.
150
    @stepto('projects')
151
    def projects(self):
152
        # DEPRECATED
4011.2.7 by Bjorn Tillenius
move /products to /projects
153
        return getUtility(IProductSet)
2622 by Canonical.com Patch Queue Manager
[trivial] remove all uses of browser:suburl and nuke the suburl directive code.
154
155
    @stepto('products')
156
    def products(self):
157
        # DEPRECATED
158
        return getUtility(IProductSet)
4910.1.7 by Danilo Šegan
Apply carlos' +translate/+translations redirects and add language packs migration script.
159
160
161
class PageRedirectView:
162
    """Redirects to translations site for the given page."""
163
164
    def __init__(self, context, request, page):
165
        self.context = context
166
        self.request = request
167
        self.page = page
168
169
    def __call__(self):
170
        """Redirect to self.page in the translations site."""
171
        self.request.response.redirect(
172
            '/'.join([
173
                canonical_url(self.context, rootsite='translations'),
11128.6.18 by Michael Hudson
fix lots of lint
174
                self.page,
4910.1.7 by Danilo Šegan
Apply carlos' +translate/+translations redirects and add language packs migration script.
175
                ]), status=301)
176
177
178
class TranslateRedirectView(PageRedirectView):
179
    """Redirects to translations site for +translate page."""
180
181
    def __init__(self, context, request):
182
        PageRedirectView.__init__(self, context, request, '+translate')
183
184
185
class TranslationsRedirectView(PageRedirectView):
186
    """Redirects to translations site for +translations page."""
187
188
    def __init__(self, context, request):
189
        PageRedirectView.__init__(self, context, request, '+translations')
9360.4.1 by Danilo Šegan
Migrate DistroSeriesLanguage page to 3.0 and introduce breadcrumbs.
190
191
192
class TranslationsVHostBreadcrumb(Breadcrumb):
193
    rootsite = 'translations'
9565.2.1 by Gary Poster
fix vhost breadcrumbs so that we do not duplicate information
194
    text = 'Translations'
9360.4.1 by Danilo Šegan
Migrate DistroSeriesLanguage page to 3.0 and introduce breadcrumbs.
195
196
197
class TranslationsLanguageBreadcrumb(Breadcrumb):
198
    """Breadcrumb for objects with language."""
11128.6.18 by Michael Hudson
fix lots of lint
199
9360.4.1 by Danilo Šegan
Migrate DistroSeriesLanguage page to 3.0 and introduce breadcrumbs.
200
    @property
201
    def text(self):
202
        return self.context.language.displayname