Add history + more components
This commit is contained in:
@@ -55,3 +55,62 @@ def item_details(request, id):
|
||||
"linked_properties_headers": header_for_table(LinkedProperty),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def item_history(request, id):
|
||||
|
||||
item = Item.objects.filter(author=request.user.setting, id=id).first()
|
||||
|
||||
if not item:
|
||||
return JsonResponse({}, status=404)
|
||||
|
||||
headers = [field for field in Item.objects.headers().keys() if field not in ["id", "history"]]
|
||||
headers.extend(["history_id", "history_date"])
|
||||
|
||||
qs = item.history.order_by("-history_date").values(*headers)
|
||||
return JsonResponse(
|
||||
{
|
||||
"object": item.serialize(),
|
||||
"headers": header_for_table(Item),
|
||||
# Return only the last 50 versions
|
||||
"history": list(qs[:50]),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def item_history_edit(request, id, hid):
|
||||
|
||||
item = Item.objects.filter(author=request.user.setting, id=id).first()
|
||||
|
||||
if not item:
|
||||
return JsonResponse({}, status=404)
|
||||
|
||||
version = item.history.filter(history_user=request.user.setting, history_id=hid).first()
|
||||
|
||||
if not version:
|
||||
return JsonResponse({}, status=404)
|
||||
|
||||
if request.method == "DELETE":
|
||||
try:
|
||||
version.delete()
|
||||
except Exception:
|
||||
return JsonResponse({}, status=401)
|
||||
|
||||
return JsonResponse({})
|
||||
|
||||
if request.method == "POST":
|
||||
try:
|
||||
version.instance.save(force_update=True)
|
||||
|
||||
except Exception:
|
||||
return JsonResponse({}, status=401)
|
||||
|
||||
return JsonResponse(
|
||||
{
|
||||
"object": version.instance.serialize(),
|
||||
}
|
||||
)
|
||||
|
||||
return JsonResponse({}, status=405)
|
||||
|
||||
Reference in New Issue
Block a user