~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/views/api.py

  • Committer: Tarmac
  • Author(s): Brad Crittenden
  • Date: 2013-07-17 15:05:30 UTC
  • mfrom: (310.1.2 use-charm-objects)
  • Revision ID: tarmac-20130717150530-sirtkpu7vpklu7du
[r=sinzui][bug=][author=bac] In the api internals there was a mixture of raw charm dicts and Charm model objects. This branch ensures all charm usage are model objects.

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] = [
325
325
 
326
326
    def charm(self, path):
327
327
        """Retrieve a charm according to its charm_id."""
328
 
        charm_id, trailing, charm = self._find_charm(path)
329
 
        if charm is None:
 
328
        charm_id, trailing, charm_data = self._find_charm(path)
 
329
        if charm_data is None:
330
330
            return json_response(
331
331
                404, {'type': 'no_such_charm', 'charm_id': charm_id})
 
332
        charm = Charm(charm_data)
332
333
        if trailing is None:
333
 
            return self._charm_results([charm])[0]
 
334
            return self._charm_results([charm_data])[0]
334
335
        elif trailing.startswith('file/'):
335
336
            return self._charm_file(charm, trailing)
336
337
        elif trailing == ('icon.svg'):
338
339
        elif trailing == ('qa'):
339
340
            return self._charm_qa(charm)
340
341
        elif trailing == ('related'):
341
 
            return self._charm_related(charm)
 
342
            return self._charm_related(charm_data)
342
343
        else:
343
344
            raise HTTPNotFound(self.request.path)
344
345
 
 
346
    def bundle(self, path):
 
347
        """Retrieve a bundle based on id."""
 
348
        raise HTTPNotFound(self.request.path)
 
349
 
345
350
    @staticmethod
346
351
    def _get_file_headers(md5sum, content_type=None):
347
352
        headerlist = [
356
361
 
357
362
    def _charm_file(self, charm, trailing):
358
363
        path = trailing.split('/', 1)[1]
359
 
        file_data = charm['files'].get(quote_key(path.split('/')[-1]))
 
364
        file_data = charm.files.get(quote_key(path.split('/')[-1]))
360
365
        if file_data is not None:
361
366
            if_none_match = getattr(self.request, 'if_none_match', None)
362
367
            if if_none_match is not None and file_data['md5'] in if_none_match:
364
369
                    file_data['md5'], file_data.get('contentType'))
365
370
                return Response('', headerlist=headerlist, status_code=304)
366
371
        fs = getfs(self.request.db)
367
 
        file_id = CharmFileSet.gen_fileid(charm, path)
 
372
        file_id = CharmFileSet.gen_fileid(charm._representation, path)
368
373
        try:
369
374
            charm_file = CharmFileSet.get_by_id(fs, file_id)
370
375
        except NoFile:
377
382
            status_code=200)
378
383
 
379
384
    def _icon(self, charm):
380
 
        if charm.get('files') and charm['files'].get(quote_key('icon.svg')) and charm.get('promulgated'):
 
385
        if charm.files and charm.files.get(quote_key('icon.svg')) and charm.promulgated:
381
386
            return self._charm_file(charm, '/icon.svg')
382
 
        elif charm.get('categories'):
383
 
            main_category = charm['categories'][0]
 
387
        elif charm.categories:
 
388
            main_category = charm.categories[0]
384
389
            icon_url = "{0}charm-{1}.svg".format(self.ICON_PATH, main_category)
385
390
        else:
386
391
            icon_url = self.ICON_PATH + 'charm_160.svg'
387
392
        return HTTPFound(icon_url)
388
393
 
389
 
    def _charm_related(self, charm):
390
 
        requires, provides = self._related_charms(charm)
 
394
    def _charm_related(self, charm_data):
 
395
        requires, provides = self._related_charms(charm_data)
391
396
        return {'result': {'requires': requires, 'provides': provides}}
392
397
 
393
398
    @staticmethod
396
401
                    if key != '_id')
397
402
 
398
403
    def _charm_qa(self, charm):
399
 
        qa_data = QAData(self.request.db, charm.get('qa'))
 
404
        qa_data = QAData(self.request.db, charm.qa)
400
405
        categories = [self._format_category(category) for category
401
406
                      in qa_data.qa_categories.values()]
402
407
        scores = qa_data.filtered_scores()
447
452
        return {'result': self._charm_results(results)}
448
453
 
449
454
    def _charm_results(self, charms):
450
 
        return [self._charm_result(charm) for charm in charms]
 
455
        return [self._charm_result(Charm(charm_data)) for charm_data in charms]
451
456
 
452
457
    def _interesting_charms(self):
453
458
        """Generate a JSON structure of interesting charms.