def category_sidebar(request, form_data, facets):
    APP = request.APP
    qatype, qcat = form_data.get('atype'), form_data.get('cat')
    cats = [f['term'] for f in facets['categories']]
    categories = Category.objects.filter(id__in=cats)
    if qatype in amo.ADDON_TYPES:
        categories = categories.filter(type=qatype)
    # Search categories don't have an application.
    categories = categories.filter(Q(application=APP.id) |
                                   Q(type=amo.ADDON_SEARCH))

    # If category is listed as a facet but type is not, then show All.
    if qcat in cats and not qatype:
        qatype = True

    # If category is not listed as a facet NOR available for this application,
    # then show All.
    if qcat not in categories.values_list('id', flat=True):
        qatype = qcat = None

    categories = [(_atype, sorted(_cats, key=lambda x: x.name))
                  for _atype, _cats in sorted_groupby(categories, 'type')]

    rv = []
    cat_params = dict(cat=None)
    all_label = _(u'All Add-ons')

    rv = [FacetLink(all_label, dict(atype=None, cat=None), not qatype)]

    for addon_type, cats in categories:
        selected = addon_type == qatype and not qcat

        # Build the linkparams.
        cat_params = cat_params.copy()
        cat_params.update(atype=addon_type)

        link = FacetLink(amo.ADDON_TYPES[addon_type],
                         cat_params, selected)
        link.children = [FacetLink(c.name, dict(cat_params, **dict(cat=c.id)),
                                   c.id == qcat) for c in cats]
        rv.append(link)
    return rv
dataset/ETHPy150Open mozilla/addons-server/src/olympia/search/views.py/category_sidebar/original