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

Modify viewHIstory API #1596

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions FusionIIIT/applications/filetracking/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
DraftFileView,
CreateDraftFile,
GetDesignationsView,
CreateArchiveFile,
ArchiveFileView
)

urlpatterns = [
Expand All @@ -20,5 +22,7 @@
url(r'^forwardfile/(?P<file_id>\d+)/$', ForwardFileView.as_view(), name='forward_file'),
url(r'^draft/$', DraftFileView.as_view(), name='view_drafts'),
url(r'^createdraft/$', CreateDraftFile.as_view(), name='create_draft'),
url(r'^createarchive/$', CreateArchiveFile.as_view(), name='archive_file'),
url(r'^archive/$', ArchiveFileView.as_view(), name='view_archived'),
url(r'^designations/(?P<username>\w+)/$', GetDesignationsView.as_view(), name='get_designations'),
]
61 changes: 53 additions & 8 deletions FusionIIIT/applications/filetracking/api/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logging
from venv import logger
from django.forms import ValidationError
from django.contrib.auth.models import User
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status, permissions
from rest_framework.authentication import TokenAuthentication
from ..models import File, Tracking
from ..sdk.methods import create_draft, create_file, view_drafts, view_file, delete_file, view_inbox, view_outbox, view_history, forward_file, get_designations
from applications.globals.models import Designation
from ..sdk.methods import create_draft, create_file, view_drafts, view_file, delete_file, view_inbox, view_outbox, view_history, forward_file, get_designations, archive_file, view_archived

class CreateFileView(APIView):
authentication_classes = [TokenAuthentication]
Expand All @@ -21,11 +23,13 @@ def post(self, request):
subject = request.data.get('subject')
description = request.data.get('description')

uploaded_file = request.FILES.get('file') # Get the file if provided

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)

file_id = create_file(uploader=current_user, uploader_designation=current_designation,
receiver=receiver_username, receiver_designation=receiver_designation, subject=subject, description=description)
receiver=receiver_username, receiver_designation=receiver_designation, subject=subject, description=description, attached_file=uploaded_file)

return Response({'file_id': file_id}, status=status.HTTP_201_CREATED)
except Exception as e:
Expand Down Expand Up @@ -145,15 +149,21 @@ def get(self, request, file_id):
Returns:
rest_framework.response.Response: JSON response containing serialized tracking history.
"""

try:
history = view_history(file_id)
return Response(history)
tracking_array = []
histories = view_history(file_id)
for history in histories:
temp_obj_action = history;
temp_obj_action['receiver_id'] = User.objects.get(id=history['receiver_id']).username
temp_obj_action['receive_design'] = Designation.objects.get(id=history['receive_design']).name
tracking_array.append(temp_obj_action)

return Response(tracking_array)
except Tracking.DoesNotExist:
return Response({'error': f'File with ID {file_id} not found.'}, status=404)
return Response({'error': f'File with ID {file_id} not found.'}, status=404)
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return Response({'error': 'Internal server error.'}, status=500)
logger.error(f"An unexpected error occurred: {e}")
return Response({'error': 'Internal server error.'}, status=500)

class ForwardFileView(APIView):
# # # Authentication and permission classes (adjust based on your needs)
Expand Down Expand Up @@ -235,6 +245,41 @@ def get(self, request):
return Response(draft_files, status=status.HTTP_200_OK)
except Exception as e:
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

class ArchiveFileView(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [permissions.IsAuthenticated]

def get(self, request):
username = request.query_params.get('username')
designation = request.query_params.get('designation', '')
src_module = request.query_params.get('src_module')
try:
archived_files = view_archived(username, designation, src_module)
return Response(archived_files, status=status.HTTP_200_OK)
except Exception as e:
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class CreateArchiveFile(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [permissions.IsAuthenticated]

def post(self, request):
file_id = request.data.get('file_id', None)

if file_id is None:
return Response({'error': 'Missing file_id'}, status=status.HTTP_400_BAD_REQUEST)

try:
success = archive_file(file_id)
if success:
return Response({'success': True})
else:
return Response({'error': 'File does not exist'}, status=status.HTTP_404_NOT_FOUND)

except Exception as e:
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)



Expand Down
Loading