Skip to content

Commit

Permalink
add rest api for create file and view file
Browse files Browse the repository at this point in the history
  • Loading branch information
theoden42 committed Mar 3, 2024
1 parent 5c6ab07 commit a4e01b3
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions FusionIIIT/applications/filetracking/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a4e01b3

Please sign in to comment.