From a4e01b3bbb443b50bd661eb956a1504f62dbaa03 Mon Sep 17 00:00:00 2001 From: Divyansh Tripathi <55395696+theoden42@users.noreply.github.com> Date: Sun, 3 Mar 2024 19:13:23 +0530 Subject: [PATCH] add rest api for create file and view file --- .../applications/filetracking/api/views.py | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/FusionIIIT/applications/filetracking/api/views.py b/FusionIIIT/applications/filetracking/api/views.py index 6694e749f..c4bb01189 100644 --- a/FusionIIIT/applications/filetracking/api/views.py +++ b/FusionIIIT/applications/filetracking/api/views.py @@ -2,25 +2,47 @@ from rest_framework.response import Response from rest_framework import status, permissions from rest_framework.authentication import TokenAuthentication +from ..models import File from ..sdk.methods import create_file, view_file, delete_file, view_inbox, view_outbox, view_history, forward_file, get_designations - class CreateFileView(APIView): - #authentication_classes = [TokenAuthentication] - #permission_classes = [permissions.IsAuthenticated] + authentication_classes = [TokenAuthentication] + permission_classes = [permissions.IsAuthenticated] - def post(self, request, *args, **kwargs): - file_id = create_file(**request.data) - return Response({'file_id': file_id}, status=status.HTTP_201_CREATED) + def post(self, request): + try: + current_user = request.user.username + current_designation = request.data.get('designation') + receiver_username = request.data.get('receiver_username') + receiver_designation = request.data.get('receiver_designation') + subject = request.data.get('subject') + description = request.data.get('description') + if None in [current_designation, receiver_username, receiver_designation, subject, description]: + return Response({'error': 'One or more required fields are missing.'}, status=status.HTTP_400_BAD_REQUEST) -class ViewFileView(APIView): - #authentication_classes = [TokenAuthentication] - #permission_classes = [permissions.IsAuthenticated] + file_id = create_file(uploader=current_user, uploader_designation=current_designation, + receiver=receiver_username, receiver_designation=receiver_designation, subject=subject, description=description) - def get(self, request, file_id, *args, **kwargs): - file_details = view_file(int(file_id)) - return Response(file_details) + return Response({'file_id': file_id}, status=status.HTTP_201_CREATED) + except Exception as e: + return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) + + +class ViewFileView(APIView): + authentication_classes = [TokenAuthentication] + permission_classes = [permissions.IsAuthenticated] + + def get(self, request, file_id): + try: + file_details = view_file(int(file_id)) + return Response(file_details, status=status.HTTP_200_OK) + except ValueError: + return Response({'error': 'Invalid file ID format.'}, status=status.HTTP_400_BAD_REQUEST) + except File.DoesNotExist: + return Response({'error': 'File not found.'}, status=status.HTTP_404_NOT_FOUND) + except Exception as e: + return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) class DeleteFileView(APIView):