"""Logic Tier for Categories.""" from podcast.models import category as category_model def get_categories(): """Get all categories for podcasts. Returns: response.Response: containing a list of dicts with categories. """ flat_categories = category_model.get_categories()['items'] top_level = [category for category in flat_categories if category['parent_id'] is None] for tl in top_level: tl['children'] = [] for category in flat_categories: if category['parent_id'] is None: continue for tl in top_level: if tl['id'] == category['parent_id']: tl['children'].append(category) return {'items': top_level}