@permission_required("core.manage_shop")
def manage_accessories_inline(request, product_id, as_string=False, template_name="manage/product/accessories_inline.html"):
    """View which shows all accessories for the product with the passed id.
    """
    product = Product.objects.get(pk=product_id)
    product_accessories = ProductAccessories.objects.filter(product=product_id)
    accessory_ids = [p.accessory.id for p in product_accessories]

    r = request.POST if request.method == 'POST' else request.GET
    s = request.session

    # If we get the parameter ``keep-filters`` or ``page`` we take the
    # filters out of the request resp. session. The request takes precedence.
    # The page parameter is given if the user clicks on the next/previous page
    # links. The ``keep-filters`` parameters is given is the users adds/removes
    # products. In this way we keeps the current filters when we needed to. If
    # the whole page is reloaded there is no ``keep-filters`` or ``page`` and
    # all filters are reset as they should.

    if r.get("keep-filters") or r.get("page"):
        page = r.get("page", s.get("accessories_page", 1))
        filter_ = r.get("filter", s.get("filter"))
        category_filter = r.get("accessories_category_filter",
                          s.get("accessories_category_filter"))
    else:
        page = r.get("page", 1)
        filter_ = r.get("filter")
        category_filter = r.get("accessories_category_filter")

    # The current filters are saved in any case for later use.
    s["accessories_page"] = page
    s["filter"] = filter_
    s["accessories_category_filter"] = category_filter

    try:
        s["accessories-amount"] = int(r.get("accessories-amount",
                                      s.get("accessories-amount")))
    except __HOLE__:
        s["accessories-amount"] = 25

    filters = Q()
    if filter_:
        filters &= Q(name__icontains=filter_)
        filters |= Q(sku__icontains=filter_)
        filters |= (Q(sub_type=VARIANT) & Q(active_sku=False) & Q(parent__sku__icontains=filter_))
        filters |= (Q(sub_type=VARIANT) & Q(active_name=False) & Q(parent__name__icontains=filter_))

    if category_filter:
        if category_filter == "None":
            filters &= Q(categories=None)
        elif category_filter == "All":
            pass
        else:
            # First we collect all sub categories and using the `in` operator
            category = lfs_get_object_or_404(Category, pk=category_filter)
            categories = [category]
            categories.extend(category.get_all_children())

            filters &= Q(categories__in=categories)

    products = Product.objects.filter(filters).exclude(pk=product_id)

    paginator = Paginator(products.exclude(pk__in=accessory_ids), s["accessories-amount"])

    try:
        page = paginator.page(page)
    except EmptyPage:
        page = 0

    result = render_to_string(template_name, RequestContext(request, {
        "product": product,
        "product_accessories": product_accessories,
        "page": page,
        "paginator": paginator,
        "filter": filter_,
    }))

    if as_string:
        return result
    else:
        return HttpResponse(
            json.dumps({
                "html": [["#accessories-inline", result]],
            }), content_type='application/json')


# Actions
dataset/ETHPy150Open diefenbach/django-lfs/lfs/manage/product/accessories.py/manage_accessories_inline