From e91f82366d74bf1ff2a3bd74f92b2ec42728ebff Mon Sep 17 00:00:00 2001 From: Etzelia Date: Sun, 24 Nov 2019 19:52:29 +0100 Subject: [PATCH] Add model POST API (#45) --- api/views.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/api/views.py b/api/views.py index 04d55b7..88a0f4c 100644 --- a/api/views.py +++ b/api/views.py @@ -398,7 +398,32 @@ class ModelAPI(View): return JsonResponse(json, safe=False) def post(self, request, request_model): - pass + json = {"success": False, "message": ""} + print(request.POST) + if request_allowed(request, 'model_post_permission'): + post = request.POST + model = None + for m in apps.get_app_config('minecraft_manager').get_models(): + if m._meta.model_name.upper() == request_model.upper(): + model = m + break + if model: + keywords = clean(model, post) + if "id" in keywords: + try: + obj = model.objects.get(id=keywords["id"]) + for key in keywords.keys(): + setattr(obj, key, keywords[key]) + obj.save() + json["success"] = True + json["message"] = "Model updated" + except Exception as ex: + print(ex) + json["message"] = "Could not update model" + else: + json["message"] = "Must provide an ID" + + return JsonResponse(json) class StatsAPI(View):