|
|
@ -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 import View
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
|
|
|
|
|
|
from api.models import APIUserToken
|
|
|
|
from api.models import APIUserToken
|
|
|
|
from api.settings import KSP_TOKEN_HEADER_IDENTIFIER
|
|
|
|
from api.settings import KSP_TOKEN_HEADER_IDENTIFIER
|
|
|
|
|
|
|
|
from intervention.models import Intervention
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AbstractModelAPIView(View):
|
|
|
|
class AbstractAPIView(View):
|
|
|
|
""" Base class for API views
|
|
|
|
""" Base class for API views
|
|
|
|
|
|
|
|
|
|
|
|
The API must follow the GeoJSON Specification RFC 7946
|
|
|
|
The API must follow the GeoJSON Specification RFC 7946
|
|
|
@ -22,21 +23,11 @@ class AbstractModelAPIView(View):
|
|
|
|
https://datatracker.ietf.org/doc/html/rfc7946
|
|
|
|
https://datatracker.ietf.org/doc/html/rfc7946
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
serializer = None
|
|
|
|
|
|
|
|
user = None
|
|
|
|
user = None
|
|
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
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
|
|
|
|
@csrf_exempt
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
@ -65,3 +56,76 @@ class AbstractModelAPIView(View):
|
|
|
|
},
|
|
|
|
},
|
|
|
|
status=status_code
|
|
|
|
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
|
|
|
|