~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/views/api.py

  • Committer: Brad Crittenden
  • Date: 2013-07-17 14:38:00 UTC
  • mto: This revision was merged to the branch mainline in revision 312.
  • Revision ID: bac@canonical.com-20130717143800-dxmlstd311xtsudn
Use Charm model objects rather than dicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
152
152
        }
153
153
 
154
154
    @classmethod
155
 
    def _charm_result(cls, charm_data):
156
 
        charm = Charm(charm_data)
 
155
    def _charm_result(cls, charm):
157
156
        return {
158
157
            'charm': cls._format_charm(charm),
159
158
            'metadata': {
160
159
            }
161
160
        }
162
161
 
163
 
    def _related_charms(self, charm):
164
 
        charms_provide = set(charm['i_provides'])
165
 
        charms_require = set(charm['i_requires'])
 
162
    def _related_charms(self, charm_data):
 
163
        charm = Charm(charm_data)
 
164
        charms_provide = set(charm.i_provides)
 
165
        charms_require = set(charm.i_requires)
166
166
        # Swap provides and requires, so we get the charms which *require*
167
167
        # what these charms provide, and the charms that *provide* what these
168
168
        # charms require.
169
169
        r_requires, r_provides = self.request.index_client.related_charms(
170
 
            charms_provide, charms_require, series=charm['series'],
171
 
            exclude_name=charm['name'])
 
170
            charms_provide, charms_require, series=charm.series,
 
171
            exclude_name=charm.name)
172
172
        f_requires = {}
173
173
        for key, value in r_requires.items():
174
174
            f_requires[key] = [
319
319
 
320
320
    def charm(self, path):
321
321
        """Retrieve a charm according to its charm_id."""
322
 
        charm_id, trailing, charm = self._find_charm(path)
323
 
        if charm is None:
 
322
        charm_id, trailing, charm_data = self._find_charm(path)
 
323
        if charm_data is None:
324
324
            return json_response(
325
325
                404, {'type': 'no_such_charm', 'charm_id': charm_id})
 
326
        charm = Charm(charm_data)
326
327
        if trailing is None:
327
 
            return self._charm_results([charm])[0]
 
328
            return self._charm_results([charm_data])[0]
328
329
        elif trailing.startswith('file/'):
329
330
            return self._charm_file(charm, trailing)
330
331
        elif trailing == ('icon.svg'):
332
333
        elif trailing == ('qa'):
333
334
            return self._charm_qa(charm)
334
335
        elif trailing == ('related'):
335
 
            return self._charm_related(charm)
 
336
            return self._charm_related(charm_data)
336
337
        else:
337
338
            raise HTTPNotFound(self.request.path)
338
339
 
 
340
    def bundle(self, path):
 
341
        """Retrieve a bundle based on id."""
 
342
        raise HTTPNotFound(self.request.path)
 
343
 
339
344
    @staticmethod
340
345
    def _get_file_headers(md5sum, content_type=None):
341
346
        headerlist = [
349
354
        return headerlist
350
355
 
351
356
    def _charm_file(self, charm, trailing):
 
357
        # XXX: BradCrittenden 2013-07-17 remove this shim when possible.
 
358
        #if isinstance(charm, dict):
 
359
        #    charm = Charm(charm)
352
360
        path = trailing.split('/', 1)[1]
353
 
        file_data = charm['files'].get(quote_key(path.split('/')[-1]))
 
361
        file_data = charm.files.get(quote_key(path.split('/')[-1]))
354
362
        if file_data is not None:
355
363
            if_none_match = getattr(self.request, 'if_none_match', None)
356
364
            if if_none_match is not None and file_data['md5'] in if_none_match:
358
366
                    file_data['md5'], file_data.get('contentType'))
359
367
                return Response('', headerlist=headerlist, status_code=304)
360
368
        fs = getfs(self.request.db)
361
 
        file_id = CharmFileSet.gen_fileid(charm, path)
 
369
        file_id = CharmFileSet.gen_fileid(charm._representation, path)
362
370
        try:
363
371
            charm_file = CharmFileSet.get_by_id(fs, file_id)
364
372
        except NoFile:
371
379
            status_code=200)
372
380
 
373
381
    def _icon(self, charm):
374
 
        if charm.get('files') and charm['files'].get(quote_key('icon.svg')) and charm.get('promulgated'):
 
382
        if charm.files and charm.files.get(quote_key('icon.svg')) and charm.promulgated:
375
383
            return self._charm_file(charm, '/icon.svg')
376
 
        elif charm.get('categories'):
377
 
            main_category = charm['categories'][0]
 
384
        elif charm.categories:
 
385
            main_category = charm.categories[0]
378
386
            icon_url = "{0}charm-{1}.svg".format(self.ICON_PATH, main_category)
379
387
        else:
380
388
            icon_url = self.ICON_PATH + 'charm_160.svg'
381
389
        return HTTPFound(icon_url)
382
390
 
383
 
    def _charm_related(self, charm):
384
 
        requires, provides = self._related_charms(charm)
 
391
    def _charm_related(self, charm_data):
 
392
        requires, provides = self._related_charms(charm_data)
385
393
        return {'result': {'requires': requires, 'provides': provides}}
386
394
 
387
395
    @staticmethod
390
398
                    if key != '_id')
391
399
 
392
400
    def _charm_qa(self, charm):
393
 
        qa_data = QAData(self.request.db, charm.get('qa'))
 
401
        qa_data = QAData(self.request.db, charm.qa)
394
402
        categories = [self._format_category(category) for category
395
403
                      in qa_data.qa_categories.values()]
396
404
        scores = qa_data.filtered_scores()
441
449
        return {'result': self._charm_results(results)}
442
450
 
443
451
    def _charm_results(self, charms):
444
 
        return [self._charm_result(charm) for charm in charms]
 
452
        return [self._charm_result(Charm(charm_data)) for charm_data in charms]
445
453
 
446
454
    def _interesting_charms(self):
447
455
        """Generate a JSON structure of interesting charms.