Beispiel #1
0
def update_featured(request):
    """Saves or removes passed featured product passed id (within request body).
    """
    if request.POST.get("action") == "remove":
        for temp_id in request.POST.keys():

            if not temp_id.startswith("product"):
                continue

            temp_id = temp_id.split("-")[1]
            try:
                featured = FeaturedProduct.objects.get(pk=temp_id)
                featured.delete()
            except (FeaturedProduct.DoesNotExist, ValueError):
                pass
            else:
                _update_positions()
                featured_changed.send(featured)

        html = [[
            "#featured-inline",
            manage_featured_inline(request, as_string=True)
        ]]
        result = json.dumps(
            {
                "html": html,
                "message": _(u"Featured product has been removed.")
            },
            cls=LazyEncoder)

    else:
        for temp_id in request.POST.keys():

            if temp_id.startswith("position") is False:
                continue

            temp_id = temp_id.split("-")[1]
            featured = FeaturedProduct.objects.get(pk=temp_id)

            # Update position
            position = request.POST.get("position-%s" % temp_id)
            featured.position = position
            featured.save()

        _update_positions()

        html = [[
            "#featured-inline",
            manage_featured_inline(request, as_string=True)
        ]]
        result = json.dumps(
            {
                "html": html,
                "message": _(u"Featured product has been updated.")
            },
            cls=LazyEncoder)

    return HttpResponse(result, content_type='application/json')
def update_featured(request):
    """Saves or removes passed featured product passed id (within request body).
    """
    if request.POST.get("action") == "remove":
        for temp_id in request.POST.keys():

            if temp_id.startswith("category") == False:
                continue

            temp_id = temp_id.split("-")[1]
            try:
                featured = FeaturedCategory.objects.get(pk=temp_id)
                featured.delete()
            except (FeaturedProduct.DoesNotExist, ValueError):
                pass

            _update_positions()
            featured_changed.send(featured)

        html = [["#featured-inline", manage_featured_inline(request, as_string=True)]]
        result = simplejson.dumps({
            "html": html,
            "message": _(u"Featured category has been removed.")
        }, cls=LazyEncoder)

    else:
        for temp_id in request.POST.keys():

            if temp_id.startswith("position") == False:
                continue

            temp_id = temp_id.split("-")[1]
            featured = FeaturedCategory.objects.get(pk=temp_id)

            # Update position
            position = request.POST.get("position-%s" % temp_id)
            featured.position = position
            featured.save()

        _update_positions()

        html = [["#featured-inline", manage_featured_inline(request, as_string=True)]]
        result = simplejson.dumps({
            "html": html,
            "message": _(u"Featured category has been updated.")
        }, cls=LazyEncoder)

    return HttpResponse(result)