Exemple #1
0
    def post(self, request, file_mgr_name, system_id, file_path):
        if file_mgr_name == AgaveFileManager.NAME \
            or file_mgr_name == 'public':
            if not request.user.is_authenticated():
                return HttpResponseForbidden('Log in required')

            agave_client = request.user.agave_oauth.client
            fm = AgaveFileManager(agave_client=agave_client)
            if request.FILES:
                upload_file = request.FILES['file']
                upload_dir = file_path

                relative_path = request.POST.get('relative_path', None)
                if relative_path:
                    # user uploaded a folder structure; ensure path exists
                    upload_dir = os.path.join(file_path, os.path.dirname(relative_path))
                    BaseFileResource.ensure_path(agave_client, system_id, upload_dir)

                try:
                    result = fm.upload(system_id, upload_dir, upload_file)
                except HTTPError as e:
                    logger.error(e.response.text)
                    return HttpResponseBadRequest(e.response.text)

            return JsonResponse({'status': 'ok'})

        return HttpResponseBadRequest("Unsupported operation")
Exemple #2
0
    def post(self, request, file_mgr_name, system_id, file_path):
        if file_mgr_name == AgaveFileManager.NAME \
            or file_mgr_name == 'public':
            if not request.user.is_authenticated:
                return HttpResponseForbidden('Login required')

            agave_client = request.user.agave_oauth.client
            fm = AgaveFileManager(agave_client=agave_client)
            if request.FILES:
                upload_file = request.FILES['file']
                upload_dir = file_path

                relative_path = request.POST.get('relative_path', None)
                if relative_path:
                    # user uploaded a folder structure; ensure path exists
                    upload_dir = os.path.join(file_path,
                                              os.path.dirname(relative_path))
                    BaseFileResource.ensure_path(agave_client, system_id,
                                                 upload_dir)
                    metrics.info('Data Depot',
                                 extra={
                                     'user':
                                     request.user.username,
                                     'sessionId':
                                     getattr(request.session, 'session_key',
                                             ''),
                                     'operation':
                                     'data_depot_folder_upload',
                                     'info': {
                                         'filePath':
                                         file_path,
                                         'relativePath':
                                         os.path.dirname(relative_path),
                                         'systemId':
                                         system_id,
                                         'uploadDir':
                                         upload_dir
                                     }
                                 })
                try:
                    result = fm.upload(system_id, upload_dir, upload_file)
                    metrics.info('Data Depot',
                                 extra={
                                     'user':
                                     request.user.username,
                                     'sessionId':
                                     getattr(request.session, 'session_key',
                                             ''),
                                     'operation':
                                     'data_depot_file_upload',
                                     'info': {
                                         'systemId': system_id,
                                         'uploadDir': upload_dir,
                                         'uploadFile': upload_file
                                     }
                                 })
                    result['system'] = result['systemId']
                    result_file = BaseFileResource(agave_client, **result)
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot',
                        Notification.OPERATION: 'data_depot_file_upload',
                        Notification.STATUS: Notification.SUCCESS,
                        Notification.USER: request.user.username,
                        Notification.MESSAGE: 'File Upload was successful.',
                        Notification.EXTRA: result_file.to_dict()
                    }
                    Notification.objects.create(**event_data)
                except HTTPError as e:
                    logger.error(e.response.text)
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot',
                        Notification.OPERATION: 'data_depot_file_upload',
                        Notification.STATUS: Notification.ERROR,
                        Notification.USER: request.user.username,
                        Notification.MESSAGE:
                        'There was an error uploading one or more file(s).',
                        Notification.EXTRA: {
                            'system': system_id,
                            'file_path': file_path
                        }
                    }
                    Notification.objects.create(**event_data)
                    return HttpResponseBadRequest(e.response.text)

            return JsonResponse({'status': 'ok'})

        return HttpResponseBadRequest("Unsupported operation")