Esempio n. 1
0
    def _format_output(self, request, action, action_result):
        fields = request.params.get('fields', None)
        if action not in ('index', 'show') or fields is None:
            return action_result

        if action == 'show':
            action_result = utils.apply_template(fields, action_result)
            return action_result
        sp = utils.split_by_comma(fields)
        top_level = []
        items = []
        for string in sp:
            if 'items' in string:
                items.append(string)
            else:
                top_level.append(string)
        res = {}
        if len(items) > 0:
            res['items'] = []
        for string in top_level:
            dct = utils.apply_template(string, action_result)
            for key, val in dct.items():
                res[key] = val
        for string in items:
            if '(' in string:
                dct = utils.apply_template(string, action_result)
                for key, val in dct.items():
                    res[key] = val
            elif string.startswith('items/'):
                string = string[len('items/'):]
                for element in action_result['items']:
                    dct = utils.apply_template(string, element)
                    res['items'].append(dct)

        return res
Esempio n. 2
0
 def test_split_by_comma(self):
     string = 'bla,bla,smth/else,another(bla,bla/bla),yet/bla'
     expected = ['bla', 'bla', 'smth/else', 'another(bla,bla/bla)',
                 'yet/bla']
     res = utils.split_by_comma(string)
     self.assertEqual(res, expected)