Compare commits
No commits in common. "c7382f1e54f35e82bfa49f38865725f9af0955c3" and "3c9eed894a778a915f626f78971a26b483d436d8" have entirely different histories.
c7382f1e54
...
3c9eed894a
@ -36,12 +36,7 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
|
|||||||
"""
|
"""
|
||||||
response = self._run_get_request(url)
|
response = self._run_get_request(url)
|
||||||
content = json.loads(response.content)
|
content = json.loads(response.content)
|
||||||
self.assertIn("rpp", content)
|
geojson = content[str(obj.id)]
|
||||||
self.assertIn("p", content)
|
|
||||||
self.assertIn("next", content)
|
|
||||||
self.assertIn("results", content)
|
|
||||||
paginated_content = content["results"]
|
|
||||||
geojson = paginated_content[str(obj.id)]
|
|
||||||
self.assertEqual(response.status_code, 200, msg=response.content)
|
self.assertEqual(response.status_code, 200, msg=response.content)
|
||||||
return geojson
|
return geojson
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ from abc import abstractmethod
|
|||||||
|
|
||||||
from django.contrib.gis import geos
|
from django.contrib.gis import geos
|
||||||
from django.contrib.gis.geos import GEOSGeometry
|
from django.contrib.gis.geos import GEOSGeometry
|
||||||
from django.core.paginator import Paginator
|
|
||||||
|
|
||||||
from konova.utils.message_templates import DATA_UNSHARED
|
from konova.utils.message_templates import DATA_UNSHARED
|
||||||
|
|
||||||
@ -20,10 +19,6 @@ class AbstractModelAPISerializer:
|
|||||||
lookup = None
|
lookup = None
|
||||||
properties_data = None
|
properties_data = None
|
||||||
|
|
||||||
rpp = None
|
|
||||||
page_number = None
|
|
||||||
paginator = None
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
@ -85,12 +80,9 @@ class AbstractModelAPISerializer:
|
|||||||
Returns:
|
Returns:
|
||||||
serialized_data (dict)
|
serialized_data (dict)
|
||||||
"""
|
"""
|
||||||
entries = self.model.objects.filter(**self.lookup).order_by("id")
|
entries = self.model.objects.filter(**self.lookup)
|
||||||
self.paginator = Paginator(entries, self.rpp)
|
|
||||||
requested_entries = self.paginator.page(self.page_number)
|
|
||||||
|
|
||||||
serialized_data = {}
|
serialized_data = {}
|
||||||
for entry in requested_entries.object_list:
|
for entry in entries:
|
||||||
serialized_data[str(entry.id)] = self._model_to_geo_json(entry)
|
serialized_data[str(entry.id)] = self._model_to_geo_json(entry)
|
||||||
return serialized_data
|
return serialized_data
|
||||||
|
|
||||||
|
@ -367,9 +367,7 @@ class AbstractCompensationAPISerializerV1Mixin:
|
|||||||
"""
|
"""
|
||||||
actions = []
|
actions = []
|
||||||
for entry in actions_data:
|
for entry in actions_data:
|
||||||
action_types = [
|
action = entry["action"]
|
||||||
self._konova_code_from_json(e, CODELIST_COMPENSATION_ACTION_ID) for e in entry["action_types"]
|
|
||||||
]
|
|
||||||
action_details = [
|
action_details = [
|
||||||
self._konova_code_from_json(e, CODELIST_COMPENSATION_ACTION_DETAIL_ID) for e in entry["action_details"]
|
self._konova_code_from_json(e, CODELIST_COMPENSATION_ACTION_DETAIL_ID) for e in entry["action_details"]
|
||||||
]
|
]
|
||||||
@ -386,7 +384,7 @@ class AbstractCompensationAPISerializerV1Mixin:
|
|||||||
# If this exact data is already existing, we do not create it new. Instead put it's id in the list of
|
# If this exact data is already existing, we do not create it new. Instead put it's id in the list of
|
||||||
# entries, we will use to set the new actions
|
# entries, we will use to set the new actions
|
||||||
action_entry = obj.actions.filter(
|
action_entry = obj.actions.filter(
|
||||||
action_type__in=action_types,
|
action_type__atom_id=action,
|
||||||
amount=amount,
|
amount=amount,
|
||||||
unit=unit,
|
unit=unit,
|
||||||
comment=comment,
|
comment=comment,
|
||||||
@ -398,13 +396,13 @@ class AbstractCompensationAPISerializerV1Mixin:
|
|||||||
else:
|
else:
|
||||||
# Create and add id to list
|
# Create and add id to list
|
||||||
action_entry = CompensationAction.objects.create(
|
action_entry = CompensationAction.objects.create(
|
||||||
|
action_type=self._konova_code_from_json(action, CODELIST_COMPENSATION_ACTION_ID),
|
||||||
amount=amount,
|
amount=amount,
|
||||||
unit=unit,
|
unit=unit,
|
||||||
comment=comment,
|
comment=comment,
|
||||||
)
|
)
|
||||||
actions.append(action_entry.id)
|
actions.append(action_entry.id)
|
||||||
|
|
||||||
action_entry.action_type.set(action_types)
|
|
||||||
action_entry.action_type_details.set(action_details)
|
action_entry.action_type_details.set(action_details)
|
||||||
obj.actions.set(actions)
|
obj.actions.set(actions)
|
||||||
return obj
|
return obj
|
||||||
@ -440,9 +438,7 @@ class AbstractCompensationAPISerializerV1Mixin:
|
|||||||
"""
|
"""
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
"action_types": [
|
"action": self._konova_code_to_json(entry.action_type),
|
||||||
self._konova_code_to_json(action) for action in entry.action_type.all()
|
|
||||||
],
|
|
||||||
"action_details": [
|
"action_details": [
|
||||||
self._konova_code_to_json(detail) for detail in entry.action_type_details.all()
|
self._konova_code_to_json(detail) for detail in entry.action_type_details.all()
|
||||||
],
|
],
|
||||||
|
@ -21,6 +21,7 @@ class AbstractAPIViewV1(AbstractAPIView):
|
|||||||
""" Holds general serialization functions for API v1
|
""" Holds general serialization functions for API v1
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
serializer = None
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.lookup = {
|
self.lookup = {
|
||||||
@ -44,17 +45,11 @@ class AbstractAPIViewV1(AbstractAPIView):
|
|||||||
response (JsonResponse)
|
response (JsonResponse)
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
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
|
|
||||||
|
|
||||||
self.serializer.prepare_lookup(id, self.user)
|
self.serializer.prepare_lookup(id, self.user)
|
||||||
data = self.serializer.fetch_and_serialize()
|
data = self.serializer.fetch_and_serialize()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self._return_error_response(e, 500)
|
return self.return_error_response(e, 500)
|
||||||
return self._return_response(request, data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
def post(self, request: HttpRequest):
|
def post(self, request: HttpRequest):
|
||||||
""" Handles the POST request
|
""" Handles the POST request
|
||||||
@ -72,7 +67,7 @@ class AbstractAPIViewV1(AbstractAPIView):
|
|||||||
body = json.loads(body)
|
body = json.loads(body)
|
||||||
created_id = self.serializer.create_model_from_json(body, self.user)
|
created_id = self.serializer.create_model_from_json(body, self.user)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self._return_error_response(e, 500)
|
return self.return_error_response(e, 500)
|
||||||
return JsonResponse({"id": created_id})
|
return JsonResponse({"id": created_id})
|
||||||
|
|
||||||
def put(self, request: HttpRequest, id=None):
|
def put(self, request: HttpRequest, id=None):
|
||||||
@ -92,7 +87,7 @@ class AbstractAPIViewV1(AbstractAPIView):
|
|||||||
body = json.loads(body)
|
body = json.loads(body)
|
||||||
updated_id = self.serializer.update_model_from_json(id, body, self.user)
|
updated_id = self.serializer.update_model_from_json(id, body, self.user)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self._return_error_response(e, 500)
|
return self.return_error_response(e, 500)
|
||||||
return JsonResponse({"id": updated_id})
|
return JsonResponse({"id": updated_id})
|
||||||
|
|
||||||
def delete(self, request: HttpRequest, id=None):
|
def delete(self, request: HttpRequest, id=None):
|
||||||
@ -109,7 +104,7 @@ class AbstractAPIViewV1(AbstractAPIView):
|
|||||||
try:
|
try:
|
||||||
success = self.serializer.delete_entry(id, self.user)
|
success = self.serializer.delete_entry(id, self.user)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self._return_error_response(e, 500)
|
return self.return_error_response(e, 500)
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{
|
{
|
||||||
"success": success,
|
"success": success,
|
||||||
|
@ -31,22 +31,10 @@ class AbstractAPIView(View):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
user = None
|
user = None
|
||||||
serializer = None
|
|
||||||
rpp = 5 # Results per page default
|
|
||||||
page_number = 1 # Page number default
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.response_body_base = {
|
|
||||||
"rpp": None,
|
|
||||||
"p": None,
|
|
||||||
"next": None,
|
|
||||||
"results": None
|
|
||||||
}
|
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
@ -54,14 +42,13 @@ class AbstractAPIView(View):
|
|||||||
ksp_token = request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None)
|
ksp_token = request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None)
|
||||||
ksp_user = request.headers.get(KSP_USER_HEADER_IDENTIFIER, None)
|
ksp_user = request.headers.get(KSP_USER_HEADER_IDENTIFIER, None)
|
||||||
self.user = APIUserToken.get_user_from_token(ksp_token, ksp_user)
|
self.user = APIUserToken.get_user_from_token(ksp_token, ksp_user)
|
||||||
request.user = self.user
|
|
||||||
if not self.user.is_default_user():
|
if not self.user.is_default_user():
|
||||||
raise PermissionError("Default permissions required")
|
raise PermissionError("Default permissions required")
|
||||||
except PermissionError as e:
|
except PermissionError as e:
|
||||||
return self._return_error_response(e, 403)
|
return self.return_error_response(e, 403)
|
||||||
return super().dispatch(request, *args, **kwargs)
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
def _return_error_response(self, error, status_code=500):
|
def return_error_response(self, error, status_code=500):
|
||||||
""" Returns an error as JsonReponse
|
""" Returns an error as JsonReponse
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -81,31 +68,6 @@ class AbstractAPIView(View):
|
|||||||
status=status_code
|
status=status_code
|
||||||
)
|
)
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
class InterventionCheckAPIView(AbstractAPIView):
|
class InterventionCheckAPIView(AbstractAPIView):
|
||||||
|
|
||||||
@ -120,14 +82,14 @@ class InterventionCheckAPIView(AbstractAPIView):
|
|||||||
response (JsonResponse)
|
response (JsonResponse)
|
||||||
"""
|
"""
|
||||||
if not self.user.is_zb_user():
|
if not self.user.is_zb_user():
|
||||||
return self._return_error_response("Permission not granted", 403)
|
return self.return_error_response("Permission not granted", 403)
|
||||||
try:
|
try:
|
||||||
obj = Intervention.objects.get(
|
obj = Intervention.objects.get(
|
||||||
id=id,
|
id=id,
|
||||||
users__in=[self.user]
|
users__in=[self.user]
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self._return_error_response(e)
|
return self.return_error_response(e)
|
||||||
|
|
||||||
all_valid, check_details = self.run_quality_checks(obj)
|
all_valid, check_details = self.run_quality_checks(obj)
|
||||||
|
|
||||||
@ -199,7 +161,7 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
|||||||
try:
|
try:
|
||||||
users = self._get_shared_users_of_object(id)
|
users = self._get_shared_users_of_object(id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self._return_error_response(e)
|
return self.return_error_response(e)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"users": [
|
"users": [
|
||||||
@ -223,7 +185,7 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
|||||||
try:
|
try:
|
||||||
success = self._process_put_body(request.body, id)
|
success = self._process_put_body(request.body, id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self._return_error_response(e)
|
return self.return_error_response(e)
|
||||||
data = {
|
data = {
|
||||||
"success": success,
|
"success": success,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user