@permission_required("core.manage_shop")
def update_attachments(request, product_id):
    """Saves/deletes attachments with given ids (passed by request body).
    """
    product = lfs_get_object_or_404(Product, pk=product_id)
    action = request.POST.get("action")
    message = _(u"Attachment has been updated.")

    if action == "delete":
        message = _(u"Attachment has been deleted.")
        for key in request.POST.keys():
            if key.startswith("delete-"):
                try:
                    id = key.split("-")[1]
                    attachment = ProductAttachment.objects.get(pk=id).delete()
                except (__HOLE__, ObjectDoesNotExist):
                    pass
    elif action == "update":
        message = _(u"Attachment has been updated.")
        for attachment in product.attachments.all():
            attachment.title = request.POST.get("title-%s" % attachment.id)[:50]
            attachment.position = request.POST.get("position-%s" % attachment.id)
            attachment.description = request.POST.get("description-%s" % attachment.id)
            attachment.save()

    # Refresh positions
    for i, attachment in enumerate(product.attachments.all()):
        attachment.position = (i + 1) * 10
        attachment.save()

    product_changed.send(product, request=request)

    html = [["#attachments-list", list_attachments(request, product_id, as_string=True)]]
    result = json.dumps({
        "html": html,
        "message": message,
    }, cls=LazyEncoder)

    return HttpResponse(result, content_type='application/json')
dataset/ETHPy150Open diefenbach/django-lfs/lfs/manage/product/attachments.py/update_attachments