def preview(self, file_id, **kwargs): system, file_path = self.parse_file_id(file_id) f = AgaveFile.from_file_path(system, None, file_path, agave_client=self.agave_client, source="public") if f.previewable: fmt = kwargs.get("format", "json") if fmt == "html": context = {} ext = f.ext.lower() if ext in AgaveFile.SUPPORTED_IMAGE_PREVIEW_EXTS: postit = f.create_postit(force=False, lifetime=360) context["image_preview"] = postit["_links"]["self"]["href"] elif ext in AgaveFile.SUPPORTED_TEXT_PREVIEW_EXTS: content = f.download() context["text_preview"] = content elif ext in AgaveFile.SUPPORTED_OBJECT_PREVIEW_EXTS: postit = f.create_postit(force=False) context["object_preview"] = postit["_links"]["self"]["href"] return "designsafe/apps/api/data/agave/preview.html", context else: preview_url = ( reverse("designsafe_api:file", args=[self.resource, file_id]) + "?action=preview&format=html" ) return {"href": preview_url} else: return None
def preview(self, file_id, **kwargs): system, file_path = self.parse_file_id(file_id) f = AgaveFile.from_file_path(system, None, file_path, agave_client=self.agave_client, source='public') if f.previewable: fmt = kwargs.get('format', 'json') if fmt == 'html': context = {} ext = f.ext.lower() if ext in AgaveFile.SUPPORTED_IMAGE_PREVIEW_EXTS: postit = f.create_postit(force=False, lifetime=360) context['image_preview'] = postit['_links']['self']['href'] elif ext in AgaveFile.SUPPORTED_TEXT_PREVIEW_EXTS: content = f.download() context['text_preview'] = content elif ext in AgaveFile.SUPPORTED_OBJECT_PREVIEW_EXTS: postit = f.create_postit(force=False) context['object_preview'] = postit['_links']['self'][ 'href'] return 'designsafe/apps/api/data/agave/preview.html', context else: preview_url = reverse('designsafe_api:file', args=[self.resource, file_id ]) + '?action=preview&format=html' return {'href': preview_url} else: return None
def test_from_file_path(self): ac = mock.Mock(autospec = Agave) ac.files.list.return_value = [self.afile_json] file_path = '%s/%s' % (self.user.username, self.afile_json['path']) af = AgaveFile.from_file_path(settings.AGAVE_STORAGE_SYSTEM, self.user.username, file_path, agave_client = ac) ac.files.list.assert_called_with(systemId= settings.AGAVE_STORAGE_SYSTEM, filePath = file_path) self.assertEqual(af.full_path, self.afile_json['path'])
def test_from_file_path(self): ac = mock.Mock(autospec=Agave) ac.files.list.return_value = [self.afile_json] file_path = '%s/%s' % (self.user.username, self.afile_json['path']) af = AgaveFile.from_file_path(settings.AGAVE_STORAGE_SYSTEM, self.user.username, file_path, agave_client=ac) ac.files.list.assert_called_with( systemId=settings.AGAVE_STORAGE_SYSTEM, filePath=file_path) self.assertEqual(af.full_path, self.afile_json['path'])
def download(self, file_id, **kwargs): """Get the download link for a file :param str file_id: String with the format <filesystem id>[/ | /<username> [/ | /<file_path>] ] :returns: a dict with a single key `href` which has the direct noauth link to download a file :rtype: dict """ system, file_path = self.parse_file_id(file_id) f = AgaveFile.from_file_path(system, None, file_path, agave_client=self.agave_client, source="public") if f.type == "file": postit = f.create_postit(force=True, max_uses=10, lifetime=3600) return {"href": postit["_links"]["self"]["href"]} else: return None
def download(self, file_id, **kwargs): """Get the download link for a file :param str file_id: String with the format <filesystem id>[/ | /<username> [/ | /<file_path>] ] :returns: a dict with a single key `href` which has the direct noauth link to download a file :rtype: dict """ system, file_path = self.parse_file_id(file_id) f = AgaveFile.from_file_path(system, None, file_path, agave_client=self.agave_client, source='public') if f.type == 'file': postit = f.create_postit(force=True, max_uses=10, lifetime=3600) return {'href': postit['_links']['self']['href']} else: return None
def share_agave(self, username, file_id, permissions, recursive): try: # n = Notification(event_type = 'data', # status = 'INFO', # operation = 'share_initializing', # message = 'File sharing is initializing. Please wait...', # user = username, # extra = {'target_path': reverse('designsafe_data:data_depot', # args=['agave', file_id])}) # n.save() user = get_user_model().objects.get(username=username) from designsafe.apps.api.data import AgaveFileManager from designsafe.apps.api.data.agave.file import AgaveFile from designsafe.apps.api.data.agave.elasticsearch.documents import Object agave_fm = AgaveFileManager(user) system_id, file_user, file_path = agave_fm.parse_file_id(file_id) f = AgaveFile.from_file_path(system_id, username, file_path, agave_client=agave_fm.agave_client) f_dict = f.to_dict() n = Notification(event_type = 'data', status = 'INFO', operation = 'share_initializing', message = 'File sharing is initializing. Please wait...', user = username, extra = f_dict) n.save() f.share(permissions, recursive) #reindex_agave.apply_async(args=(self.username, file_id)) # self.indexer.index(system, file_path, file_user, pems_indexing=True) esf = Object.from_file_path(system_id, username, file_path) esf.share(username, permissions, recursive) # Notify owner share completed n = Notification(event_type = 'data', status = 'SUCCESS', operation = 'share_finished', message = 'File permissions were updated successfully.', user = username, extra = f_dict) n.save() # Notify users they have new shared files for pem in permissions: if pem['permission'] != 'NONE': message = '%s shared some files with you.' % user.get_full_name() n = Notification(event_type = 'data', status = 'SUCCESS', operation = 'share_finished', message = message, user = pem['user_to_share'], extra = f_dict) n.save() except: logger.error('Error sharing file/folder', exc_info=True, extra = { 'username': username, 'file_id': file_id, 'permissions': permissions }) n = Notification(event_type='data', status=Notification.ERROR, operation='share_error', message='We were unable to share the specified folder/file(s). ' 'Please try again...', user=username, extra={'system': system_id, 'path': file_path }) n.save()
def share_agave(self, username, file_id, permissions, recursive): try: n = Notification(event_type = 'data', status = 'INFO', operation = 'share_initializing', message = 'File sharing is initializing. Please wait...', user = username, extra = {'target_path': reverse('designsafe_data:data_browser', args=['agave', file_id])}) n.save() user = get_user_model().objects.get(username=username) from designsafe.apps.api.data import AgaveFileManager from designsafe.apps.api.data.agave.file import AgaveFile from designsafe.apps.api.data.agave.elasticsearch.documents import Object agave_fm = AgaveFileManager(user) system_id, file_user, file_path = agave_fm.parse_file_id(file_id) f = AgaveFile.from_file_path(system_id, username, file_path, agave_client=agave_fm.agave_client) f.share(permissions, recursive) #reindex_agave.apply_async(args=(self.username, file_id)) # self.indexer.index(system, file_path, file_user, pems_indexing=True) esf = Object.from_file_path(system_id, username, file_path) esf.share(username, permissions, recursive) # Notify owner share completed n = Notification(event_type = 'data', status = 'SUCCESS', operation = 'share_finished', message = 'File permissions were updated successfully.', user = username, extra = {'target_path': reverse('designsafe_data:data_browser', args=['agave', file_id])}) n.save() # Notify users they have new shared files for pem in permissions: if pem['permission'] != 'NONE': message = '%s shared some files with you.' % user.get_full_name() n = Notification(event_type = 'data', status = 'SUCCESS', operation = 'share_finished', message = message, user = pem['user_to_share'], extra = {'target_path': reverse('designsafe_data:data_browser', args=['agave', file_id])}) n.save() except: logger.error('Error sharing file/folder', exc_info=True, extra = { 'username': username, 'file_id': file_id, 'permissions': permissions }) n = Notification(event_type='data', status=Notification.ERROR, operation='share_error', message='We were unable to share the specified folder/file(s). ' 'Please try again...', user=username, extra={}) n.save()