Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for DRF API access #132

Merged
merged 6 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions backend/apps/ifc_validation/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from apps.ifc_validation_models.models import ValidationOutcome


class BaseSerializer(serializers.HyperlinkedModelSerializer):
class BaseSerializer(serializers.ModelSerializer):

def get_field_names(self, declared_fields, info):

Expand All @@ -28,7 +28,8 @@ class Meta:
model = ValidationRequest
fields = '__all__'
show = ["public_id", "model_public_id"]
hide = ["id", "model"]
hide = ["id"]
read_only_fields = ['size', 'created_by']


class ValidationTaskSerializer(BaseSerializer):
Expand Down
82 changes: 62 additions & 20 deletions backend/apps/ifc_validation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,46 @@ class ValidationRequestDetailAPIView(APIView):
parser_classes = (MultiPartParser, FormParser)
serializer_class = ValidationRequestSerializer

def get_for_user_by_public_id(self, user_id, public_id):

user_requests = ValidationRequest.objects.filter(created_by__id=user_id, deleted=False)
instance = [r for r in user_requests if r.public_id == public_id]
rw-bsi marked this conversation as resolved.
Show resolved Hide resolved

return instance[0] if instance else None

@extend_schema(operation_id='validationrequest_get')
def get(self, request, id, *args, **kwargs):

"""
Retrieves a single Validation Request by Id.
Retrieves a single Validation Request by id.
"""

logger.info('API request - User IP: %s Request Method: %s Request URL: %s Content-Length: %s' % (get_client_ip_address(request), request.method, request.path, request.META.get('CONTENT_LENGTH')))

instance = ValidationRequest.objects.filter(created_by__id=request.user.id, deleted=False, id=id).first()

instance = self.get_for_user_by_public_id(user_id=request.user.id, public_id=id)
if instance:
serializer = ValidationRequestSerializer(instance)
return Response(serializer.data, status=status.HTTP_200_OK)
else:
data = {'message': f"Validation Request with id='{id}' does not exist for user with id='{request.user.id}'."}
data = {'message': f"Validation Request with public_id={id} does not exist for user with id={request.user.id}."}
return Response(data, status=status.HTTP_404_NOT_FOUND)

@extend_schema(operation_id='validationrequest_delete')
def delete(self, request, id, *args, **kwargs):

"""
Deletes an IFC Validation Request instance.
Deletes an IFC Validation Request instance by id.
"""

logger.info('API request - User IP: %s Request Method: %s Request URL: %s Content-Length: %s' % (get_client_ip_address(request), request.method, request.path, request.META.get('CONTENT_LENGTH')))

instance = ValidationRequest.objects.filter(created_by__id=request.user.id, deleted=False).filter(id=id).first()
instance = self.get_for_user_by_public_id(user_id=request.user.id, public_id=id)
if instance:
instance.delete()
data = {'message': f"Validation Request with id='{id}' was deleted successfully."}
data = {'message': f"Validation Request with public_id={id} was deleted successfully."}
return Response(data, status=status.HTTP_204_NO_CONTENT)
else:
data = {'message': f"Validation Request with id='{id}' does not exist."}
data = {'message': f"Validation Request with public_id={id} does not exist."}
return Response(data, status=status.HTTP_404_NOT_FOUND)


Expand All @@ -88,8 +94,8 @@ def get(self, request, *args, **kwargs):

logger.info('API request - User IP: %s Request Method: %s Request URL: %s Content-Length: %s' % (get_client_ip_address(request), request.method, request.path, request.META.get('CONTENT_LENGTH')))

all_user_instances = ValidationRequest.objects.filter(created_by__id=request.user.id, deleted=False)
serializer = self.serializer_class(all_user_instances, many=True)
user_requests = ValidationRequest.objects.filter(created_by__id=request.user.id, deleted=False)
serializer = self.serializer_class(user_requests, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

@extend_schema(operation_id='validationrequest_create')
Expand Down Expand Up @@ -157,6 +163,13 @@ class ValidationTaskDetailAPIView(APIView):
permission_classes = [IsAuthenticated]
serializer_class = ValidationTaskSerializer

def get_for_user_by_public_id(self, user_id, public_id):

user_tasks = ValidationTask.objects.filter(request__created_by__id=user_id, request__deleted=False)
instance = [t for t in user_tasks if t.public_id == public_id]

return instance[0] if instance else None

@extend_schema(operation_id='validationtask_get')
def get(self, request, id, *args, **kwargs):

Expand All @@ -166,12 +179,12 @@ def get(self, request, id, *args, **kwargs):

logger.info('API request - User IP: %s Request Method: %s Request URL: %s Content-Length: %s' % (get_client_ip_address(request), request.method, request.path, request.META.get('CONTENT_LENGTH')))

instance = ValidationTask.objects.filter(request__created_by__id=request.user.id, request__deleted=False, id=id).first()
instance = self.get_for_user_by_public_id(request.user.id, id)
if instance:
serializer = ValidationTaskSerializer(instance)
return Response(serializer.data, status=status.HTTP_200_OK)
else:
data = {'message': f"Validation Task with id='{id}' does not exist for user with id='{request.user.id}'."}
data = {'message': f"Validation Task with public_id={id} does not exist for user with id={request.user.id}."}
return Response(data, status=status.HTTP_404_NOT_FOUND)


Expand All @@ -186,13 +199,22 @@ class ValidationTaskListAPIView(APIView):
def get(self, request, *args, **kwargs):

"""
Returns a list of all Validation Tasks.
Returns a list of all Validation Tasks, optionally filtered by request_public_id.
"""

logger.info('API request - User IP: %s Request Method: %s Request URL: %s Content-Length: %s' % (get_client_ip_address(request), request.method, request.path, request.META.get('CONTENT_LENGTH')))

all_user_instances = ValidationTask.objects.filter(request__created_by__id=request.user.id, request__deleted=False)
serializer = self.serializer_class(all_user_instances, many=True)
user_tasks = ValidationTask.objects.filter(request__created_by__id=request.user.id, request__deleted=False)

# parse query arguments
request_public_id = self.request.query_params.get('request_public_id', '').lower()
request_public_ids = [id for id in (request_public_id.split(',') if request_public_id else [])]

# apply filter(s)
if request_public_ids:
user_tasks = [t for t in user_tasks if t.request_public_id in request_public_ids]

serializer = self.serializer_class(user_tasks, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)


Expand All @@ -203,6 +225,13 @@ class ValidationOutcomeDetailAPIView(APIView):
permission_classes = [IsAuthenticated]
serializer_class = ValidationOutcomeSerializer

def get_for_user_by_public_id(self, user_id, public_id):

user_outcomes = ValidationOutcome.objects.filter(validation_task__request__created_by__id=user_id, validation_task__request__deleted=False)
instance = [o for o in user_outcomes if o.public_id == public_id]

return instance[0] if instance else None

@extend_schema(operation_id='validationoutcome_get')
def get(self, request, id, *args, **kwargs):

Expand All @@ -212,12 +241,12 @@ def get(self, request, id, *args, **kwargs):

logger.info('API request - User IP: %s Request Method: %s Request URL: %s Content-Length: %s' % (get_client_ip_address(request), request.method, request.path, request.META.get('CONTENT_LENGTH')))

instance = ValidationOutcome.objects.filter(validation_task__request__created_by__id=request.user.id, validation_task__request__deleted=False, id=id).first()
instance = self.get_for_user_by_public_id(request.user.id, id)
if instance:
serializer = ValidationOutcomeSerializer(instance)
return Response(serializer.data, status=status.HTTP_200_OK)
else:
data = {'message': f"Validation Outcome with id='{id}' does not exist for user with id='{request.user.id}'."}
data = {'message': f"Validation Outcome with public_id={id} does not exist for user with id={request.user.id}."}
return Response(data, status=status.HTTP_404_NOT_FOUND)


Expand All @@ -232,12 +261,25 @@ class ValidationOutcomeListAPIView(APIView):
def get(self, request, *args, **kwargs):

"""
Returns a list of all Validation Outcomes.
Returns a list of all Validation Outcomes, optionally filtered by request_public_id or validation_task_public_id.
"""

logger.info('API request - User IP: %s Request Method: %s Request URL: %s Content-Length: %s' % (get_client_ip_address(request), request.method, request.path, request.META.get('CONTENT_LENGTH')))

all_user_instances = ValidationOutcome.objects.filter(validation_task__request__created_by__id=request.user.id, validation_task__request__deleted=False)
serializer = self.serializer_class(all_user_instances, many=True)
user_outcomes = ValidationOutcome.objects.filter(validation_task__request__created_by__id=request.user.id, validation_task__request__deleted=False)

# parse query arguments
request_public_id = self.request.query_params.get('request_public_id', '').lower()
task_public_id = self.request.query_params.get('validation_task_public_id', '').lower()
request_public_ids = [id for id in (request_public_id.split(',') if request_public_id else [])]
task_public_ids = [id for id in (task_public_id.split(',') if task_public_id else [])]

# apply filter(s)
if request_public_ids:
user_outcomes = [o for o in user_outcomes if o.validation_task.request_public_id in request_public_ids]
if task_public_ids:
user_outcomes = [o for o in user_outcomes if o.validation_task_public_id in task_public_ids]

serializer = self.serializer_class(user_outcomes, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

8 changes: 6 additions & 2 deletions backend/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,15 @@
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
]
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_PERMISSION_CLASSES':(
'rest_framework.permissions.IsAuthenticated',
),
}

SPECTACULAR_SETTINGS = {
'TITLE': 'IFC Validation Service API',
'TITLE': 'IFC Validation Service API (PREVIEW)',
'DESCRIPTION': 'API for the buildingSMART Validation Service',
'VERSION': os.environ.get("VERSION", "UNDEFINED"),
'SERVE_INCLUDE_SCHEMA': False,
Expand Down
Loading