def upload(self, system, file_path, upload_file): f = BaseFileResource(self._ag, system, file_path) resp = f.upload(upload_file) reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, file_path), 'levels': 1}) return resp
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")
def rename(self, system, file_path, rename_to): f = BaseFileResource.listing(self._ag, system, file_path) resp = f.rename(rename_to) parent_path = '/'.join(file_path.strip('/').split('/')[:-1]) reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, parent_path), 'levels': 1}) return resp
def project_directory(self): """ Queries for the File object that represents the root of this Project's files. :return: The project's root dir :rtype: :class:`BaseFileResource` """ if self._project_directory is None: self._project_directory = BaseFileResource.listing( system=self.project_system_id, path='/', agave_client=self._agave) return self._project_directory
def import_data(self, system, file_path, from_system, from_file_path): file_path = file_path or '/' if file_path != '/': file_path = file_path.strip('/') from_file_path = from_file_path.strip('/') f = BaseFileResource.listing(self._ag, system, file_path) res = f.import_data(from_system, from_file_path) file_name = from_file_path.split('/')[-1] reindex_agave.apply_async(kwargs={'username': '******', 'file_id': '{}/{}'.format(system, os.path.join(file_path, file_name))}) return res
def get(self, request, project_id, file_path=''): """ :return: The root directory for the Project's data :rtype: JsonResponse """ ag = request.user.agave_oauth.client p = Project(ag, uuid=project_id) list_path = '/'.join([project_id, file_path]) listing = BaseFileResource.listing(ag, p.project_system_id, list_path) return JsonResponse(listing, encoder=AgaveJSONEncoder, safe=False)
def move(self, system, file_path, dest_path, dest_name=None): f = BaseFileResource.listing(self._ag, system, file_path) resp = f.move(dest_path, dest_name) parent_path = '/'.join(file_path.strip('/').split('/')[:-1]) parent_path = parent_path.strip('/') or '/' reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, parent_path), 'levels': 1}) reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, os.path.join(dest_path, resp.name)), 'levels': 1}) return resp
def trash(self, system, file_path, trash_path): name = os.path.basename(file_path) f = BaseFileResource(self._ag, system, file_path) # first ensure trash_path exists BaseFileResource.ensure_path(self._ag, system, trash_path) # check if file with same name exists in trash; expect 404 try: check = os.path.join(trash_path, name) BaseFileResource.listing(self._ag, system, check) # if we did not 404, then we have a conflict; append date to name name_ext = os.path.splitext(name) timestamp = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') name = '{0} {1}{2}'.format(name_ext[0], timestamp, name_ext[1]) except HTTPError as e: if e.response.status_code != 404: raise resp = f.move(trash_path, name) parent_path = '/'.join(file_path.strip('/').split('/')[:-1]) parent_path = parent_path.strip('/') or '/' reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, trash_path), 'levels': 1}) reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, parent_path), 'levels': 1}) return resp
def copy(self, system, file_path, dest_path=None, dest_name=None): f = BaseFileResource.listing(self._ag, system, file_path) # default to same path if dest_path is None: dest_path = f.path # default to same name if dest_name is None: dest_name = f.name # if same path and name, add suffix to file name if dest_name == f.name and dest_path == f.path: dest_name = '{0}_copy{1}'.format(*os.path.splitext(dest_name)) copied_file = f.copy(dest_path, dest_name) # schedule celery task to index new copy reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}/{}'.format(system, dest_path.strip('/'), dest_name)}) return copied_file
def post(self, request): """ Create a new Project. Projects and the root File directory for a Project should be owned by the portal, with roles/permissions granted to the creating user. 1. Create the metadata record for the project 2. Create a directory on the projects storage system named after the metadata uuid 3. Associate the metadata uuid and file uuid :param request: :return: The newly created project :rtype: JsonResponse """ # portal service account needs to create the objects on behalf of the user ag = get_service_account_client() if request.is_ajax(): post_data = json.loads(request.body) else: post_data = request.POST.copy() # create Project (metadata) metrics.info('projects', extra={'user' : request.user.username, 'sessionId': getattr(request.session, 'session_key', ''), 'operation': 'metadata_create', 'info': {'postData': post_data} }) p = Project(ag) p.title = post_data.get('title') p.pi = post_data.get('pi', None) p.save() # create Project Directory on Managed system metrics.info('projects', extra={'user' : request.user.username, 'sessionId': getattr(request.session, 'session_key', ''), 'operation': 'base_directory_create', 'info': { 'systemId': Project.STORAGE_SYSTEM_ID, 'uuid': p.uuid }}) project_storage_root = BaseFileResource(ag, Project.STORAGE_SYSTEM_ID, '/') project_storage_root.mkdir(p.uuid) # Wrap Project Directory as private system for project project_system_tmpl = template_project_storage_system(p) metrics.info('projects', extra={'user' : request.user.username, 'sessionId': getattr(request.session, 'session_key', ''), 'operation': 'private_system_create', 'info': { 'id': project_system_tmpl.get('id'), 'site': project_system_tmpl.get('site'), 'default': project_system_tmpl.get('default'), 'status': project_system_tmpl.get('status'), 'description': project_system_tmpl.get('description'), 'name': project_system_tmpl.get('name'), 'globalDefault': project_system_tmpl.get('globalDefault'), 'available': project_system_tmpl.get('available'), 'public': project_system_tmpl.get('public'), 'type': project_system_tmpl.get('type'), 'storage': { 'homeDir': project_system_tmpl.get('storage', {}).get('homeDir'), 'rootDir': project_system_tmpl.get('storage', {}).get('rootDir') } }}) ag.systems.add(body=project_system_tmpl) # grant initial permissions for creating user and PI, if exists project_system_tmpl = template_project_storage_system(p) metrics.info('projects', extra={'user' : request.user.username, 'sessionId': getattr(request.session, 'session_key', ''), 'operation': 'initial_pems_create', 'info': {'collab': request.user.username, 'pi': p.pi} }) p.add_collaborator(request.user.username) if p.pi and p.pi != request.user.username: p.add_collaborator(p.pi) return JsonResponse(p, encoder=AgaveJSONEncoder, safe=False)
def project_data_listing(self, path='/'): return BaseFileResource.listing(system=self.project_system_id, path=path, agave_client=self._agave)
def listing(self, system, file_path, offset=0, limit=100): return BaseFileResource.listing(self._ag, system, file_path, offset, limit)
def download(self, system, path): file_obj = BaseFileResource.listing(self._ag, system, path) postit = file_obj.download_postit() return postit
def mkdir(self, system, file_path, dir_name): f = BaseFileResource(self._ag, system, file_path) resp = f.mkdir(dir_name) reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, file_path)}) return resp