2022-01-21 15:26:08 +01:00
|
|
|
"""
|
|
|
|
Author: Michel Peltriaux
|
|
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
|
|
Created on: 21.01.22
|
|
|
|
|
|
|
|
"""
|
2022-01-24 12:17:17 +01:00
|
|
|
import json
|
|
|
|
|
2022-01-21 18:34:01 +01:00
|
|
|
from django.http import JsonResponse, HttpRequest
|
2022-01-21 15:26:08 +01:00
|
|
|
|
2022-01-24 12:17:17 +01:00
|
|
|
from api.utils.serializer.v1.compensation import CompensationAPISerializerV1
|
2022-01-28 15:44:09 +01:00
|
|
|
from api.utils.serializer.v1.deduction import DeductionAPISerializerV1
|
2022-01-24 12:17:17 +01:00
|
|
|
from api.utils.serializer.v1.ecoaccount import EcoAccountAPISerializerV1
|
|
|
|
from api.utils.serializer.v1.ema import EmaAPISerializerV1
|
|
|
|
from api.utils.serializer.v1.intervention import InterventionAPISerializerV1
|
2022-01-25 09:29:14 +01:00
|
|
|
from api.views.views import AbstractAPIView
|
2022-01-21 15:26:08 +01:00
|
|
|
|
|
|
|
|
2022-01-25 09:29:14 +01:00
|
|
|
class AbstractAPIViewV1(AbstractAPIView):
|
2022-01-21 15:26:08 +01:00
|
|
|
""" Holds general serialization functions for API v1
|
|
|
|
|
|
|
|
"""
|
2022-01-25 09:29:14 +01:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.serializer = self.serializer()
|
2022-01-21 15:26:08 +01:00
|
|
|
|
2022-01-24 12:17:17 +01:00
|
|
|
def get(self, request: HttpRequest, id=None):
|
2022-01-21 18:34:01 +01:00
|
|
|
""" Handles the GET request
|
|
|
|
|
|
|
|
Performs the fetching and serialization of the data
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
2022-01-24 16:23:38 +01:00
|
|
|
id (str): The entries id (optional)
|
2022-01-21 18:34:01 +01:00
|
|
|
|
|
|
|
Returns:
|
2022-01-24 16:23:38 +01:00
|
|
|
response (JsonResponse)
|
2022-01-21 18:34:01 +01:00
|
|
|
"""
|
|
|
|
try:
|
2022-02-16 11:38:24 +01:00
|
|
|
self.rpp = int(request.GET.get("rpp", self.rpp))
|
|
|
|
self.page_number = int(request.GET.get("p", self.page_number))
|
|
|
|
|
|
|
|
self.serializer.rpp = self.rpp
|
|
|
|
self.serializer.page_number = self.page_number
|
|
|
|
|
2022-01-24 10:31:48 +01:00
|
|
|
self.serializer.prepare_lookup(id, self.user)
|
|
|
|
data = self.serializer.fetch_and_serialize()
|
2022-01-21 18:34:01 +01:00
|
|
|
except Exception as e:
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response(e, 500)
|
|
|
|
return self._return_response(request, data)
|
2022-01-21 18:34:01 +01:00
|
|
|
|
2022-01-24 16:23:38 +01:00
|
|
|
def post(self, request: HttpRequest):
|
|
|
|
""" Handles the POST request
|
|
|
|
|
|
|
|
Performs creation of new data
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
response (JsonResponse)
|
|
|
|
"""
|
2022-01-24 12:17:17 +01:00
|
|
|
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:
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response(e, 500)
|
2022-01-24 12:17:17 +01:00
|
|
|
return JsonResponse({"id": created_id})
|
|
|
|
|
2022-01-24 13:04:39 +01:00
|
|
|
def put(self, request: HttpRequest, id=None):
|
2022-01-24 16:23:38 +01:00
|
|
|
""" Handles the PUT request
|
|
|
|
|
|
|
|
Performs updating
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The entries id
|
|
|
|
|
|
|
|
Returns:
|
2022-01-28 08:52:11 +01:00
|
|
|
response (JsonResponse)
|
2022-01-24 16:23:38 +01:00
|
|
|
"""
|
2022-01-24 13:04:39 +01:00
|
|
|
try:
|
|
|
|
body = request.body.decode("utf-8")
|
|
|
|
body = json.loads(body)
|
|
|
|
updated_id = self.serializer.update_model_from_json(id, body, self.user)
|
|
|
|
except Exception as e:
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response(e, 500)
|
2022-01-24 13:04:39 +01:00
|
|
|
return JsonResponse({"id": updated_id})
|
|
|
|
|
2022-01-28 08:52:11 +01:00
|
|
|
def delete(self, request: HttpRequest, id=None):
|
|
|
|
""" Handles a DELETE request
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The object's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
response (JsonResponse)
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
success = self.serializer.delete_entry(id, self.user)
|
|
|
|
except Exception as e:
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response(e, 500)
|
2022-01-28 08:52:11 +01:00
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"success": success,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-01-21 15:26:08 +01:00
|
|
|
|
2022-01-25 09:29:14 +01:00
|
|
|
class InterventionAPIViewV1(AbstractAPIViewV1):
|
2022-01-24 10:31:48 +01:00
|
|
|
serializer = InterventionAPISerializerV1
|
2022-01-21 15:26:08 +01:00
|
|
|
|
|
|
|
|
2022-01-25 09:29:14 +01:00
|
|
|
class CompensationAPIViewV1(AbstractAPIViewV1):
|
2022-01-24 10:31:48 +01:00
|
|
|
serializer = CompensationAPISerializerV1
|
2022-01-21 15:26:08 +01:00
|
|
|
|
|
|
|
|
2022-01-25 09:29:14 +01:00
|
|
|
class EcoAccountAPIViewV1(AbstractAPIViewV1):
|
2022-01-24 10:31:48 +01:00
|
|
|
serializer = EcoAccountAPISerializerV1
|
2022-01-21 18:34:01 +01:00
|
|
|
|
|
|
|
|
2022-01-25 09:29:14 +01:00
|
|
|
class EmaAPIViewV1(AbstractAPIViewV1):
|
2022-01-24 10:31:48 +01:00
|
|
|
serializer = EmaAPISerializerV1
|
2022-01-28 15:44:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DeductionAPIViewV1(AbstractAPIViewV1):
|
|
|
|
serializer = DeductionAPISerializerV1
|