1
# Copyright 2009-2010 Canonical Ltd. This software is licensed under the
1
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
4
"""Views which export vocabularies as JSON for widgets."""
11
11
'get_person_picker_entry_metadata',
15
14
from itertools import izip
17
16
from lazr.restful.interfaces import IWebServiceClientRequest
18
18
from zope.app.form.interfaces import MissingInputError
19
19
from zope.app.schema.vocabulary import IVocabularyFactory
20
20
from zope.component import (
32
32
from canonical.launchpad.webapp.batching import BatchNavigator
33
33
from canonical.launchpad.webapp.interfaces import NoCanonicalUrl
34
34
from canonical.launchpad.webapp.publisher import canonical_url
35
from canonical.launchpad.webapp.vocabulary import IHugeVocabulary
35
36
from lp.app.browser.tales import (
36
37
DateTimeFormatterAPI,
37
38
IRCNicknameFormatterAPI,
38
39
ObjectImageDisplayAPI,
40
from canonical.launchpad.webapp.vocabulary import IHugeVocabulary
41
41
from lp.app.errors import UnexpectedFormData
42
42
from lp.code.interfaces.branch import IBranch
43
from lp.registry.interfaces.distribution import IDistribution
44
from lp.registry.interfaces.distributionsourcepackage import (
45
IDistributionSourcePackage,
43
47
from lp.registry.interfaces.person import IPerson
48
from lp.registry.interfaces.product import IProduct
44
49
from lp.registry.interfaces.sourcepackagename import ISourcePackageName
45
50
from lp.registry.model.pillaraffiliation import IHasAffiliation
46
51
from lp.registry.model.sourcepackagename import getSourcePackageDescriptions
147
152
personpicker_affiliation_enabled = kwarg.get(
148
153
'personpicker_affiliation_enabled', False)
149
if personpicker_affiliation_enabled:
154
affiliated_context = IHasAffiliation(context_object, None)
155
if (affiliated_context is not None
156
and personpicker_affiliation_enabled):
150
157
# If a person is affiliated with the associated_object then we
151
158
# can display a badge.
152
badges = IHasAffiliation(
153
context_object).getAffiliationBadges(term_values)
159
badges = affiliated_context.getAffiliationBadges(term_values)
154
160
for picker_entry, badges in izip(picker_entries, badges):
155
161
picker_entry.badges = []
156
162
for badge_info in badges:
231
class TargetPickerEntrySourceAdapter(DefaultPickerEntrySourceAdapter):
232
"""Adapt targets (Product, Package, Distribution) to PickerEntrySource."""
234
def getDescription(self, target):
235
"""Gets the description data for target picker entries."""
238
def getPickerEntries(self, term_values, context_object, **kwarg):
239
"""See `IPickerEntrySource`"""
241
super(TargetPickerEntrySourceAdapter, self)
242
.getPickerEntries(term_values, context_object, **kwarg))
243
for target, picker_entry in izip(term_values, entries):
244
picker_entry.description = self.getDescription(target)
225
248
@adapter(ISourcePackageName)
226
249
class SourcePackageNamePickerEntrySourceAdapter(
227
250
DefaultPickerEntrySourceAdapter):
228
251
"""Adapts ISourcePackageName to IPickerEntrySource."""
230
def getPickerEntry(self, term_values, context_object, **kwarg):
253
def getPickerEntries(self, term_values, context_object, **kwarg):
231
254
"""See `IPickerEntrySource`"""
233
256
super(SourcePackageNamePickerEntrySourceAdapter, self)
265
@adapter(IDistributionSourcePackage)
266
class DistributionSourcePackagePickerEntrySourceAdapter(
267
TargetPickerEntrySourceAdapter):
268
"""Adapts IDistributionSourcePackage to IPickerEntrySource."""
270
def getDescription(self, target):
271
"""See `TargetPickerEntrySource`"""
272
binaries = target.publishing_history[0].getBuiltBinaries()
273
binary_names = [binary.binary_package_name for binary in binaries]
274
if binary_names != []:
275
description = ', '.join(binary_names)
277
description = 'Not yet built.'
282
class ProductPickerEntrySourceAdapter(TargetPickerEntrySourceAdapter):
283
"""Adapts IProduct to IPickerEntrySource."""
285
def getDescription(self, target):
286
"""See `TargetPickerEntrySource`"""
287
return target.summary
290
@adapter(IDistribution)
291
class DistributionPickerEntrySourceAdapter(TargetPickerEntrySourceAdapter):
293
def getDescription(self, target):
294
"""See `TargetPickerEntrySource`"""
295
return target.summary
242
298
@adapter(IArchive)
243
299
class ArchivePickerEntrySourceAdapter(DefaultPickerEntrySourceAdapter):
244
300
"""Adapts IArchive to IPickerEntrySource."""
246
def getPickerEntry(self, term_values, context_object, **kwarg):
302
def getPickerEntries(self, term_values, context_object, **kwarg):
247
303
"""See `IPickerEntrySource`"""
249
305
super(ArchivePickerEntrySourceAdapter, self)
280
336
search_text = self.request.form.get('search_text')
281
337
if search_text is None:
282
338
raise MissingInputError('search_text', '')
339
search_filter = self.request.form.get('search_filter')
285
342
factory = getUtility(IVocabularyFactory, name)
290
347
vocabulary = factory(self.context)
292
349
if IHugeVocabulary.providedBy(vocabulary):
293
matches = vocabulary.searchForTerms(search_text)
350
matches = vocabulary.searchForTerms(search_text, search_filter)
294
351
total_size = matches.count()
296
353
matches = list(vocabulary)
330
387
enhanced_picker_enabled=self.enhanced_picker_enabled,
331
388
picker_expander_enabled=self.picker_expander_enabled,
332
personpicker_affiliation_enabled=
333
self.personpicker_affiliation_enabled)
389
personpicker_affiliation_enabled=(
390
self.personpicker_affiliation_enabled))
334
391
for term_value, picker_entry in izip(term_values, picker_entries):
335
392
picker_term_entries[term_value] = picker_entry