Add model POST API (#45)

loop
Etzelia 2019-11-24 19:52:29 +01:00 committed by Gitea
parent e1696d3f5f
commit e91f82366d
1 changed files with 26 additions and 1 deletions

View File

@ -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):