Skip to content

Commit

Permalink
In Progress - List contents of input_file and output_file
Browse files Browse the repository at this point in the history
  • Loading branch information
vinulw committed Dec 2, 2024
1 parent f642000 commit 1f5f98c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/server/oasisapi/analyses/v2_api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
RUN_MODE_PARAM,
SUBTASK_STATUS_PARAM,
SUBTASK_SLUG_PARAM,
LIST_FILE_PARAM,
)


Expand Down Expand Up @@ -419,6 +420,17 @@ def input_file(self, request, pk=None, version=None):
"""
return handle_related_file(self.get_object(), 'input_file', request, ['application/x-gzip', 'application/gzip', 'application/x-tar', 'application/tar'])


@swagger_auto_schema(methods=['get'], manual_parameters=[LIST_FILE_PARAM,])
@action(methods=['get'], detail=True)
def list_input_files(self, request, pk=None, version=None):
"""
get:
List the files in `input_file`.
"""
return handle_related_file(self.get_object(), 'input_file', request, ['application/x-gzip', 'application/gzip', 'application/x-tar', 'application/tar'])


@swagger_auto_schema(methods=['get'], responses={200: FILE_RESPONSE})
@action(methods=['get'], detail=True)
def lookup_errors_file(self, request, pk=None, version=None):
Expand Down Expand Up @@ -503,6 +515,15 @@ def output_file(self, request, pk=None, version=None):
"""
return handle_related_file(self.get_object(), 'output_file', request, ['application/x-gzip', 'application/gzip', 'application/x-tar', 'application/tar'])

@swagger_auto_schema(methods=['get'], manual_parameters=[LIST_FILE_PARAM,])
@action(methods=['get'], detail=True)
def list_output_files(self, request, pk=None, version=None):
"""
get:
List the files in `output_file`.
"""
return handle_related_file(self.get_object(), 'output_file', request, ['application/x-gzip', 'application/gzip', 'application/x-tar', 'application/tar'])

@requires_sql_reader
@swagger_auto_schema(methods=['get'], responses={200: NestedRelatedFileSerializer})
@action(methods=['get'], detail=True)
Expand Down
9 changes: 9 additions & 0 deletions src/server/oasisapi/files/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from io import BytesIO

import pandas as pd
import tarfile
from uuid import uuid4

from django.conf import settings
Expand All @@ -25,6 +26,14 @@ def related_file_to_df(RelatedFile):
return pd.read_csv(BytesIO(RelatedFile.read()))


def list_tar_file(RelatedFile):
if not RelatedFile:
return None
# Need to verify this is a tar file
tarf = tarfile.open(fileobj=BytesIO(RelatedFile.read()), mode='r')
return tarf.getnames()


def random_file_name(instance, filename):
if getattr(instance, "store_as_filename", False):
return filename
Expand Down
12 changes: 10 additions & 2 deletions src/server/oasisapi/files/v1_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

from django.conf import settings
from django.core.files import File
from django.http import StreamingHttpResponse, Http404
from django.http import StreamingHttpResponse, Http404, JsonResponse
from rest_framework.response import Response
from rest_framework.exceptions import ValidationError

from oasis_data_manager.df_reader.config import get_df_reader
from oasis_data_manager.df_reader.exceptions import InvalidSQLException
from ..models import RelatedFile
from ..models import RelatedFile, list_tar_file
from .serializers import RelatedFileSerializer, EXPOSURE_ARGS
from ...permissions.group_auth import verify_user_is_in_obj_groups

Expand Down Expand Up @@ -45,9 +45,12 @@ def _handle_get_related_file(parent, field, request):
if not f:
raise Http404()


verify_user_is_in_obj_groups(request.user, f, 'You do not have permission to read this file')
file_format = request.GET.get('file_format', None)

list_files = request.query_params.get('file_mode', False)

if 'converted' in request.GET:
if not (f.converted_file and f.conversion_state == RelatedFile.ConversionState.DONE):
raise Http404()
Expand Down Expand Up @@ -86,6 +89,11 @@ def _handle_get_related_file(parent, field, request):
response['Content-Disposition'] = 'attachment; filename="{}{}"'.format(download_name, '.csv')
return response

if list_files:
files = list_tar_file(f)
# todo: change this to a proper response
return JsonResponse({'files': files})

# Original Fallback method - Reutrn data 'as is'
response = StreamingHttpResponse(_get_chunked_content(file_obj), content_type=f.content_type)
response['Content-Disposition'] = 'attachment; filename="{}"'.format(download_name)
Expand Down
9 changes: 9 additions & 0 deletions src/server/oasisapi/schemas/custom_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'SUBTASK_STATUS_PARAM',
'SUBTASK_SLUG_PARAM',
'FILE_VALIDATION_PARAM',
'LIST_FILE_PARAM',
]

from drf_yasg import openapi
Expand Down Expand Up @@ -109,3 +110,11 @@
description="Validate OED files on upload, default `True`",
type=openapi.TYPE_BOOLEAN,
)

LIST_FILE_PARAM = openapi.Parameter(
'file_mode',
openapi.IN_QUERY,
required=False,
description="List file, if directory or tar, list contents.",
type=openapi.TYPE_BOOLEAN,
)

0 comments on commit 1f5f98c

Please sign in to comment.