456
459
scores = qa_data.filtered_scores()
457
460
return {'result': {'questions': categories}, 'scores': scores}
459
def charms(self, path=None, *args, **kwargs):
462
def search(self, path=None, *args, **kwargs):
460
463
if path is None or path == ['']:
461
handler = self._charms
464
handler = self._search
462
465
elif path[0] == 'interesting':
463
466
handler = self._interesting_charms
465
468
raise HTTPNotFound(self.request.path)
466
469
return self._handle(handler, args, kwargs)
468
def _charms(self, limit=None, name=None, series=None, owner=None,
471
def _search(self, limit=None, name=None, series=None, owner=None,
469
472
provides=None, requires=None, type_=None, provider=None,
470
scope=None, categories=None, text=None, autocomplete=False):
471
"""Search for charms matching parameters.
473
scope=None, categories=None, text=None, autocomplete=False,
475
"""Search for charms and bundles matching parameters.
473
477
:limit: A query limit. (max number of results)
488
492
limit = int(limit[0])
490
494
results = self.request.index_client.api_search(
491
text[0], filters, type_, limit, autocomplete=autocomplete)
495
text[0], filters, type_, limit, autocomplete=autocomplete,
492
497
except InvalidCharmType as e:
493
498
return json_response(406, {
494
499
'type': 'unsupported_value',
498
503
return json_response(406, {
499
504
'type': 'negative_value',
500
505
'parameter': 'limit'})
501
return {'result': self._charm_results(results)}
506
return {'result': self._item_results(results)}
503
def _charm_results(self, charms):
504
return [self._charm_result(Charm(charm_data)) for charm_data in charms]
508
def _item_results(self, items):
511
# XXX: sinzui: api_search() stripped the doctype.
512
# A formatter might be needed.
515
'bundle': Bundle(item)._representation,
521
result = self._charm_result(Charm(item))
522
results.append(result)
506
525
def _interesting_charms(self):
507
526
"""Generate a JSON structure of interesting charms.
515
534
featured = FeaturedSource.from_db(db).get_featured(db.charms, 'charm')
518
'new': self._charm_results(new),
519
'featured': self._charm_results(featured),
520
'popular': self._charm_results(popular),
537
'new': self._item_results(new),
538
'featured': self._item_results(featured),
539
'popular': self._item_results(popular),
531
550
Methods may return a webob Response, which is returned directly, or a
532
551
json-serializable value, which will be returned as a json HTTP response.
554
def charms(self, path=None, *args, **kwargs):
555
if path is None or path == ['']:
556
handler = self._search
557
kwargs['doctype'] = CHARM
558
elif path[0] == 'interesting':
559
handler = self._interesting_charms
561
raise HTTPNotFound(self.request.path)
562
return self._handle(handler, args, kwargs)