~launchpad-pqm/launchpad/devel

8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
2068 by Canonical.com Patch Queue Manager
[r=SteveA/trivial] import fascism, kill translationeffort, kill sshkey browser code, add __all__s to interfaces, other cleanings-up
3
4
"""View classes to handle signed Codes of Conduct."""
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
5
6
__metaclass__ = type
7
2071 by Canonical.com Patch Queue Manager
[trivial] add more __all__ statements, other minor tidyings-up
8
__all__ = [
2628 by Canonical.com Patch Queue Manager
[trivial] converted a bunch of browser:traverse into navigation
9
    'SignedCodeOfConductSetNavigation',
10
    'CodeOfConductSetNavigation',
9140.3.1 by Guilherme Salgado
Convert a couple remaining CoC pages to 3.0
11
    'CodeOfConductOverviewMenu',
12
    'CodeOfConductSetOverviewMenu',
9122.2.1 by Guilherme Salgado
Convert a bunch of signed-coc pages to 3.0
13
    'SignedCodeOfConductSetOverviewMenu',
14
    'SignedCodeOfConductOverviewMenu',
2071 by Canonical.com Patch Queue Manager
[trivial] add more __all__ statements, other minor tidyings-up
15
    'CodeOfConductView',
16
    'CodeOfConductDownloadView',
17
    'CodeOfConductSetView',
18
    'SignedCodeOfConductAddView',
19
    'SignedCodeOfConductAckView',
20
    'SignedCodeOfConductView',
21
    'SignedCodeOfConductAdminView',
22
    'SignedCodeOfConductActiveView',
23
    'SignedCodeOfConductDeactiveView',
24
    ]
25
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
26
from zope.component import getUtility
27
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
28
from lp.services.webapp import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
29
    ApplicationMenu,
30
    canonical_url,
31
    enabled_with_permission,
32
    GetitemNavigation,
33
    LaunchpadView,
34
    Link,
35
    )
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
36
from lp.services.webapp.interfaces import ILaunchBag
11929.9.1 by Tim Penhey
Move launchpadform into lp.app.browser.
37
from lp.app.browser.launchpadform import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
38
    action,
39
    LaunchpadFormView,
40
    )
7675.110.3 by Curtis Hovey
Ran the migration script to move registry code to lp.registry.
41
from lp.registry.interfaces.codeofconduct import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
42
    ICodeOfConduct,
43
    ICodeOfConductConf,
44
    ICodeOfConductSet,
45
    ISignedCodeOfConduct,
46
    ISignedCodeOfConductSet,
47
    )
11252.2.2 by Michael Hudson
VWS fixups
48
49
2630 by Canonical.com Patch Queue Manager
[trivial] lots of tidying up. converting all database classes to use NotFoundError consistently, and to import it from launchpad.interfaces in preparation for the move to a new zope3. Also, introduced a NameNotAvailable error. removed browser:traverse rdirective. commented out shipit test that fails sometimes.
50
class SignedCodeOfConductSetNavigation(GetitemNavigation):
2628 by Canonical.com Patch Queue Manager
[trivial] converted a bunch of browser:traverse into navigation
51
52
    usedfor = ISignedCodeOfConductSet
53
2630 by Canonical.com Patch Queue Manager
[trivial] lots of tidying up. converting all database classes to use NotFoundError consistently, and to import it from launchpad.interfaces in preparation for the move to a new zope3. Also, introduced a NameNotAvailable error. removed browser:traverse rdirective. commented out shipit test that fails sometimes.
54
55
class CodeOfConductSetNavigation(GetitemNavigation):
2628 by Canonical.com Patch Queue Manager
[trivial] converted a bunch of browser:traverse into navigation
56
57
    usedfor = ICodeOfConductSet
58
59
9140.3.1 by Guilherme Salgado
Convert a couple remaining CoC pages to 3.0
60
class CodeOfConductOverviewMenu(ApplicationMenu):
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
61
62
    usedfor = ICodeOfConduct
9140.3.1 by Guilherme Salgado
Convert a couple remaining CoC pages to 3.0
63
    facet = 'overview'
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
64
    links = ['sign', 'download']
65
66
    def sign(self):
9140.3.1 by Guilherme Salgado
Convert a couple remaining CoC pages to 3.0
67
        text = 'Sign it'
8926.2.1 by Barry Warsaw
Remove all occurrences of the term "Ubuntero" from the Launchpad site and
68
        if (self.context.current and
69
            self.user and
8926.2.2 by Barry Warsaw
signee -> signer
70
            not self.user.is_ubuntu_coc_signer):
8926.2.1 by Barry Warsaw
Remove all occurrences of the term "Ubuntero" from the Launchpad site and
71
            # Then...
3504.1.60 by kiko
Attempt to better describe what Ubuntero is, and how you get register as one. Improve the display of information in the Person details portlet, clarifying it for people who have not yet activated accounts in Launchpad. Don't offer the +sign CoC link if the person has already signed the CoC. Include a way of easily grabbing the current code of conduct. Modify xx-person-home to be testbrowser compliant. And, as a bonus, fix #54818:
72
            enabled = True
73
        else:
74
            enabled = False
75
        return Link('+sign', text, enabled=enabled, icon='edit')
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
76
77
    def download(self):
3847.2.15 by Mark Shuttleworth
Consistency in menu text and capitalisation
78
        text = 'Download this version'
7362.9.16 by Jonathan Lange
Merge lower thread
79
        is_current = self.context.current
3564.6.1 by Diogo Matsubara
fix bug 39547 (Code of Conduct 1.0.1 signatures not accepted)
80
        return Link('+download', text, enabled=is_current, icon='download')
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
81
82
9140.3.1 by Guilherme Salgado
Convert a couple remaining CoC pages to 3.0
83
class CodeOfConductSetOverviewMenu(ApplicationMenu):
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
84
85
    usedfor = ICodeOfConductSet
9140.3.1 by Guilherme Salgado
Convert a couple remaining CoC pages to 3.0
86
    facet = 'overview'
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
87
    links = ['admin']
88
89
    @enabled_with_permission('launchpad.Admin')
90
    def admin(self):
3847.2.20 by Mark Shuttleworth
Fix FOAF tests related to menu capitalisation.
91
        text = 'Administration console'
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
92
        return Link('console', text, icon='edit')
93
94
9122.2.1 by Guilherme Salgado
Convert a bunch of signed-coc pages to 3.0
95
class SignedCodeOfConductSetOverviewMenu(ApplicationMenu):
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
96
97
    usedfor = ISignedCodeOfConductSet
9122.2.1 by Guilherme Salgado
Convert a bunch of signed-coc pages to 3.0
98
    facet = 'overview'
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
99
    links = ['register']
100
101
    def register(self):
2900.2.6 by Matthew Paul Thomas
fix menu item capitalization (and HTML error in front page)
102
        text = "Register Someone's Signature"
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
103
        return Link('+new', text, icon='add')
104
105
9122.2.1 by Guilherme Salgado
Convert a bunch of signed-coc pages to 3.0
106
class SignedCodeOfConductOverviewMenu(ApplicationMenu):
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
107
108
    usedfor = ISignedCodeOfConduct
9122.2.1 by Guilherme Salgado
Convert a bunch of signed-coc pages to 3.0
109
    facet = 'overview'
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
110
    links = ['activation', 'adminconsole']
111
112
    def activation(self):
113
        if self.context.active:
9122.2.1 by Guilherme Salgado
Convert a bunch of signed-coc pages to 3.0
114
            text = 'deactivate'
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
115
            return Link('+deactivate', text, icon='edit')
116
        else:
9122.2.1 by Guilherme Salgado
Convert a bunch of signed-coc pages to 3.0
117
            text = 'activate'
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
118
            return Link('+activate', text, icon='edit')
119
120
    def adminconsole(self):
3847.2.20 by Mark Shuttleworth
Fix FOAF tests related to menu capitalisation.
121
        text = 'Administration console'
2548 by Canonical.com Patch Queue Manager
r=bjornt, plus some [trivial], lots of menus work, special error page for request timeouts.
122
        return Link('../', text, icon='info')
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
123
124
9447.2.2 by Barry Warsaw
Convert ~person/codesofconduct pages to UI 3.0
125
class CodeOfConductView(LaunchpadView):
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
126
    """Simple view class for CoC page."""
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
127
9447.2.2 by Barry Warsaw
Convert ~person/codesofconduct pages to UI 3.0
128
    @property
129
    def page_title(self):
130
        """See `LaunchpadView`."""
131
        # This page has no breadcrumbs, nor should it.
132
        return self.context.title
9140.3.1 by Guilherme Salgado
Convert a couple remaining CoC pages to 3.0
133
1398 by Canonical.com Patch Queue Manager
Page for Dowload CoC Release as text file.
134
3564.6.1 by Diogo Matsubara
fix bug 39547 (Code of Conduct 1.0.1 signatures not accepted)
135
class CodeOfConductDownloadView:
1412 by Canonical.com Patch Queue Manager
CoC partially reviewed.
136
    """Download view class for CoC page.
137
138
    This view does not use a template, but uses a __call__ method
139
    that returns a file to the browser.
1398 by Canonical.com Patch Queue Manager
Page for Dowload CoC Release as text file.
140
    """
141
142
    def __init__(self, context, request):
143
        self.context = context
144
        self.request = request
145
146
    def __call__(self):
1412 by Canonical.com Patch Queue Manager
CoC partially reviewed.
147
        """Set response headers to download an attachment, and return
148
        CoC file data.
149
        """
150
        # Use the context attribute 'content' as data to return.
151
        # Avoid open the CoC file again.
1398 by Canonical.com Patch Queue Manager
Page for Dowload CoC Release as text file.
152
        content = self.context.content
153
1412 by Canonical.com Patch Queue Manager
CoC partially reviewed.
154
        # Build a fancy filename:
155
        # - Use title with no spaces and append '.txt'
1398 by Canonical.com Patch Queue Manager
Page for Dowload CoC Release as text file.
156
        filename = self.context.title.replace(' ', '') + '.txt'
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
157
1398 by Canonical.com Patch Queue Manager
Page for Dowload CoC Release as text file.
158
        self.request.response.setHeader('Content-Type', 'application/text')
159
        self.request.response.setHeader('Content-Length', len(content))
7362.9.16 by Jonathan Lange
Merge lower thread
160
        self.request.response.setHeader(
161
            'Content-Disposition', 'attachment; filename="%s"' % filename)
1398 by Canonical.com Patch Queue Manager
Page for Dowload CoC Release as text file.
162
        return content
163
164
9140.3.1 by Guilherme Salgado
Convert a couple remaining CoC pages to 3.0
165
class CodeOfConductSetView(LaunchpadView):
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
166
    """Simple view class for CoCSet page."""
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
167
13980.3.23 by Curtis Hovey
Moved remaning coc page_titles into views.
168
    page_title = 'Ubuntu Codes of Conduct'
169
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
170
7362.9.16 by Jonathan Lange
Merge lower thread
171
class SignedCodeOfConductAddView(LaunchpadFormView):
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
172
    """Add a new SignedCodeOfConduct Entry."""
7362.9.16 by Jonathan Lange
Merge lower thread
173
    schema = ISignedCodeOfConduct
174
    field_names = ['signedcode']
175
13980.3.7 by Curtis Hovey
Removed obsolete coc page_titles and moved the used into their views.
176
    @property
177
    def page_title(self):
178
        return 'Sign %s' % self.context.title
179
7362.9.16 by Jonathan Lange
Merge lower thread
180
    @action('Continue', name='continue')
181
    def continue_action(self, action, data):
182
        signedcode = data["signedcode"]
3564.6.1 by Diogo Matsubara
fix bug 39547 (Code of Conduct 1.0.1 signatures not accepted)
183
        signedcocset = getUtility(ISignedCodeOfConductSet)
184
        error_message = signedcocset.verifyAndStore(self.user, signedcode)
7362.9.16 by Jonathan Lange
Merge lower thread
185
        # It'd be nice to do this validation before, but the method which does
186
        # the validation is also the one that stores the signed CoC, so we
187
        # need to do everything here.
3564.6.1 by Diogo Matsubara
fix bug 39547 (Code of Conduct 1.0.1 signatures not accepted)
188
        if error_message:
7362.9.16 by Jonathan Lange
Merge lower thread
189
            self.addError(error_message)
190
            return
191
        self.next_url = canonical_url(self.user) + '/+codesofconduct'
3564.6.1 by Diogo Matsubara
fix bug 39547 (Code of Conduct 1.0.1 signatures not accepted)
192
193
    @property
7362.9.16 by Jonathan Lange
Merge lower thread
194
    def current(self):
3564.6.1 by Diogo Matsubara
fix bug 39547 (Code of Conduct 1.0.1 signatures not accepted)
195
        """Return the current release of the Code of Conduct."""
196
        coc_conf = getUtility(ICodeOfConductConf)
197
        coc_set = getUtility(ICodeOfConductSet)
198
        return coc_set[coc_conf.currentrelease]
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
199
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
200
11694.1.5 by Curtis Hovey
Updated SignedCodeOfConductAckView to be a LaunchpadFormView.
201
class SignedCodeOfConductAckView(LaunchpadFormView):
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
202
    """Acknowledge a Paper Submitted CoC."""
11694.1.5 by Curtis Hovey
Updated SignedCodeOfConductAckView to be a LaunchpadFormView.
203
    schema = ISignedCodeOfConduct
204
    field_names = ['owner']
205
    label = 'Register a code of conduct signature'
206
    page_title = label
207
208
    @property
209
    def next_url(self):
210
        return canonical_url(self.context)
211
212
    cancel_url = next_url
213
214
    @action('Register', name='add')
215
    def createAndAdd(self, action, data):
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
216
        """Verify and Add the Acknowledge SignedCoC entry."""
11694.1.5 by Curtis Hovey
Updated SignedCodeOfConductAckView to be a LaunchpadFormView.
217
        self.context.acknowledgeSignature(
218
            user=data['owner'], recipient=self.user)
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
219
220
13980.3.7 by Curtis Hovey
Removed obsolete coc page_titles and moved the used into their views.
221
class SignedCodeOfConductView(CodeOfConductView):
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
222
    """Simple view class for SignedCoC page."""
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
223
224
13303.11.14 by Aaron Bentley
Fix failing test.
225
class SignedCodeOfConductAdminView(LaunchpadView):
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
226
    """Admin Console for SignedCodeOfConduct Entries."""
227
13980.3.23 by Curtis Hovey
Moved remaning coc page_titles into views.
228
    page_title = 'Administer Codes of Conduct'
229
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
230
    def __init__(self, context, request):
231
        self.context = context
232
        self.request = request
233
        self.bag = getUtility(ILaunchBag)
234
        self.results = None
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
235
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
236
    def search(self):
237
        """Search Signed CoC by Owner Displayname"""
238
        name = self.request.form.get('name')
239
        searchfor = self.request.form.get('searchfor')
240
2557 by Canonical.com Patch Queue Manager
[r=salgado] Fix CoC admin console search, empty 'From' returns every results, pending batching for that (bug # 2609)
241
        if (self.request.method != "POST" or
242
            self.request.form.get("search") != "Search"):
243
            return
1389 by Canonical.com Patch Queue Manager
CodeOfConduct spec, almost done, missed pagetests.
244
2557 by Canonical.com Patch Queue Manager
[r=salgado] Fix CoC admin console search, empty 'From' returns every results, pending batching for that (bug # 2609)
245
        # use utility to query on SignedCoCs
246
        sCoC_util = getUtility(ISignedCodeOfConductSet)
247
        self.results = sCoC_util.searchByDisplayname(name,
248
                                                     searchfor=searchfor)
4785.3.6 by Jeroen Vermeulen
Removed whitespace from ends of lines.
249
2557 by Canonical.com Patch Queue Manager
[r=salgado] Fix CoC admin console search, empty 'From' returns every results, pending batching for that (bug # 2609)
250
        return True
1372 by Canonical.com Patch Queue Manager
SignedCoC Admin Console Prototype
251
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
252
11694.1.2 by Curtis Hovey
Updated SignedCodeOfConduct views to be LaunchpadFormViews.
253
class SignedCodeOfConductActiveView(LaunchpadFormView):
254
    """Active a SignedCodeOfConduct Entry."""
255
    schema = ISignedCodeOfConduct
256
    field_names = ['admincomment']
257
    label = 'Activate code of conduct signature'
258
    page_title = label
259
    state = True
260
261
    @property
262
    def next_url(self):
263
        return canonical_url(self.context)
264
265
    cancel_url = next_url
266
267
    def _change(self, action, data):
268
        admincomment = data['admincomment']
269
        sCoC_util = getUtility(ISignedCodeOfConductSet)
270
        sCoC_util.modifySignature(
271
            sign_id=self.context.id, recipient=self.user,
272
            admincomment=admincomment, state=self.state)
273
        self.request.response.redirect(self.next_url)
274
275
    @action('Activate', name='change')
276
    def activate(self, action, data):
277
        self._change(action, data)
278
279
280
class SignedCodeOfConductDeactiveView(SignedCodeOfConductActiveView):
11694.1.3 by Curtis Hovey
Removed the last of the EditViews.
281
    """Deactivate a SignedCodeOfConduct Entry."""
11694.1.2 by Curtis Hovey
Updated SignedCodeOfConduct views to be LaunchpadFormViews.
282
    label = 'Deactivate code of conduct signature'
283
    page_title = label
284
    state = False
285
286
    @action('Deactivate', name='change')
287
    def deactivate(self, action, data):
288
        self._change(action, data)