#31 API WIP
* adds support for GET /check on intervention to run checks automatically via API
This commit is contained in:
@@ -8,9 +8,11 @@ Created on: 21.01.22
|
||||
from django.urls import path
|
||||
|
||||
from api.views.v1.views import EmaAPIViewV1, EcoAccountAPIViewV1, CompensationAPIViewV1, InterventionAPIViewV1
|
||||
from api.views.views import InterventionCheckAPIView
|
||||
|
||||
app_name = "v1"
|
||||
urlpatterns = [
|
||||
path("intervention/<id>/check", InterventionCheckAPIView.as_view(), name="intervention-check"),
|
||||
path("intervention/<id>", InterventionAPIViewV1.as_view(), name="intervention"),
|
||||
path("intervention/", InterventionAPIViewV1.as_view(), name="intervention"),
|
||||
path("compensation/<id>", CompensationAPIViewV1.as_view(), name="compensation"),
|
||||
|
||||
@@ -182,7 +182,6 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
|
||||
# Fill in data to objects
|
||||
properties = json_model["properties"]
|
||||
obj.title = properties["title"]
|
||||
obj.modified = update_action
|
||||
self.set_responsibility(obj, properties.get("responsible", None))
|
||||
self.set_legal(obj, properties.get("legal", None))
|
||||
self.set_payments(obj, properties.get("payments", None))
|
||||
@@ -194,7 +193,7 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
|
||||
obj.legal.save()
|
||||
obj.save()
|
||||
|
||||
obj.log.add(update_action)
|
||||
obj.mark_as_edited(user)
|
||||
|
||||
celery_update_parcels.delay(obj.geometry.id)
|
||||
|
||||
|
||||
@@ -13,13 +13,23 @@ 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
|
||||
from api.views.views import AbstractAPIView
|
||||
|
||||
|
||||
class AbstractModelAPIViewV1(AbstractModelAPIView):
|
||||
class AbstractAPIViewV1(AbstractAPIView):
|
||||
""" Holds general serialization functions for API v1
|
||||
|
||||
"""
|
||||
serializer = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.lookup = {
|
||||
"id": None, # must be set in subclasses
|
||||
"deleted__isnull": True,
|
||||
"users__in": [], # must be set in subclasses
|
||||
}
|
||||
super().__init__(*args, **kwargs)
|
||||
self.serializer = self.serializer()
|
||||
|
||||
def get(self, request: HttpRequest, id=None):
|
||||
""" Handles the GET request
|
||||
@@ -80,17 +90,17 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
|
||||
return JsonResponse({"id": updated_id})
|
||||
|
||||
|
||||
class InterventionAPIViewV1(AbstractModelAPIViewV1):
|
||||
class InterventionAPIViewV1(AbstractAPIViewV1):
|
||||
serializer = InterventionAPISerializerV1
|
||||
|
||||
|
||||
class CompensationAPIViewV1(AbstractModelAPIViewV1):
|
||||
class CompensationAPIViewV1(AbstractAPIViewV1):
|
||||
serializer = CompensationAPISerializerV1
|
||||
|
||||
|
||||
class EcoAccountAPIViewV1(AbstractModelAPIViewV1):
|
||||
class EcoAccountAPIViewV1(AbstractAPIViewV1):
|
||||
serializer = EcoAccountAPISerializerV1
|
||||
|
||||
|
||||
class EmaAPIViewV1(AbstractModelAPIViewV1):
|
||||
class EmaAPIViewV1(AbstractAPIViewV1):
|
||||
serializer = EmaAPISerializerV1
|
||||
|
||||
@@ -6,15 +6,16 @@ Created on: 21.01.22
|
||||
|
||||
"""
|
||||
|
||||
from django.http import JsonResponse
|
||||
from django.http import JsonResponse, HttpRequest
|
||||
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
|
||||
from intervention.models import Intervention
|
||||
|
||||
|
||||
class AbstractModelAPIView(View):
|
||||
class AbstractAPIView(View):
|
||||
""" Base class for API views
|
||||
|
||||
The API must follow the GeoJSON Specification RFC 7946
|
||||
@@ -22,21 +23,11 @@ class AbstractModelAPIView(View):
|
||||
https://datatracker.ietf.org/doc/html/rfc7946
|
||||
|
||||
"""
|
||||
serializer = None
|
||||
user = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.lookup = {
|
||||
"id": None, # must be set in subclasses
|
||||
"deleted__isnull": True,
|
||||
"users__in": [], # must be set in subclasses
|
||||
}
|
||||
super().__init__(*args, **kwargs)
|
||||
self.serializer = self.serializer()
|
||||
|
||||
@csrf_exempt
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
try:
|
||||
@@ -65,3 +56,76 @@ class AbstractModelAPIView(View):
|
||||
},
|
||||
status=status_code
|
||||
)
|
||||
|
||||
|
||||
class InterventionCheckAPIView(AbstractAPIView):
|
||||
|
||||
def get(self, request: HttpRequest, id):
|
||||
""" Takes the GET request
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
|
||||
Returns:
|
||||
response (JsonResponse)
|
||||
"""
|
||||
if not self.user.is_zb_user():
|
||||
return self.return_error_response("Permission not granted", 403)
|
||||
try:
|
||||
obj = Intervention.objects.get(
|
||||
id=id,
|
||||
users__in=[self.user]
|
||||
)
|
||||
except Exception as e:
|
||||
return self.return_error_response(e)
|
||||
|
||||
all_valid, check_details = self.run_quality_checks(obj)
|
||||
|
||||
if all_valid:
|
||||
log_entry = obj.set_checked(self.user)
|
||||
obj.log.add(log_entry)
|
||||
|
||||
data = {
|
||||
"success": all_valid,
|
||||
"details": check_details
|
||||
}
|
||||
return JsonResponse(data)
|
||||
|
||||
def run_quality_checks(self, obj: Intervention) -> (bool, dict):
|
||||
""" Performs a check for intervention and related compensations
|
||||
|
||||
Args:
|
||||
obj (Intervention): The intervention
|
||||
|
||||
Returns:
|
||||
all_valid (boold): Whether an error occured or not
|
||||
check_details (dict): A dict containg details on which elements have errors
|
||||
"""
|
||||
# Run quality check for Intervention
|
||||
all_valid = True
|
||||
intervention_checker = obj.quality_check()
|
||||
all_valid = intervention_checker.valid and all_valid
|
||||
|
||||
# Run quality checks for linked compensations
|
||||
comps = obj.compensations.all()
|
||||
comp_checkers = []
|
||||
for comp in comps:
|
||||
comp_checker = comp.quality_check()
|
||||
comp_checkers.append(comp_checker)
|
||||
all_valid = comp_checker.valid and all_valid
|
||||
|
||||
check_details = {
|
||||
"intervention": {
|
||||
"id": obj.id,
|
||||
"errors": intervention_checker.messages
|
||||
},
|
||||
"compensations": [
|
||||
{
|
||||
"id": comp_checker.obj.id,
|
||||
"errors": comp_checker.messages
|
||||
}
|
||||
for comp_checker in comp_checkers
|
||||
]
|
||||
}
|
||||
return all_valid, check_details
|
||||
|
||||
Reference in New Issue
Block a user