def get_product_filters(category, product_filter, price_filter, manufacturer_filter, sorting):
    """Returns the next product filters based on products which are in the given
    category and within the result set of the current filters.
    """
    mapping_manager = MappingCache()

    properties_mapping = get_property_mapping()
    options_mapping = get_option_mapping()
    property_ids = _get_property_ids()
    product_ids = _get_product_ids(category)
    set_filters = dict(product_filter)

    ########## Number Fields ###################################################
    number_fields_dict = {}
    if property_ids and product_ids:
        cursor = connection.cursor()
        cursor.execute("""SELECT property_group_id, property_id, min(value_as_float), max(value_as_float)
                            FROM catalog_productpropertyvalue
                           WHERE type=%s
                             AND product_id IN (%s)
                             AND property_id IN (%s)
                        GROUP BY property_group_id, property_id""" % (PROPERTY_VALUE_TYPE_FILTER, product_ids, property_ids))
        for row in cursor.fetchall():
            property_group_id = row[0]
            property_id = row[1]

            prop = properties_mapping[property_id]

            if prop.is_select_field or prop.is_text_field or not prop.filterable:
                continue

            # cache property groups for later use
            property_group = mapping_manager.get(lfs.catalog.models.PropertyGroup, property_group_id)

            key = '{0}_{1}'.format(property_group_id, property_id)
            if key in product_filter.get("number-filter", {}):
                pmin, pmax = product_filter.get("number-filter").get(key)['value'][0:2]
                show_reset = True
            else:
                pmin, pmax = row[2:4]
                show_reset = False

            try:
                pmin = locale.format("%.2f", float(pmin))
            except __HOLE__:
                pmin = 0.0
            try:
                pmax = locale.format("%.2f", float(pmax))
            except TypeError:
                pmax = 0.0

            property_group_dict = number_fields_dict.setdefault(property_group_id, {'property_group': property_group,
                                                                                    'items': []})

            property_group_dict['items'].append({
                "id": property_id,
                "property_group_id": property_group_id,
                "position": prop.position,
                "object": prop,
                "name": prop.name,
                "title": prop.title,
                "unit": prop.unit,
                "show_reset": show_reset,
                "show_quantity": True,
                "items": {"min": pmin, "max": pmax}
            })

    # convert to list ordered by property group name
    number_fields = number_fields_dict.values()
    number_fields = sorted(number_fields, key=lambda a: a["property_group"].name)
    for pg in number_fields:
        pg['items'] = sorted(pg['items'], key=lambda a: a['name'])

    ########## Select Fields & Text Fields #####################################
    select_fields_dict = {}
    if property_ids and product_ids:
        cursor = connection.cursor()
        cursor.execute("""SELECT property_group_id, property_id, value
                          FROM catalog_productpropertyvalue
                          WHERE type=%s
                          AND product_id IN (%s)
                          AND property_id IN (%s)
                          GROUP BY property_group_id, property_id, value""" % (PROPERTY_VALUE_TYPE_FILTER, product_ids, property_ids))

        for row in cursor.fetchall():
            property_group_id = row[0]
            property_id = row[1]
            value = row[2]

            prop = properties_mapping[property_id]

            if prop.is_number_field or not prop.filterable:
                continue

            # use property group cache
            property_group = mapping_manager.get(lfs.catalog.models.PropertyGroup, property_group_id)
            property_group_dict = select_fields_dict.setdefault(property_group_id, {'property_group': property_group,
                                                                                    'properties': {}})

            properties = property_group_dict['properties']

            if prop.is_select_field:
                name = options_mapping[value].name
                position = options_mapping[value].position
            else:
                name = value
                position = 10

            if name == value and name == '':
                continue

            # initialize list of property values
            properties.setdefault(property_id, [])

            properties[property_id].append({
                "id": property_id,
                "property_group_id": property_group_id,
                "value": value,
                "name": name,
                "title": prop.title,
                "position": position,
                "show_quantity": True,
            })

    # Creates the filters to count the existing products per property option,
    # which is used within the filter portlet
    new_product_filter = {}
    if product_filter.get("number-filter"):
        new_product_filter["number-filter"] = product_filter["number-filter"]

    for property_group_id, property_group_dict in select_fields_dict.items():
        properties = property_group_dict['properties']

        for property_id, options in properties.items():
            key = '{0}_{1}'.format(property_group_id, property_id)
            for option in options:
                # The option in question is used at any rate
                new_product_filter["select-filter"] = {key: {'property_id': property_id,
                                                             'property_group_id': property_group_id,
                                                             'value': option["value"]}}

                # All checked options of all other properties is also used
                for f0, f1 in product_filter.get("select-filter", {}).items():
                    print f0, f1, key
                    if f0 != key:
                        new_product_filter["select-filter"][f0] = f1

                    # Tests if the option is checked
                    if (f0 == key) and (option["value"] in f1['value'].split("|")):
                        option["checked"] = True

                option["quantity"] = len(get_filtered_products_for_category(category, new_product_filter, price_filter, None))

    # Transform the property groups and properties inside into lists to be able to iterate over these in template
    property_groups_list = select_fields_dict.values()

    for property_group_dict in property_groups_list:
        properties = property_group_dict['properties']
        property_group_id = property_group_dict['property_group'].pk
        result = []

        # Transform the group properties into a list of dicts
        for property_id, items in properties.items():
            prop = properties_mapping[property_id]
            items.sort(lambda a, b: cmp(a["position"], b["position"]))

            # Move items with zero quantity to the end of the list
            for x in range(0, len(items)):
                if items[x]["quantity"] == 0:
                    items.insert(len(items), items.pop(x))

            result.append({
                "id": property_id,
                "property_group_id": property_group_id,
                "position": prop.position,
                "unit": prop.unit,
                "show_reset": '%s_%s' % (property_group_id, property_id) in set_filters.get('select-filter', {}).keys(),
                "name": prop.name,
                "title": prop.title,
                "items": items,
            })

        result = sorted(result, key=lambda a: a["position"])
        property_group_dict['properties'] = result
    property_groups_list = sorted(property_groups_list, key=lambda a: a['property_group'].name)

    return {
        "select_fields": property_groups_list,
        "number_fields": number_fields,
    }
dataset/ETHPy150Open diefenbach/django-lfs/lfs/catalog/utils.py/get_product_filters