#31 API POST Intervention

* adds support for proper POST of intervention
* makes /<id> optional (required for Post)
This commit is contained in:
2022-01-24 12:17:17 +01:00
parent 45ac5b68b9
commit d0f3fb9f61
12 changed files with 171 additions and 41 deletions

View File

@@ -5,12 +5,14 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
import json
from django.http import JsonResponse, HttpRequest
from api.utils.v1.compensation import CompensationAPISerializerV1
from api.utils.v1.ecoaccount import EcoAccountAPISerializerV1
from api.utils.v1.ema import EmaAPISerializerV1
from api.utils.v1.intervention import InterventionAPISerializerV1
from api.utils.serializer.v1.compensation import CompensationAPISerializerV1
from api.utils.serializer.v1.ecoaccount import EcoAccountAPISerializerV1
from api.utils.serializer.v1.ema import EmaAPISerializerV1
from api.utils.serializer.v1.intervention import InterventionAPISerializerV1
from api.views.views import AbstractModelAPIView
@@ -19,7 +21,7 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
"""
def get(self, request: HttpRequest, id):
def get(self, request: HttpRequest, id=None):
""" Handles the GET request
Performs the fetching and serialization of the data
@@ -32,12 +34,23 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
"""
try:
if id is None:
raise AttributeError("No id provided")
self.serializer.prepare_lookup(id, self.user)
data = self.serializer.fetch_and_serialize()
except Exception as e:
return self.return_error_response(e, 500)
return JsonResponse(data)
def post(self, request: HttpRequest, id=None):
try:
body = request.body.decode("utf-8")
body = json.loads(body)
created_id = self.serializer.create_model_from_json(body, self.user)
except Exception as e:
return self.return_error_response(e, 500)
return JsonResponse({"id": created_id})
class InterventionAPIViewV1(AbstractModelAPIViewV1):
serializer = InterventionAPISerializerV1

View File

@@ -8,6 +8,7 @@ Created on: 21.01.22
from django.http import JsonResponse
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from api.models import APIUserToken
from api.settings import KSP_TOKEN_HEADER_IDENTIFIER
@@ -36,6 +37,7 @@ class AbstractModelAPIView(View):
super().__init__(*args, **kwargs)
self.serializer = self.serializer()
@csrf_exempt
def dispatch(self, request, *args, **kwargs):
try:
# Fetch the proper user from the given request header token