Exemple #1
0
    def put(self, request, file_mgr_name, system_id, file_path):
        if request.is_ajax():
            body = json.loads(request.body)
        else:
            body = request.POST.copy()

        if file_mgr_name == AgaveFileManager.NAME \
            or file_mgr_name == 'public' \
            or file_mgr_name == 'community' \
            or file_mgr_name == 'published':

            if not request.user.is_authenticated:
                if file_mgr_name in ['public', 'community', 'published']:
                    ag = get_user_model().objects.get(
                        username='******').agave_oauth.client
                else:
                    return HttpResponseForbidden('Login required')
            else:
                ag = request.user.agave_oauth.client

            fm = AgaveFileManager(agave_client=ag)
            action = body.get('action', '')
            if action == 'copy':
                try:
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot',
                        Notification.OPERATION: 'data_depot_copy',
                        Notification.STATUS: Notification.SUCCESS,
                        Notification.USER: request.user.username,
                        Notification.MESSAGE: 'Data was copied.',
                    }
                    if body.get('system') is None:
                        external = body.get('resource')
                        if external not in ['box', 'dropbox', 'googledrive']:
                            return HttpResponseBadRequest(
                                "External resource not available.")
                        if external == 'googledrive':
                            dest_file_id = body.get('id')
                        else:
                            dest_file_id = body.get('path')
                        external_resource_upload.apply_async(kwargs={
                            'username':
                            request.user.username,
                            'dest_resource':
                            external,
                            'src_file_id':
                            os.path.join(system_id, file_path.strip('/')),
                            'dest_file_id':
                            dest_file_id
                        },
                                                             queue='files')
                        event_data[
                            Notification.
                            MESSAGE] = 'Data copy was scheduled. This may take a few minutes.'
                        event_data[Notification.EXTRA] = {
                            'resource':
                            external,
                            'dest_file_id':
                            body.get('path'),
                            'src_file_id':
                            os.path.join(system_id, file_path.strip('/'))
                        }
                    elif body.get('system') != system_id:
                        if body.get('ipynb'):
                            path = '/{}'.format(request.user.username)
                        else:
                            path = body.get('path')
                        copied = fm.import_data(body.get('system'), path,
                                                system_id, file_path)
                        metrics.info('Data Depot',
                                     extra={
                                         'user':
                                         request.user.username,
                                         'sessionId':
                                         getattr(request.session,
                                                 'session_key', ''),
                                         'operation':
                                         'data_depot_copy',
                                         'info': {
                                             'destSystemId':
                                             body.get('system'),
                                             'destFilePath': path,
                                             'fromSystemId': system_id,
                                             'fromFilePath': file_path
                                         }
                                     })
                        event_data[Notification.EXTRA] = copied.to_dict()
                    else:
                        copied = fm.copy(system_id, file_path,
                                         body.get('path'), body.get('name'))
                        metrics.info('Data Depot',
                                     extra={
                                         'user':
                                         request.user.username,
                                         'sessionId':
                                         getattr(request.session,
                                                 'session_key', ''),
                                         'operation':
                                         'data_depot_copy',
                                         'info': {
                                             'destSystemId': system_id,
                                             'destFilePath': file_path,
                                             'fromSystemId':
                                             body.get('system'),
                                             'fromFilePath': body.get('path')
                                         }
                                     })
                        event_data[Notification.EXTRA] = copied.to_dict()

                    notification = Notification.objects.create(**event_data)
                    notification.save()
                    return JsonResponse(copied,
                                        encoder=AgaveJSONEncoder,
                                        safe=False)
                except HTTPError as e:
                    logger.exception(e.response.text)
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot',
                        Notification.OPERATION: 'data_depot_copy',
                        Notification.STATUS: Notification.ERROR,
                        Notification.USER: request.user.username,
                        Notification.MESSAGE:
                        'There was an error copying data.',
                        Notification.EXTRA: {
                            'message': e.response.text
                        }
                    }
                    Notification.objects.create(**event_data)
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'download':
                metrics.info('Data Depot',
                             extra={
                                 'user':
                                 request.user.username,
                                 'sessionId':
                                 getattr(request.session, 'session_key', ''),
                                 'operation':
                                 'agave_file_download',
                                 'info': {
                                     'systemId': system_id,
                                     'filePath': file_path
                                 }
                             })
                return JsonResponse(
                    {'href': fm.download(system_id, file_path)})

            elif action == 'mkdir':
                try:
                    dir_name = body.get('name')
                    new_dir = fm.mkdir(system_id, file_path, dir_name)
                    metrics.info('Data Depot',
                                 extra={
                                     'user':
                                     request.user.username,
                                     'sessionId':
                                     getattr(request.session, 'session_key',
                                             ''),
                                     'operation':
                                     'agave_file_mkdir',
                                     'info': {
                                         'systemId': system_id,
                                         'filePath': file_path,
                                         'dirName': dir_name
                                     }
                                 })
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot',
                        Notification.OPERATION: 'data_depot_mkdir',
                        Notification.STATUS: Notification.SUCCESS,
                        Notification.USER: request.user.username,
                        Notification.MESSAGE: 'Directory created.',
                        Notification.EXTRA: new_dir.to_dict()
                    }
                    Notification.objects.create(**event_data)
                    return JsonResponse(new_dir,
                                        encoder=AgaveJSONEncoder,
                                        safe=False)
                except HTTPError as e:
                    logger.exception(e.response.text)
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot',
                        Notification.OPERATION: 'data_depot_mkdir',
                        Notification.STATUS: Notification.ERROR,
                        Notification.USER: request.user.username,
                        Notification.MESSAGE: 'Error creating directory.',
                        Notification.EXTRA: {
                            'message': e.response.text
                        }
                    }
                    Notification.objects.create(**event_data)
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'move':
                try:
                    if body.get('system') != system_id:
                        moved = fm.import_data(body.get('system'),
                                               body.get('path'), system_id,
                                               file_path)
                        fm.delete(system_id, file_path)
                        metrics.info('Data Depot',
                                     extra={
                                         'user':
                                         request.user.username,
                                         'sessionId':
                                         getattr(request.session,
                                                 'session_key', ''),
                                         'operation':
                                         'agave_file_copy_move',
                                         'info': {
                                             'destSystemId':
                                             body.get('system'),
                                             'destFilePath': body.get('path'),
                                             'fromSystemId': system_id,
                                             'fromFilePath': file_path
                                         }
                                     })
                    else:
                        moved = fm.move(system_id, file_path, body.get('path'),
                                        body.get('name'))
                        metrics.info('Data Depot',
                                     extra={
                                         'user':
                                         request.user.username,
                                         'sessionId':
                                         getattr(request.session,
                                                 'session_key', ''),
                                         'operation':
                                         'agave_file_copy',
                                         'info': {
                                             'destSystemId': system_id,
                                             'destFilePath': file_path,
                                             'fromSystemId':
                                             body.get('system'),
                                             'fromFilePath': body.get('path')
                                         }
                                     })
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot',
                        Notification.STATUS: Notification.SUCCESS,
                        Notification.OPERATION: 'data_depot_move',
                        Notification.USER: request.user.username,
                        Notification.MESSAGE: 'Data has been moved.',
                        Notification.EXTRA: moved.to_dict()
                    }
                    Notification.objects.create(**event_data)
                    return JsonResponse(moved,
                                        encoder=AgaveJSONEncoder,
                                        safe=False)
                except HTTPError as e:
                    logger.exception(e.response.text)
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot_move',
                        Notification.STATUS: Notification.ERROR,
                        Notification.OPERATION: 'data_depot_move',
                        Notification.USER: request.user.username,
                        Notification.MESSAGE:
                        'There was an error moving your data.',
                        Notification.EXTRA: {
                            'message': e.response.text
                        }
                    }
                    Notification.objects.create(**event_data)
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'preview':
                try:
                    file_listing = fm.listing(system_id, file_path)
                    if file_listing.previewable:
                        preview_url = reverse(
                            'designsafe_api:files_media',
                            args=[file_mgr_name, system_id, file_path])
                        return JsonResponse({
                            'href':
                            '{}?preview=true'.format(preview_url),
                            'postit':
                            file_listing.download_postit(force=False,
                                                         lifetime=360)
                        })
                    else:
                        return HttpResponseBadRequest(
                            'Preview not available for this item.')
                except HTTPError as e:
                    logger.exception(
                        'Unable to preview file: {file_path}'.format(
                            file_path=file))
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'rename':
                try:
                    renamed = fm.rename(system_id, file_path, body.get('name'))
                    metrics.info('Data Depot',
                                 extra={
                                     'user':
                                     request.user.username,
                                     'sessionId':
                                     getattr(request.session, 'session_key',
                                             ''),
                                     'operation':
                                     'agave_file_rename',
                                     'info': {
                                         'systemId': system_id,
                                         'filePath': file_path,
                                         'name': body.get('name')
                                     }
                                 })
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot_rename',
                        Notification.STATUS: Notification.SUCCESS,
                        Notification.USER: request.user.username,
                        Notification.MESSAGE: 'File/folder was renamed.',
                        Notification.EXTRA: renamed.to_dict()
                    }
                    Notification.objects.create(**event_data)
                    return JsonResponse(renamed,
                                        encoder=AgaveJSONEncoder,
                                        safe=False)
                except HTTPError as e:
                    logger.exception(e.response.text)
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot_rename',
                        Notification.STATUS: Notification.ERROR,
                        Notification.USER: request.user.username,
                        Notification.MESSAGE:
                        'There was an error renaming a file/folder.',
                        Notification.EXTRA: {
                            'message': e.response.text
                        }
                    }
                    Notification.objects.create(**event_data)
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'trash':
                trash_path = '/Trash'
                if system_id == AgaveFileManager.DEFAULT_SYSTEM_ID:
                    trash_path = request.user.username + '/.Trash'

                try:
                    trashed = fm.trash(system_id, file_path, trash_path)
                    metrics.info('Data Depot',
                                 extra={
                                     'user':
                                     request.user.username,
                                     'sessionId':
                                     getattr(request.session, 'session_key',
                                             ''),
                                     'operation':
                                     'agave_file_trash',
                                     'info': {
                                         'systemId': system_id,
                                         'filePath': file_path,
                                         'trashPath': trash_path
                                     }
                                 })
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot',
                        Notification.STATUS: Notification.SUCCESS,
                        Notification.OPERATION: 'data_depot_trash',
                        Notification.USER: request.user.username,
                        Notification.MESSAGE:
                        'File/folder was moved to trash.',
                        Notification.EXTRA: trashed.to_dict()
                    }
                    Notification.objects.create(**event_data)
                    return JsonResponse(trashed,
                                        encoder=AgaveJSONEncoder,
                                        safe=False)
                except HTTPError as e:
                    logger.error(e.response.text)
                    event_data = {
                        Notification.EVENT_TYPE: 'data_depot_trash',
                        Notification.STATUS: Notification.ERROR,
                        Notification.OPERATION: 'data_depot_trash',
                        Notification.USER: request.user.username,
                        Notification.MESSAGE:
                        'There was an error moving file/folder to trash.',
                        Notification.EXTRA: {
                            'message': e.response.text
                        }
                    }
                    Notification.objects.create(**event_data)
                    return HttpResponseBadRequest(e.response.text)

        return HttpResponseBadRequest("Unsupported operation")
Exemple #2
0
    def put(self, request, file_mgr_name, system_id, file_path):
        if request.is_ajax():
            body = json.loads(request.body)
        else:
            body = request.POST.copy()

        if file_mgr_name == AgaveFileManager.NAME \
            or file_mgr_name == 'public':
            if not request.user.is_authenticated():
                return HttpResponseForbidden('Log in required')

            fm = AgaveFileManager(agave_client=request.user.agave_oauth.client)
            action = body.get('action', '')
            if action == 'copy':
                try:
                    if body.get('system') != system_id:
                        copied = fm.import_data(body.get('system'), body.get('path'),
                                                system_id, file_path)
                    else:
                        copied = fm.copy(system_id, file_path, body.get('path'), body.get('name'))
                    return JsonResponse(copied, encoder=AgaveJSONEncoder, safe=False)
                except HTTPError as e:
                    logger.exception(e.response.text)
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'download':
                return JsonResponse({
                    'href': fm.download(system_id, file_path)
                })

            elif action == 'mkdir':
                try:
                    new_dir = fm.mkdir(system_id, file_path, body.get('name'))
                    return JsonResponse(new_dir, encoder=AgaveJSONEncoder, safe=False)
                except HTTPError as e:
                    logger.exception(e.response.text)
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'move':
                try:
                    if body.get('system') != system_id:
                        moved = fm.import_data(body.get('system'), body.get('path'), system_id, file_path)
                        fm.delete(system_id, file_path)
                    else:
                        moved = fm.move(system_id, file_path, body.get('path'), body.get('name'))
                    return JsonResponse(moved, encoder=AgaveJSONEncoder, safe=False)
                except HTTPError as e:
                    logger.exception(e.response.text)
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'preview':
                try:
                    file_listing = fm.listing(system_id, file_path)
                    if file_listing.previewable:
                        preview_url = reverse('designsafe_api:files_media',
                                              args=[file_mgr_name, system_id, file_path])
                        return JsonResponse({'href': '{}?preview=true'.format(preview_url)})
                    else:
                        return HttpResponseBadRequest('Preview not available for this item')
                except HTTPError as e:
                    logger.exception('Unable to preview file')
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'rename':
                try:
                    renamed = fm.rename(system_id, file_path, body.get('name'))
                    return JsonResponse(renamed, encoder=AgaveJSONEncoder, safe=False)
                except HTTPError as e:
                    logger.exception(e.response.text)
                    return HttpResponseBadRequest(e.response.text)

            elif action == 'trash':
                trash_path = '/Trash'
                if system_id == AgaveFileManager.DEFAULT_SYSTEM_ID:
                    trash_path = request.user.username + '/.Trash'

                try:
                    trashed = fm.trash(system_id, file_path, trash_path)
                    return JsonResponse(trashed, encoder=AgaveJSONEncoder, safe=False)
                except HTTPError as e:
                    logger.error(e.response.text)
                    return HttpResponseBadRequest(e.response.text)

        return HttpResponseBadRequest("Unsupported operation")