Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
fix request files upload
Browse files Browse the repository at this point in the history
  • Loading branch information
anatskiy committed Jun 12, 2018
1 parent a557269 commit 7f9d796
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 167 deletions.
48 changes: 47 additions & 1 deletion request/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
CsrfExemptSessionAuthentication,
StandardResultsSetPagination,
)
from .models import Request
from .models import Request, FileRequest
from .serializers import RequestSerializer, RequestFileSerializer

User = get_user_model()
Expand Down Expand Up @@ -276,6 +276,52 @@ def get_files(self, request, pk=None):
serializer = RequestFileSerializer(files, many=True)
return Response(serializer.data)

@action(methods=['post'], detail=False,
authentication_classes=[CsrfExemptSessionAuthentication])
def upload_files(self, request):
file_ids = []

if not any(request.FILES):
return JsonResponse({
'success': False,
'message': 'No files provided.'
}, status=400)

for file in request.FILES.getlist('files'):
f = FileRequest(name=file.name, file=file)
f.save()
file_ids.append(f.id)

return JsonResponse({'success': True, 'fileIds': file_ids})

@action(methods=['get'], detail=False)
def get_files_after_upload(self, request):
file_ids = json.loads(request.query_params.get('file_ids', '[]'))
error = ''
data = []

try:
files = [f for f in FileRequest.objects.all() if f.id in file_ids]
data = [
{
'id': file.id,
'name': file.name,
'size': file.file.size,
'path': settings.MEDIA_URL + file.file.name,
}
for file in files
]

except Exception as e:
error = 'Could not get the attached files.'
logger.exception(e)

return JsonResponse({
'success': not error,
'error': error,
'data': data,
})

@action(methods=['get'], detail=True)
def download_deep_sequencing_request(self, request, pk=None): # pragma: no cover
""" Generate a deep sequencing request form in PDF. """
Expand Down
5 changes: 1 addition & 4 deletions static/main-hub/app/view/requests/RequestWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ Ext.define('MainHub.view.requests.RequestWindow', {
xtype: 'filegridfield',
fieldLabel: 'Files',
store: 'requestFilesStore',
uploadFileUrl: 'request/upload_files/',
getFileUrl: 'request/get_files/'
// uploadFileUrl: 'api/requests/upload_files/',
// getFileUrl: 'api/requests/get_files/'
uploadFileUrl: 'api/requests/upload_files/'

}
]
Expand Down

0 comments on commit 7f9d796

Please sign in to comment.