本文整理汇总了Python中osf.models.BaseFileNode类的典型用法代码示例。如果您正苦于以下问题:Python BaseFileNode类的具体用法?Python BaseFileNode怎么用?Python BaseFileNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BaseFileNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: addon_delete_file_node
def addon_delete_file_node(self, node, user, event_type, payload):
""" Get addon BaseFileNode(s), move it into the TrashedFileNode collection
and remove it from StoredFileNode.
Required so that the guids of deleted addon files are not re-pointed when an
addon file or folder is moved or renamed.
"""
if event_type == 'file_removed' and payload.get('provider', None) != 'osfstorage':
provider = payload['provider']
path = payload['metadata']['path']
materialized_path = payload['metadata']['materialized']
if path.endswith('/'):
folder_children = BaseFileNode.resolve_class(provider, BaseFileNode.ANY).objects.filter(
provider=provider,
node=node,
_materialized_path__startswith=materialized_path
)
for item in folder_children:
if item.kind == 'file' and not TrashedFileNode.load(item._id):
item.delete(user=user)
elif item.kind == 'folder':
BaseFileNode.remove_one(item)
else:
try:
file_node = BaseFileNode.resolve_class(provider, BaseFileNode.FILE).objects.get(
node=node,
_materialized_path=materialized_path
)
except BaseFileNode.DoesNotExist:
file_node = None
if file_node and not TrashedFileNode.load(file_node._id):
file_node.delete(user=user)
示例2: update_file_guid_referent
def update_file_guid_referent(self, node, event_type, payload, user=None):
if event_type not in ('addon_file_moved', 'addon_file_renamed'):
return # Nothing to do
source, destination = payload['source'], payload['destination']
source_node, destination_node = Node.load(source['node']['_id']), Node.load(destination['node']['_id'])
if source['provider'] in settings.ADDONS_BASED_ON_IDS:
if event_type == 'addon_file_renamed':
return # Node has not changed and provider has not changed
# Must be a move
if source['provider'] == destination['provider'] and source_node == destination_node:
return # Node has not changed and provider has not changed
file_guids = BaseFileNode.resolve_class(source['provider'], BaseFileNode.ANY).get_file_guids(
materialized_path=source['materialized'] if source['provider'] != 'osfstorage' else source['path'],
provider=source['provider'],
node=source_node
)
for guid in file_guids:
obj = Guid.load(guid)
if source_node != destination_node and Comment.find(Q('root_target._id', 'eq', guid)).count() != 0:
update_comment_node(guid, source_node, destination_node)
if source['provider'] != destination['provider'] or source['provider'] != 'osfstorage':
old_file = BaseFileNode.load(obj.referent._id)
obj.referent = create_new_file(obj, source, destination, destination_node)
obj.save()
if old_file and not TrashedFileNode.load(old_file._id):
old_file.delete()
示例3: get_metadata_files
def get_metadata_files(draft):
data = draft.registration_metadata
for q, question in get_file_questions('prereg-prize.json'):
if not isinstance(data[q]['value'], dict):
for i, file_info in enumerate(data[q]['extra']):
provider = file_info['data']['provider']
if provider != 'osfstorage':
raise Http404(
'File does not exist in OSFStorage ({}: {})'.format(
q, question
))
file_guid = file_info.get('fileId')
if not file_guid:
node = Node.load(file_info.get('nodeId'))
path = file_info['data'].get('path')
item = BaseFileNode.resolve_class(
provider,
BaseFileNode.FILE
).get_or_create(node, path)
file_guid = item.get_guid(create=True)._id
data[q]['extra'][i]['fileId'] = file_guid
draft.update_metadata(data)
draft.save()
else:
item = BaseFileNode.load(file_info['data']['path'].replace('/', ''))
if item is None:
raise Http404(
'File with guid "{}" in "{}" does not exist'.format(
file_guid, question
))
yield item
continue
for i, file_info in enumerate(data[q]['value']['uploader']['extra']):
provider = file_info['data']['provider']
if provider != 'osfstorage':
raise Http404(
'File does not exist in OSFStorage ({}: {})'.format(
q, question
))
file_guid = file_info.get('fileId')
if not file_guid:
node = Node.load(file_info.get('nodeId'))
path = file_info['data'].get('path')
item = BaseFileNode.resolve_class(
provider,
BaseFileNode.FILE
).get_or_create(node, path)
file_guid = item.get_guid(create=True)._id
data[q]['value']['uploader']['extra'][i]['fileId'] = file_guid
draft.update_metadata(data)
draft.save()
else:
item = BaseFileNode.load(file_info['data']['path'].replace('/', ''))
if item is None:
raise Http404(
'File with guid "{}" in "{}" does not exist'.format(
file_guid, question
))
yield item
示例4: test_comments_move_when_file_moved_to_osfstorage
def test_comments_move_when_file_moved_to_osfstorage(self, project, user):
osfstorage = project.get_addon('osfstorage')
root_node = osfstorage.get_root()
osf_file = root_node.append_file('file.txt')
osf_file.create_version(user, {
'object': '06d80e',
'service': 'cloud',
osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
}, {
'size': 1337,
'contentType': 'img/png',
'etag': 'abcdefghijklmnop'
}).save()
source = {
'path': '/file.txt',
'node': project,
'provider': self.provider
}
destination = {
'path': osf_file.path,
'node': project,
'provider': 'osfstorage'
}
self._create_file_with_comment(node=source['node'], path=source['path'], user=user)
payload = self._create_payload('move', user, source, destination, self.file._id, destination_file_id=destination['path'].strip('/'))
update_file_guid_referent(self=None, target=destination['node'], event_type='addon_file_moved', payload=payload)
self.guid.reload()
file_node = BaseFileNode.resolve_class('osfstorage', BaseFileNode.FILE).get_or_create(destination['node'], destination['path'])
assert self.guid._id == file_node.get_guid()._id
file_comments = Comment.objects.filter(root_target=self.guid.pk)
assert file_comments.count() == 1
示例5: test_comments_move_when_folder_moved_to_different_provider
def test_comments_move_when_folder_moved_to_different_provider(self, destination_provider, destination_path, project, user):
if self.provider == destination_provider:
return True
project.add_addon(destination_provider, auth=Auth(user))
project.save()
self.addon_settings = project.get_addon(destination_provider)
self.addon_settings.folder = '/AddonFolder'
self.addon_settings.save()
source = {
'path': '/',
'node': project,
'provider': self.provider
}
destination = {
'path': '/subfolder/',
'node': project,
'provider': destination_provider,
'children': [{
'path': '/subfolder/file.txt',
'node': project,
'provider': destination_provider
}]
}
file_name = 'file.txt'
self._create_file_with_comment(node=source['node'], path='{}{}'.format(source['path'], file_name), user=user)
payload = self._create_payload('move', user, source, destination, self.file._id)
update_file_guid_referent(self=None, target=destination['node'], event_type='addon_file_moved', payload=payload)
self.guid.reload()
file_node = BaseFileNode.resolve_class(destination_provider, BaseFileNode.FILE).get_or_create(destination['node'], destination_path)
assert self.guid._id == file_node.get_guid()._id
file_comments = Comment.objects.filter(root_target=self.guid.pk)
assert file_comments.count() == 1
示例6: get_file_node_from_wb_resp
def get_file_node_from_wb_resp(self, item):
"""Takes file data from wb response, touches/updates metadata for it, and returns file object"""
attrs = item['attributes']
file_node = BaseFileNode.resolve_class(
attrs['provider'],
BaseFileNode.FOLDER if attrs['kind'] == 'folder'
else BaseFileNode.FILE,
).get_or_create(self.get_node(check_object_permissions=False), attrs['path'])
file_node.update(None, attrs, user=self.request.user)
return file_node
示例7: create_new_file
def create_new_file(obj, source, destination, destination_node):
# TODO: Remove when materialized paths are fixed in the payload returned from waterbutler
if not source['materialized'].startswith('/'):
source['materialized'] = '/' + source['materialized']
if not destination['materialized'].startswith('/'):
destination['materialized'] = '/' + destination['materialized']
if not source['path'].endswith('/'):
data = dict(destination)
new_file = BaseFileNode.resolve_class(destination['provider'], BaseFileNode.FILE).get_or_create(destination_node, destination['path'])
if destination['provider'] != 'osfstorage':
new_file.update(revision=None, data=data)
else:
new_file = find_and_create_file_from_metadata(destination.get('children', []), source, destination, destination_node, obj)
if not new_file:
if source['provider'] == 'box':
new_path = obj.referent.path
else:
new_path = obj.referent.materialized_path.replace(source['materialized'], destination['materialized'])
new_file = BaseFileNode.resolve_class(destination['provider'], BaseFileNode.FILE).get_or_create(destination_node, new_path)
new_file.name = new_path.split('/')[-1]
new_file.materialized_path = new_path
new_file.save()
return new_file
示例8: find_and_create_file_from_metadata
def find_and_create_file_from_metadata(children, source, destination, destination_node, obj):
""" Given a Guid obj, recursively search for the metadata of its referent (a file obj)
in the waterbutler response. If found, create a new addon BaseFileNode with that metadata
and return the new file.
"""
for item in children:
# TODO: Remove when materialized paths are fixed in the payload returned from waterbutler
if not item['materialized'].startswith('/'):
item['materialized'] = '/' + item['materialized']
if item['kind'] == 'folder':
return find_and_create_file_from_metadata(item.get('children', []), source, destination, destination_node, obj)
elif item['kind'] == 'file' and item['materialized'].replace(destination['materialized'], source['materialized']) == obj.referent.materialized_path:
data = dict(item)
new_file = BaseFileNode.resolve_class(destination['provider'], BaseFileNode.FILE).get_or_create(destination_node, item['path'])
if destination['provider'] != 'osfstorage':
new_file.update(revision=None, data=data)
return new_file
示例9: test_comments_move_on_file_rename
def test_comments_move_on_file_rename(self, project, user):
source = {
'path': '/file.txt',
'node': project,
'provider': self.provider
}
destination = {
'path': '/file_renamed.txt',
'node': project,
'provider': self.provider
}
self._create_file_with_comment(node=source['node'], path=source['path'], user=user)
payload = self._create_payload('move', user, source, destination, self.file._id)
update_file_guid_referent(self=None, target=destination['node'], event_type='addon_file_renamed', payload=payload)
self.guid.reload()
file_node = BaseFileNode.resolve_class(self.provider, BaseFileNode.FILE).get_or_create(destination['node'], self._format_path(destination['path'], file_id=self.file._id))
assert self.guid._id == file_node.get_guid()._id
file_comments = Comment.objects.filter(root_target=self.guid.pk)
assert file_comments.count() == 1
示例10: test_comments_move_when_folder_moved_from_component_to_project
def test_comments_move_when_folder_moved_from_component_to_project(self, project, component, user):
source = {
'path': '/subfolder/',
'node': component,
'provider': self.provider
}
destination = {
'path': '/subfolder/',
'node': project,
'provider': self.provider
}
file_name = 'file.txt'
self._create_file_with_comment(node=source['node'], path='{}{}'.format(source['path'], file_name), user=user)
payload = self._create_payload('move', user, source, destination, self.file._id)
update_file_guid_referent(self=None, node=destination['node'], event_type='addon_file_moved', payload=payload)
self.guid.reload()
file_node = BaseFileNode.resolve_class(self.provider, BaseFileNode.FILE).get_or_create(destination['node'], self._format_path('{}{}'.format(destination['path'], file_name), file_id=self.file._id))
assert self.guid._id == file_node.get_guid()._id
file_comments = Comment.objects.filter(root_target=self.guid.pk)
assert file_comments.count() == 1
示例11: test_comments_move_on_subfolder_file_when_parent_folder_is_renamed
def test_comments_move_on_subfolder_file_when_parent_folder_is_renamed(self, project, user):
source = {
'path': '/subfolder1/',
'node': project,
'provider': self.provider
}
destination = {
'path': '/subfolder2/',
'node': project,
'provider': self.provider
}
file_path = 'sub-subfolder/file.txt'
self._create_file_with_comment(node=source['node'], path='{}{}'.format(source['path'], file_path), user=user)
payload = self._create_payload('move', user, source, destination, self.file._id)
update_file_guid_referent(self=None, node=destination['node'], event_type='addon_file_renamed', payload=payload)
self.guid.reload()
file_node = BaseFileNode.resolve_class(self.provider, BaseFileNode.FILE).get_or_create(destination['node'], self._format_path('{}{}'.format(destination['path'], file_path), file_id=self.file._id))
assert self.guid._id == file_node.get_guid()._id
file_comments = Comment.find(Q('root_target', 'eq', self.guid.pk))
assert file_comments.count() == 1
示例12: test_comments_move_when_folder_moved_to_osfstorage
def test_comments_move_when_folder_moved_to_osfstorage(self, project, user):
osfstorage = project.get_addon('osfstorage')
root_node = osfstorage.get_root()
osf_folder = root_node.append_folder('subfolder')
osf_file = osf_folder.append_file('file.txt')
osf_file.create_version(user, {
'object': '06d80e',
'service': 'cloud',
osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
}, {
'size': 1337,
'contentType': 'img/png',
'etag': '1234567890abcde'
}).save()
source = {
'path': '/subfolder/',
'node': project,
'provider': self.provider
}
destination = {
'path': '/subfolder/',
'node': project,
'provider': 'osfstorage',
'children': [{
'path': '/subfolder/file.txt',
'node': project,
'provider': 'osfstorage'
}]
}
file_name = 'file.txt'
self._create_file_with_comment(node=source['node'], path='{}{}'.format(source['path'], file_name), user=user)
payload = self._create_payload('move', user, source, destination, self.file._id, destination_file_id=osf_file._id)
update_file_guid_referent(self=None, node=destination['node'], event_type='addon_file_moved', payload=payload)
self.guid.reload()
file_node = BaseFileNode.resolve_class('osfstorage', BaseFileNode.FILE).get_or_create(destination['node'], osf_file._id)
assert self.guid._id == file_node.get_guid()._id
file_comments = Comment.find(Q('root_target', 'eq', self.guid.pk))
assert file_comments.count() == 1
示例13: bulk_get_file_nodes_from_wb_resp
def bulk_get_file_nodes_from_wb_resp(self, files_list):
"""Takes a list of file data from wb response, touches/updates metadata for each, and returns list of file objects.
This function mirrors all the actions of get_file_node_from_wb_resp except the create and updates are done in bulk.
The bulk_update and bulk_create do not call the base class update and create so the actions of those functions are
done here where needed
"""
node = self.get_node(check_object_permissions=False)
content_type = ContentType.objects.get_for_model(node)
objs_to_create = defaultdict(lambda: [])
file_objs = []
for item in files_list:
attrs = item['attributes']
base_class = BaseFileNode.resolve_class(
attrs['provider'],
BaseFileNode.FOLDER if attrs['kind'] == 'folder'
else BaseFileNode.FILE,
)
# mirrors BaseFileNode get_or_create
try:
file_obj = base_class.objects.get(target_object_id=node.id, target_content_type=content_type, _path='/' + attrs['path'].lstrip('/'))
except base_class.DoesNotExist:
# create method on BaseFileNode appends provider, bulk_create bypasses this step so it is added here
file_obj = base_class(target=node, _path='/' + attrs['path'].lstrip('/'), provider=base_class._provider)
objs_to_create[base_class].append(file_obj)
else:
file_objs.append(file_obj)
file_obj.update(None, attrs, user=self.request.user, save=False)
bulk_update(file_objs)
for base_class in objs_to_create:
base_class.objects.bulk_create(objs_to_create[base_class])
file_objs += objs_to_create[base_class]
return file_objs
示例14: test_comments_move_when_file_moved_from_component_to_project
def test_comments_move_when_file_moved_from_component_to_project(self, project, component, user):
source = {
'path': '/file.txt',
'node': component,
'provider': self.provider
}
destination = {
'path': '/file.txt',
'node': project,
'provider': self.provider
}
self._create_file_with_comment(node=source['node'], path=source['path'], user=user)
self.file.move_under(destination['node'].get_addon(self.provider).get_root())
payload = self._create_payload('move', user, source, destination, self.file._id)
update_file_guid_referent(self=None, node=destination['node'], event_type='addon_file_moved', payload=payload)
self.guid.reload()
file_node = BaseFileNode.resolve_class(self.provider, BaseFileNode.FILE).get_or_create(destination['node'], self._format_path(destination['path'], file_id=self.file._id))
assert self.guid._id == file_node.get_guid()._id
assert self.guid.referent.node._id == destination['node']._id
file_comments = Comment.find(Q('root_target', 'eq', self.guid.pk))
assert file_comments.count() == 1
示例15: view_file
def view_file(request, node_id, provider, file_id):
fp = BaseFileNode.load(file_id)
wb_url = fp.generate_waterbutler_url()
return redirect(wb_url)