~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
# Copyright 2009-2012 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Translations browser views for distributions."""

__metaclass__ = type

__all__ = [
    'DistributionLanguagePackAdminView',
    'DistributionSettingsView',
    'DistributionView',
    ]

import operator

from lp.app.browser.launchpadform import (
    action,
    LaunchpadEditFormView,
    )
from lp.app.enums import service_uses_launchpad
from lp.registry.browser import RegistryEditFormView
from lp.registry.interfaces.distribution import IDistribution
from lp.registry.interfaces.series import SeriesStatus
from lp.services.propertycache import cachedproperty
from lp.services.webapp import (
    canonical_url,
    enabled_with_permission,
    Link,
    )
from lp.services.webapp.authorization import check_permission
from lp.services.webapp.menu import NavigationMenu
from lp.services.webapp.publisher import LaunchpadView
from lp.translations.browser.translations import TranslationsMixin


class DistributionTranslationsMenu(NavigationMenu):

    usedfor = IDistribution
    facet = 'translations'
    links = ['overview', 'settings', 'language_pack_admin', 'imports']

    def overview(self):
        text = 'Overview'
        link = canonical_url(self.context, rootsite='translations')
        return Link(link, text)

    @enabled_with_permission('launchpad.TranslationsAdmin')
    def settings(self):
        text = 'Configure translations'
        return Link('+settings', text, icon='edit', site='translations')

    @enabled_with_permission('launchpad.TranslationsAdmin')
    def language_pack_admin(self):
        text = 'Language pack admin'
        return Link(
            '+select-language-pack-admin', text, icon='edit',
            site='translations')

    def imports(self):
        text = 'Import queue'
        return Link('+imports', text, site='translations')


class DistributionLanguagePackAdminView(LaunchpadEditFormView):
    """Browser view to change the language pack administrator."""

    schema = IDistribution
    label = "Select the language pack administrator"
    field_names = ['language_pack_admin']

    @property
    def cancel_url(self):
        return canonical_url(self.context, rootsite="translations")

    next_url = cancel_url

    @property
    def page_title(self):
        return 'Change the %s language pack administrator' % (
            self.context.displayname)

    @action("Change", name='change')
    def change_action(self, action, data):
        self.updateContextFromData(data)


class DistributionView(LaunchpadView):
    """Default Distribution view class."""

    label = "Translations overview"

    @cachedproperty
    def translation_focus(self):
        """Return the IDistroSeries where the translators should work.

        If ther isn't a defined focus, we return latest series.
        """
        if self.context.translation_focus is None:
            return self.context.currentseries
        else:
            return self.context.translation_focus

    @cachedproperty
    def show_page_content(self):
        """Whether the main content of the page should be shown."""
        return (service_uses_launchpad(self.context.translations_usage) or
               self.is_translations_admin)

    def can_configure_translations(self):
        """Whether or not the user can configure translations."""
        return check_permission("launchpad.TranslationsAdmin", self.context)

    def is_translations_admin(self):
        """Whether or not the user is a translations admin."""
        return check_permission("launchpad.TranslationsAdmin", self.context)

    def secondary_translatable_series(self):
        """Return a list of IDistroSeries that aren't the translation_focus.

        It only includes the ones that are still supported.
        """
        series = [
            series
            for series in self.context.series
            if (series.status != SeriesStatus.OBSOLETE
                and (self.translation_focus is None or
                     self.translation_focus.id != series.id))]

        return sorted(series, key=operator.attrgetter('version'),
                      reverse=True)


class DistributionSettingsView(TranslationsMixin, RegistryEditFormView):
    label = "Translations settings"
    page_title = "Settings"
    schema = IDistribution

    field_names = [
        "translations_usage",
        "translation_focus",
        "translationgroup",
        "translationpermission",
        ]

    @property
    def cancel_url(self):
        return canonical_url(self.context, rootsite="translations")

    next_url = cancel_url

    @action('Change', name='change')
    def edit(self, action, data):
        self.updateContextFromData(data)