~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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""View classes to handle signed Codes of Conduct."""

__metaclass__ = type

__all__ = [
    'SignedCodeOfConductSetNavigation',
    'CodeOfConductSetNavigation',
    'CodeOfConductOverviewMenu',
    'CodeOfConductSetOverviewMenu',
    'SignedCodeOfConductSetOverviewMenu',
    'SignedCodeOfConductOverviewMenu',
    'CodeOfConductView',
    'CodeOfConductDownloadView',
    'CodeOfConductSetView',
    'SignedCodeOfConductAddView',
    'SignedCodeOfConductAckView',
    'SignedCodeOfConductView',
    'SignedCodeOfConductAdminView',
    'SignedCodeOfConductActiveView',
    'SignedCodeOfConductDeactiveView',
    ]

from zope.component import getUtility

from lp.services.webapp import (
    ApplicationMenu,
    canonical_url,
    enabled_with_permission,
    GetitemNavigation,
    LaunchpadView,
    Link,
    )
from lp.services.webapp.interfaces import ILaunchBag
from lp.app.browser.launchpadform import (
    action,
    LaunchpadFormView,
    )
from lp.registry.interfaces.codeofconduct import (
    ICodeOfConduct,
    ICodeOfConductConf,
    ICodeOfConductSet,
    ISignedCodeOfConduct,
    ISignedCodeOfConductSet,
    )


class SignedCodeOfConductSetNavigation(GetitemNavigation):

    usedfor = ISignedCodeOfConductSet


class CodeOfConductSetNavigation(GetitemNavigation):

    usedfor = ICodeOfConductSet


class CodeOfConductOverviewMenu(ApplicationMenu):

    usedfor = ICodeOfConduct
    facet = 'overview'
    links = ['sign', 'download']

    def sign(self):
        text = 'Sign it'
        if (self.context.current and
            self.user and
            not self.user.is_ubuntu_coc_signer):
            # Then...
            enabled = True
        else:
            enabled = False
        return Link('+sign', text, enabled=enabled, icon='edit')

    def download(self):
        text = 'Download this version'
        is_current = self.context.current
        return Link('+download', text, enabled=is_current, icon='download')


class CodeOfConductSetOverviewMenu(ApplicationMenu):

    usedfor = ICodeOfConductSet
    facet = 'overview'
    links = ['admin']

    @enabled_with_permission('launchpad.Admin')
    def admin(self):
        text = 'Administration console'
        return Link('console', text, icon='edit')


class SignedCodeOfConductSetOverviewMenu(ApplicationMenu):

    usedfor = ISignedCodeOfConductSet
    facet = 'overview'
    links = ['register']

    def register(self):
        text = "Register Someone's Signature"
        return Link('+new', text, icon='add')


class SignedCodeOfConductOverviewMenu(ApplicationMenu):

    usedfor = ISignedCodeOfConduct
    facet = 'overview'
    links = ['activation', 'adminconsole']

    def activation(self):
        if self.context.active:
            text = 'deactivate'
            return Link('+deactivate', text, icon='edit')
        else:
            text = 'activate'
            return Link('+activate', text, icon='edit')

    def adminconsole(self):
        text = 'Administration console'
        return Link('../', text, icon='info')


class CodeOfConductView(LaunchpadView):
    """Simple view class for CoC page."""

    @property
    def page_title(self):
        """See `LaunchpadView`."""
        # This page has no breadcrumbs, nor should it.
        return self.context.title


class CodeOfConductDownloadView:
    """Download view class for CoC page.

    This view does not use a template, but uses a __call__ method
    that returns a file to the browser.
    """

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

    def __call__(self):
        """Set response headers to download an attachment, and return
        CoC file data.
        """
        # Use the context attribute 'content' as data to return.
        # Avoid open the CoC file again.
        content = self.context.content

        # Build a fancy filename:
        # - Use title with no spaces and append '.txt'
        filename = self.context.title.replace(' ', '') + '.txt'

        self.request.response.setHeader('Content-Type', 'application/text')
        self.request.response.setHeader('Content-Length', len(content))
        self.request.response.setHeader(
            'Content-Disposition', 'attachment; filename="%s"' % filename)
        return content


class CodeOfConductSetView(LaunchpadView):
    """Simple view class for CoCSet page."""

    page_title = 'Ubuntu Codes of Conduct'


class SignedCodeOfConductAddView(LaunchpadFormView):
    """Add a new SignedCodeOfConduct Entry."""
    schema = ISignedCodeOfConduct
    field_names = ['signedcode']

    @property
    def page_title(self):
        return 'Sign %s' % self.context.title

    @action('Continue', name='continue')
    def continue_action(self, action, data):
        signedcode = data["signedcode"]
        signedcocset = getUtility(ISignedCodeOfConductSet)
        error_message = signedcocset.verifyAndStore(self.user, signedcode)
        # It'd be nice to do this validation before, but the method which does
        # the validation is also the one that stores the signed CoC, so we
        # need to do everything here.
        if error_message:
            self.addError(error_message)
            return
        self.next_url = canonical_url(self.user) + '/+codesofconduct'

    @property
    def current(self):
        """Return the current release of the Code of Conduct."""
        coc_conf = getUtility(ICodeOfConductConf)
        coc_set = getUtility(ICodeOfConductSet)
        return coc_set[coc_conf.currentrelease]


class SignedCodeOfConductAckView(LaunchpadFormView):
    """Acknowledge a Paper Submitted CoC."""
    schema = ISignedCodeOfConduct
    field_names = ['owner']
    label = 'Register a code of conduct signature'
    page_title = label

    @property
    def next_url(self):
        return canonical_url(self.context)

    cancel_url = next_url

    @action('Register', name='add')
    def createAndAdd(self, action, data):
        """Verify and Add the Acknowledge SignedCoC entry."""
        self.context.acknowledgeSignature(
            user=data['owner'], recipient=self.user)


class SignedCodeOfConductView(CodeOfConductView):
    """Simple view class for SignedCoC page."""


class SignedCodeOfConductAdminView(LaunchpadView):
    """Admin Console for SignedCodeOfConduct Entries."""

    page_title = 'Administer Codes of Conduct'

    def __init__(self, context, request):
        self.context = context
        self.request = request
        self.bag = getUtility(ILaunchBag)
        self.results = None

    def search(self):
        """Search Signed CoC by Owner Displayname"""
        name = self.request.form.get('name')
        searchfor = self.request.form.get('searchfor')

        if (self.request.method != "POST" or
            self.request.form.get("search") != "Search"):
            return

        # use utility to query on SignedCoCs
        sCoC_util = getUtility(ISignedCodeOfConductSet)
        self.results = sCoC_util.searchByDisplayname(name,
                                                     searchfor=searchfor)

        return True


class SignedCodeOfConductActiveView(LaunchpadFormView):
    """Active a SignedCodeOfConduct Entry."""
    schema = ISignedCodeOfConduct
    field_names = ['admincomment']
    label = 'Activate code of conduct signature'
    page_title = label
    state = True

    @property
    def next_url(self):
        return canonical_url(self.context)

    cancel_url = next_url

    def _change(self, action, data):
        admincomment = data['admincomment']
        sCoC_util = getUtility(ISignedCodeOfConductSet)
        sCoC_util.modifySignature(
            sign_id=self.context.id, recipient=self.user,
            admincomment=admincomment, state=self.state)
        self.request.response.redirect(self.next_url)

    @action('Activate', name='change')
    def activate(self, action, data):
        self._change(action, data)


class SignedCodeOfConductDeactiveView(SignedCodeOfConductActiveView):
    """Deactivate a SignedCodeOfConduct Entry."""
    label = 'Deactivate code of conduct signature'
    page_title = label
    state = False

    @action('Deactivate', name='change')
    def deactivate(self, action, data):
        self._change(action, data)