~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/browser/vocabulary.py

  • Committer: Graham Binns
  • Date: 2011-09-07 15:59:13 UTC
  • mto: This revision was merged to the branch mainline in revision 13914.
  • Revision ID: graham@canonical.com-20110907155913-p97tx2e34ysbcgp3
Added an XXX.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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).
3
3
 
4
4
"""Views which export vocabularies as JSON for widgets."""
11
11
    'get_person_picker_entry_metadata',
12
12
    ]
13
13
 
14
 
import simplejson
15
14
from itertools import izip
16
15
 
17
16
from lazr.restful.interfaces import IWebServiceClientRequest
 
17
import simplejson
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,
39
40
    )
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,
 
46
    )
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
146
151
 
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:
222
228
        return entries
223
229
 
224
230
 
 
231
class TargetPickerEntrySourceAdapter(DefaultPickerEntrySourceAdapter):
 
232
    """Adapt targets (Product, Package, Distribution) to PickerEntrySource."""
 
233
 
 
234
    def getDescription(self, target):
 
235
        """Gets the description data for target picker entries."""
 
236
        raise NotImplemented
 
237
 
 
238
    def getPickerEntries(self, term_values, context_object, **kwarg):
 
239
        """See `IPickerEntrySource`"""
 
240
        entries = (
 
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)
 
245
        return entries
 
246
 
 
247
 
225
248
@adapter(ISourcePackageName)
226
249
class SourcePackageNamePickerEntrySourceAdapter(
227
250
                                            DefaultPickerEntrySourceAdapter):
228
251
    """Adapts ISourcePackageName to IPickerEntrySource."""
229
252
 
230
 
    def getPickerEntry(self, term_values, context_object, **kwarg):
 
253
    def getPickerEntries(self, term_values, context_object, **kwarg):
231
254
        """See `IPickerEntrySource`"""
232
255
        entries = (
233
256
            super(SourcePackageNamePickerEntrySourceAdapter, self)
239
262
        return entries
240
263
 
241
264
 
 
265
@adapter(IDistributionSourcePackage)
 
266
class DistributionSourcePackagePickerEntrySourceAdapter(
 
267
    TargetPickerEntrySourceAdapter):
 
268
    """Adapts IDistributionSourcePackage to IPickerEntrySource."""
 
269
 
 
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)
 
276
        else:
 
277
            description = 'Not yet built.'
 
278
        return description
 
279
 
 
280
 
 
281
@adapter(IProduct)
 
282
class ProductPickerEntrySourceAdapter(TargetPickerEntrySourceAdapter):
 
283
    """Adapts IProduct to IPickerEntrySource."""
 
284
 
 
285
    def getDescription(self, target):
 
286
        """See `TargetPickerEntrySource`"""
 
287
        return target.summary
 
288
 
 
289
 
 
290
@adapter(IDistribution)
 
291
class DistributionPickerEntrySourceAdapter(TargetPickerEntrySourceAdapter):
 
292
 
 
293
    def getDescription(self, target):
 
294
        """See `TargetPickerEntrySource`"""
 
295
        return target.summary
 
296
 
 
297
 
242
298
@adapter(IArchive)
243
299
class ArchivePickerEntrySourceAdapter(DefaultPickerEntrySourceAdapter):
244
300
    """Adapts IArchive to IPickerEntrySource."""
245
301
 
246
 
    def getPickerEntry(self, term_values, context_object, **kwarg):
 
302
    def getPickerEntries(self, term_values, context_object, **kwarg):
247
303
        """See `IPickerEntrySource`"""
248
304
        entries = (
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')
283
340
 
284
341
        try:
285
342
            factory = getUtility(IVocabularyFactory, name)
290
347
        vocabulary = factory(self.context)
291
348
 
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()
295
352
        else:
296
353
            matches = list(vocabulary)
329
386
                self.context,
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
336
393