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-26 09:16:37 +01:00
|
|
|
import json
|
2022-01-21 15:26:08 +01:00
|
|
|
|
2022-01-26 09:16:37 +01:00
|
|
|
from django.db.models import QuerySet
|
2022-01-25 09:29:14 +01:00
|
|
|
from django.http import JsonResponse, HttpRequest
|
2022-01-21 15:26:08 +01:00
|
|
|
from django.views import View
|
2022-01-24 12:17:17 +01:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2022-01-21 15:26:08 +01:00
|
|
|
|
2022-01-21 16:15:16 +01:00
|
|
|
from api.models import APIUserToken
|
2022-01-28 16:35:25 +01:00
|
|
|
from api.settings import KSP_TOKEN_HEADER_IDENTIFIER, KSP_USER_HEADER_IDENTIFIER
|
2022-01-26 09:16:37 +01:00
|
|
|
from compensation.models import EcoAccount
|
|
|
|
from ema.models import Ema
|
2022-01-25 09:29:14 +01:00
|
|
|
from intervention.models import Intervention
|
2022-01-26 09:16:37 +01:00
|
|
|
from konova.utils.message_templates import DATA_UNSHARED
|
2022-02-18 13:52:27 +01:00
|
|
|
from user.models import User, Team
|
2022-01-21 16:15:16 +01:00
|
|
|
|
2022-01-21 15:26:08 +01:00
|
|
|
|
2022-01-25 09:29:14 +01:00
|
|
|
class AbstractAPIView(View):
|
2022-01-21 15:26:08 +01:00
|
|
|
""" Base class for API views
|
|
|
|
|
|
|
|
The API must follow the GeoJSON Specification RFC 7946
|
|
|
|
https://geojson.org/
|
|
|
|
https://datatracker.ietf.org/doc/html/rfc7946
|
|
|
|
|
|
|
|
"""
|
2022-01-21 16:15:16 +01:00
|
|
|
user = None
|
2022-02-16 11:38:24 +01:00
|
|
|
serializer = None
|
|
|
|
rpp = 5 # Results per page default
|
|
|
|
page_number = 1 # Page number default
|
2022-01-21 15:26:08 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2022-02-16 11:38:24 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.response_body_base = {
|
|
|
|
"rpp": None,
|
|
|
|
"p": None,
|
|
|
|
"next": None,
|
|
|
|
"results": None
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:17:17 +01:00
|
|
|
@csrf_exempt
|
2022-01-21 16:15:16 +01:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
2022-01-24 10:31:48 +01:00
|
|
|
# Fetch the proper user from the given request header token
|
2025-01-21 13:38:37 +01:00
|
|
|
token = request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None)
|
2022-01-28 16:35:25 +01:00
|
|
|
ksp_user = request.headers.get(KSP_USER_HEADER_IDENTIFIER, None)
|
2023-08-17 10:44:58 +02:00
|
|
|
|
2025-01-21 13:38:37 +01:00
|
|
|
if not token and not ksp_user:
|
|
|
|
bearer_token = request.headers.get("authorization", None)
|
|
|
|
if not bearer_token:
|
|
|
|
raise PermissionError("No token provided")
|
|
|
|
token = bearer_token.split(" ")[1]
|
|
|
|
|
|
|
|
token_user = APIUserToken.get_user_from_token(token)
|
|
|
|
if ksp_user and ksp_user != token_user.username:
|
2023-08-17 10:44:58 +02:00
|
|
|
raise PermissionError(f"Invalid token for {ksp_user}")
|
2025-01-21 13:38:37 +01:00
|
|
|
self.user = token_user
|
2023-08-17 10:44:58 +02:00
|
|
|
|
2022-02-16 11:38:24 +01:00
|
|
|
request.user = self.user
|
2022-01-27 14:48:42 +01:00
|
|
|
if not self.user.is_default_user():
|
|
|
|
raise PermissionError("Default permissions required")
|
2022-01-21 16:15:16 +01:00
|
|
|
except PermissionError as e:
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response(e, 403)
|
2022-01-21 16:15:16 +01:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2022-01-21 18:34:01 +01:00
|
|
|
|
2022-02-16 11:38:24 +01:00
|
|
|
def _return_error_response(self, error, status_code=500):
|
2022-01-21 18:34:01 +01:00
|
|
|
""" Returns an error as JsonReponse
|
|
|
|
|
|
|
|
Args:
|
|
|
|
error (): The error/exception
|
|
|
|
status_code (): The desired status code
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
content = [error.__str__()]
|
|
|
|
if hasattr(error, "messages"):
|
|
|
|
content = error.messages
|
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"errors": content
|
|
|
|
},
|
|
|
|
status=status_code
|
|
|
|
)
|
2022-01-25 09:29:14 +01:00
|
|
|
|
2022-02-16 11:38:24 +01:00
|
|
|
def _return_response(self, request: HttpRequest, data):
|
|
|
|
""" Returns all important data into a response object
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
data (dict): The serialized data
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
response (JsonResponse): The response to be returned
|
|
|
|
"""
|
|
|
|
response = self.response_body_base
|
|
|
|
next_page = self.page_number + 1
|
|
|
|
next_page = next_page if next_page in self.serializer.paginator.page_range else None
|
|
|
|
if next_page is not None:
|
|
|
|
next_url = request.build_absolute_uri(
|
|
|
|
request.path + f"?rpp={self.rpp}&p={next_page}"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
next_url = None
|
|
|
|
response["rpp"] = self.rpp
|
|
|
|
response["p"] = self.page_number
|
|
|
|
response["next"] = next_url
|
|
|
|
response["results"] = data
|
|
|
|
return JsonResponse(response)
|
|
|
|
|
2022-01-25 09:29:14 +01:00
|
|
|
|
|
|
|
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():
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response("Permission not granted", 403)
|
2022-01-25 09:29:14 +01:00
|
|
|
try:
|
|
|
|
obj = Intervention.objects.get(
|
|
|
|
id=id,
|
|
|
|
users__in=[self.user]
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response(e)
|
2022-01-25 09:29:14 +01:00
|
|
|
|
|
|
|
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
|
2022-01-26 09:16:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AbstractModelShareAPIView(AbstractAPIView):
|
|
|
|
model = None
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest, id):
|
|
|
|
""" Performs the GET request handling
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The object's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
users = self._get_shared_users_of_object(id)
|
2022-02-18 13:52:27 +01:00
|
|
|
teams = self._get_shared_teams_of_object(id)
|
2022-01-26 09:16:37 +01:00
|
|
|
except Exception as e:
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response(e)
|
2022-01-26 09:16:37 +01:00
|
|
|
|
|
|
|
data = {
|
|
|
|
"users": [
|
|
|
|
user.username for user in users
|
2022-02-18 13:52:27 +01:00
|
|
|
],
|
|
|
|
"teams": [
|
|
|
|
{
|
|
|
|
"id": team.id,
|
|
|
|
"name": team.name,
|
|
|
|
}
|
|
|
|
for team in teams
|
|
|
|
],
|
2022-01-26 09:16:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
|
|
def put(self, request: HttpRequest, id):
|
|
|
|
""" Performs the PUT request handling
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The incoming request
|
|
|
|
id (str): The object's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
success = self._process_put_body(request.body, id)
|
|
|
|
except Exception as e:
|
2022-02-16 11:38:24 +01:00
|
|
|
return self._return_error_response(e)
|
2022-01-26 09:16:37 +01:00
|
|
|
data = {
|
|
|
|
"success": success,
|
|
|
|
}
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
|
|
def _check_user_has_shared_access(self, obj):
|
|
|
|
""" Raises a PermissionError if user has no shared access
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (BaseObject): The object
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
is_shared = obj.is_shared_with(self.user)
|
|
|
|
if not is_shared:
|
|
|
|
raise PermissionError(DATA_UNSHARED)
|
|
|
|
|
|
|
|
def _get_shared_users_of_object(self, id) -> QuerySet:
|
|
|
|
""" Check permissions and get the users
|
|
|
|
|
|
|
|
Args:
|
|
|
|
id (str): The object's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
users (QuerySet)
|
|
|
|
"""
|
|
|
|
obj = self.model.objects.get(
|
|
|
|
id=id
|
|
|
|
)
|
|
|
|
self._check_user_has_shared_access(obj)
|
|
|
|
users = obj.shared_users
|
|
|
|
return users
|
|
|
|
|
2022-02-18 13:52:27 +01:00
|
|
|
def _get_shared_teams_of_object(self, id) -> QuerySet:
|
|
|
|
""" Check permissions and get the teams
|
|
|
|
|
|
|
|
Args:
|
|
|
|
id (str): The object's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
users (QuerySet)
|
|
|
|
"""
|
|
|
|
obj = self.model.objects.get(
|
|
|
|
id=id
|
|
|
|
)
|
|
|
|
self._check_user_has_shared_access(obj)
|
|
|
|
teams = obj.shared_teams
|
|
|
|
return teams
|
|
|
|
|
2022-01-26 09:16:37 +01:00
|
|
|
def _process_put_body(self, body: bytes, id: str):
|
|
|
|
""" Reads the body data, performs validity checks and sets the new users
|
|
|
|
|
|
|
|
Args:
|
|
|
|
body (bytes): The request.body
|
|
|
|
id (str): The object's id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
success (bool)
|
|
|
|
"""
|
|
|
|
obj = self.model.objects.get(id=id)
|
|
|
|
self._check_user_has_shared_access(obj)
|
|
|
|
|
2022-02-18 13:52:27 +01:00
|
|
|
content = json.loads(body.decode("utf-8"))
|
|
|
|
new_users = content.get("users", [])
|
2022-01-26 09:16:37 +01:00
|
|
|
if len(new_users) == 0:
|
|
|
|
raise ValueError("Shared user list must not be empty!")
|
2022-02-18 13:52:27 +01:00
|
|
|
new_teams = content.get("teams", [])
|
2022-01-26 09:16:37 +01:00
|
|
|
|
|
|
|
# Eliminate duplicates
|
|
|
|
new_users = list(dict.fromkeys(new_users))
|
2022-02-18 13:52:27 +01:00
|
|
|
new_teams = list(dict.fromkeys(new_teams))
|
2022-01-26 09:16:37 +01:00
|
|
|
|
|
|
|
# Make sure each of these names exist as a user
|
|
|
|
new_users_objs = []
|
|
|
|
for user in new_users:
|
|
|
|
new_users_objs.append(User.objects.get(username=user))
|
2022-01-27 14:48:42 +01:00
|
|
|
|
2022-02-18 13:52:27 +01:00
|
|
|
# Make sure each of these names exist as a user
|
|
|
|
new_teams_objs = []
|
|
|
|
for team_name in new_teams:
|
|
|
|
new_teams_objs.append(Team.objects.get(name=team_name))
|
|
|
|
|
2023-09-13 09:49:40 +02:00
|
|
|
if self.user.is_default_group_only():
|
2022-01-27 14:48:42 +01:00
|
|
|
# Default only users are not allowed to remove other users from having access. They can only add new ones!
|
|
|
|
new_users_to_be_added = User.objects.filter(
|
|
|
|
username__in=new_users
|
|
|
|
).exclude(
|
|
|
|
id__in=obj.shared_users
|
|
|
|
)
|
|
|
|
new_users_objs = obj.shared_users.union(new_users_to_be_added)
|
2022-02-18 13:52:27 +01:00
|
|
|
|
|
|
|
new_teams_to_be_added = Team.objects.filter(
|
|
|
|
name__in=new_teams
|
|
|
|
).exclude(
|
|
|
|
id__in=obj.shared_teams
|
|
|
|
)
|
|
|
|
new_teams_objs = obj.shared_teams.union(new_teams_to_be_added)
|
|
|
|
|
2022-02-18 11:02:40 +01:00
|
|
|
obj.share_with_user_list(new_users_objs)
|
2022-02-18 13:52:27 +01:00
|
|
|
obj.share_with_team_list(new_teams_objs)
|
2022-01-26 09:16:37 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class InterventionAPIShareView(AbstractModelShareAPIView):
|
|
|
|
model = Intervention
|
|
|
|
|
|
|
|
|
|
|
|
class EcoAccountAPIShareView(AbstractModelShareAPIView):
|
|
|
|
model = EcoAccount
|
|
|
|
|
|
|
|
|
|
|
|
class EmaAPIShareView(AbstractModelShareAPIView):
|
2022-01-27 11:37:38 +01:00
|
|
|
model = Ema
|