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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Announcement views."""
__metaclass__ = type
__all__ = [
'AnnouncementAddView',
'AnnouncementRetargetView',
'AnnouncementPublishView',
'AnnouncementRetractView',
'AnnouncementDeleteView',
'AnnouncementEditView',
'AnnouncementSetView',
'HasAnnouncementsView',
'AnnouncementView',
]
from zope.interface import (
implements,
Interface,
)
from zope.schema import (
Choice,
TextLine,
)
from canonical.config import config
from canonical.launchpad import _
from canonical.launchpad.browser.feeds import (
AnnouncementsFeedLink,
FeedsMixin,
RootAnnouncementsFeedLink,
)
from canonical.launchpad.webapp.authorization import check_permission
from canonical.launchpad.webapp.batching import BatchNavigator
from canonical.launchpad.webapp.menu import (
enabled_with_permission,
Link,
NavigationMenu,
)
from canonical.launchpad.webapp.publisher import (
canonical_url,
LaunchpadView,
)
from lp.app.browser.launchpadform import (
action,
custom_widget,
LaunchpadFormView,
)
from lp.app.validators.url import valid_webref
from lp.app.widgets.announcementdate import AnnouncementDateWidget
from lp.registry.interfaces.announcement import IAnnouncement
from lp.services.fields import (
AnnouncementDate,
Summary,
Title,
)
from lp.services.propertycache import cachedproperty
class AnnouncementMenuMixin:
"""A mixin of links common to many menus."""
@enabled_with_permission('launchpad.Edit')
def edit(self):
text = 'Modify announcement'
return Link('+edit', text, icon='edit')
@enabled_with_permission('launchpad.Edit')
def retarget(self):
text = 'Move announcement'
return Link('+retarget', text, icon='edit')
@enabled_with_permission('launchpad.Edit')
def publish(self):
text = 'Publish announcement'
enabled = not self.context.published
return Link('+publish', text, icon='edit', enabled=enabled)
@enabled_with_permission('launchpad.Edit')
def retract(self):
text = 'Retract announcement'
enabled = self.context.published
return Link('+retract', text, icon='remove', enabled=enabled)
@enabled_with_permission('launchpad.Edit')
def delete(self):
text = 'Delete announcement'
return Link('+delete', text, icon='trash-icon')
@enabled_with_permission('launchpad.Edit')
def announce(self):
text = 'Make announcement'
summary = 'Create an item of news for this project'
return Link('+announce', text, summary, icon='add')
class AnnouncementEditNavigationMenu(NavigationMenu, AnnouncementMenuMixin):
"""A sub-menu for different aspects of modifying an announcement."""
usedfor = IAnnouncement
facet = 'overview'
title = 'Change announcement'
links = ['edit', 'retarget', 'publish', 'retract', 'delete']
class IAnnouncementCreateMenu(Interface):
"""A marker interface for creation announcement navigation menu."""
class AnnouncementCreateNavigationMenu(NavigationMenu, AnnouncementMenuMixin):
"""A sub-menu for different aspects of modifying an announcement."""
usedfor = IAnnouncementCreateMenu
facet = 'overview'
title = 'Create announcement'
links = ['announce']
class AnnouncementFormMixin:
"""A mixin to provide the common form features."""
@property
def label(self):
return self.context.title
@property
def cancel_url(self):
"""The announcements URL."""
return canonical_url(self.context.target, view_name='+announcements')
class AddAnnouncementForm(Interface):
"""Form definition for the view which creates new Announcements."""
title = Title(title=_('Headline'), required=True)
summary = Summary(title=_('Summary'), required=True)
url = TextLine(title=_('URL'), required=False, constraint=valid_webref,
description=_("The web location of your announcement."))
publication_date = AnnouncementDate(title=_('Date'), required=True)
class AnnouncementAddView(LaunchpadFormView):
"""A view for creating a new Announcement."""
schema = AddAnnouncementForm
label = "Make an announcement"
page_title = label
custom_widget('publication_date', AnnouncementDateWidget)
@action(_('Make announcement'), name='announce')
def announce_action(self, action, data):
"""Registers a new announcement."""
self.context.announce(
user = self.user,
title = data.get('title'),
summary = data.get('summary'),
url = data.get('url'),
publication_date = data.get('publication_date')
)
self.next_url = canonical_url(self.context)
@property
def action_url(self):
return "%s/+announce" % canonical_url(self.context)
@property
def cancel_url(self):
"""The project's URL."""
return canonical_url(self.context)
class AnnouncementEditView(AnnouncementFormMixin, LaunchpadFormView):
"""A view which allows you to edit the announcement."""
schema = AddAnnouncementForm
field_names = ['title', 'summary', 'url', ]
page_title = 'Modify announcement'
@property
def initial_values(self):
return {
'title': self.context.title,
'summary': self.context.summary,
'url': self.context.url,
}
@action(_('Modify'), name='modify')
def modify_action(self, action, data):
self.context.modify(title=data.get('title'),
summary=data.get('summary'),
url=data.get('url'))
self.next_url = canonical_url(self.context.target)+'/+announcements'
class AnnouncementRetargetForm(Interface):
"""Form that requires the user to choose a pillar for the Announcement."""
target = Choice(
title=_("For"),
description=_("The project where this announcement is being made."),
required=True, vocabulary='DistributionOrProductOrProjectGroup')
class AnnouncementRetargetView(AnnouncementFormMixin, LaunchpadFormView):
"""A view to move an annoucement to another project."""
schema = AnnouncementRetargetForm
field_names = ['target']
page_title = 'Move announcement'
def validate(self, data):
"""Ensure that the person can publish announcement at the new
target.
"""
target = data.get('target')
if target is None:
self.setFieldError('target',
"There is no project with the name '%s'. "
"Please check that name and try again." %
self.request.form.get("field.target"))
return
if not check_permission('launchpad.Edit', target):
self.setFieldError('target',
"You don't have permission to make announcements for "
"%s. Please check that name and try again." %
target.displayname)
return
@action(_('Retarget'), name='retarget')
def retarget_action(self, action, data):
target = data.get('target')
self.context.retarget(target)
self.next_url = canonical_url(self.context.target)+'/+announcements'
class AnnouncementPublishView(AnnouncementFormMixin, LaunchpadFormView):
"""A view to publish an annoucement."""
schema = AddAnnouncementForm
field_names = ['publication_date']
page_title = 'Publish announcement'
custom_widget('publication_date', AnnouncementDateWidget)
@action(_('Publish'), name='publish')
def publish_action(self, action, data):
publication_date = data['publication_date']
self.context.setPublicationDate(publication_date)
self.next_url = canonical_url(self.context.target)+'/+announcements'
class AnnouncementRetractView(AnnouncementFormMixin, LaunchpadFormView):
"""A view to unpublish an announcement."""
schema = IAnnouncement
page_title = 'Retract announcement'
@action(_('Retract'), name='retract')
def retract_action(self, action, data):
self.context.retract()
self.next_url = canonical_url(self.context.target)+'/+announcements'
class AnnouncementDeleteView(AnnouncementFormMixin, LaunchpadFormView):
"""A view to delete an annoucement."""
schema = IAnnouncement
page_title = 'Delete announcement'
@action(_("Delete"), name="delete", validator='validate_cancel')
def action_delete(self, action, data):
self.context.destroySelf()
self.next_url = canonical_url(self.context.target)+'/+announcements'
class HasAnnouncementsView(LaunchpadView, FeedsMixin):
"""A view class for pillars which have announcements."""
implements(IAnnouncementCreateMenu)
batch_size = config.launchpad.announcement_batch_size
@cachedproperty
def feed_url(self):
if AnnouncementsFeedLink.usedfor.providedBy(self.context):
return AnnouncementsFeedLink(self.context).href
elif RootAnnouncementsFeedLink.usedfor.providedBy(self.context):
return RootAnnouncementsFeedLink(self.context).href
else:
raise AssertionError, 'Unknown feed source'
@cachedproperty
def announcements(self):
published_only = not check_permission('launchpad.Edit', self.context)
return self.context.getAnnouncements(
limit=None, published_only=published_only)
@cachedproperty
def latest_announcements(self):
published_only = not check_permission('launchpad.Edit', self.context)
return self.context.getAnnouncements(
limit=5, published_only=published_only)
@cachedproperty
def show_announcements(self):
return (self.latest_announcements.count() > 0
or check_permission('launchpad.Edit', self.context))
@cachedproperty
def announcement_nav(self):
return BatchNavigator(
self.announcements, self.request,
size=self.batch_size)
class AnnouncementSetView(HasAnnouncementsView):
"""View a list of announcements.
All other feed links should be disabled on this page by
overriding the feed_types class variable.
"""
feed_types = (
AnnouncementsFeedLink,
RootAnnouncementsFeedLink,
)
page_title = 'Announcements from all projects hosted in Launchpad'
label = page_title
class AnnouncementView(LaunchpadView):
"""A view class for a single announcement."""
@property
def label(self):
return self.context.title
|