def get_comment(self, check_permissions=True): pk = self.kwargs[self.comment_lookup_url_kwarg] try: comment = Comment.find_one(Q('_id', 'eq', pk) & Q('root_target', 'ne', None)) except NoResultsFound: raise NotFound # Deleted root targets still appear as tuples in the database and are included in # the above query, requiring an additional check if isinstance(comment.root_target.referent, TrashedFileNode): comment.root_target = None comment.save() if comment.root_target is None: raise NotFound if isinstance(comment.root_target.referent, StoredFileNode): root_target = comment.root_target referent = root_target.referent if referent.provider == 'osfstorage': try: StoredFileNode.find( Q('node', 'eq', comment.node._id) & Q('_id', 'eq', referent._id) & Q('is_file', 'eq', True) ) except NoResultsFound: Comment.update(Q('root_target', 'eq', root_target), data={'root_target': None}) raise NotFound else: if referent.provider == 'dropbox': # referent.path is the absolute path for the db file, but wb requires the relative path referent = DropboxFile.load(referent._id) url = waterbutler_api_url_for(comment.node._id, referent.provider, referent.path, meta=True) waterbutler_request = requests.get( url, cookies=self.request.COOKIES, headers={'Authorization': self.request.META.get('HTTP_AUTHORIZATION')}, ) if waterbutler_request.status_code == 401: raise PermissionDenied if waterbutler_request.status_code == 404: Comment.update(Q('root_target', 'eq', root_target), data={'root_target': None}) raise NotFound if is_server_error(waterbutler_request.status_code): raise ServiceUnavailableError(detail='Could not retrieve files information at this time.') try: waterbutler_request.json()['data'] except KeyError: raise ServiceUnavailableError(detail='Could not retrieve files information at this time.') if check_permissions: # May raise a permission denied self.check_object_permissions(self.request, comment) return comment
def addon_delete_file_node(self, node, user, event_type, payload): """ Get addon StoredFileNode(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 = FileNode.resolve_class(provider, FileNode.ANY).find( Q('provider', 'eq', provider) & Q('node', 'eq', node) & Q('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': StoredFileNode.remove_one(item.stored_object) else: try: file_node = FileNode.resolve_class(provider, FileNode.FILE).find_one( Q('node', 'eq', node) & Q('materialized_path', 'eq', materialized_path) ) except NoResultsFound: file_node = None if file_node and not TrashedFileNode.load(file_node._id): file_node.delete(user=user)
def addon_delete_file_node(self, node, user, event_type, payload): """ Get addon StoredFileNode(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 = FileNode.resolve_class( provider, FileNode.ANY).find( Q('provider', 'eq', provider) & Q('node', 'eq', node) & Q('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': StoredFileNode.remove_one(item.stored_object) else: try: file_node = FileNode.resolve_class( provider, FileNode.FILE).find_one( Q('node', 'eq', node) & Q('materialized_path', 'eq', materialized_path)) except NoResultsFound: file_node = None if file_node and not TrashedFileNode.load(file_node._id): file_node.delete(user=user)
def get_comment(self, check_permissions=True): pk = self.kwargs[self.comment_lookup_url_kwarg] try: comment = Comment.find_one(Q('_id', 'eq', pk) & Q('root_target', 'ne', None)) except NoResultsFound: raise NotFound # Deleted root targets still appear as tuples in the database and are included in # the above query, requiring an additional check if comment.root_target is None: raise NotFound if isinstance(comment.root_target, StoredFileNode): root_target = comment.root_target if root_target.provider == 'osfstorage': try: StoredFileNode.find( Q('node', 'eq', comment.node._id) & Q('_id', 'eq', root_target._id) & Q('is_file', 'eq', True) ) except NoResultsFound: Comment.update(Q('root_target', 'eq', root_target), data={'root_target': None}) del comment.node.commented_files[root_target._id] raise NotFound else: if root_target.provider == 'dropbox': root_target = DropboxFile.load(comment.root_target._id) url = waterbutler_api_url_for(comment.node._id, root_target.provider, root_target.path, meta=True) waterbutler_request = requests.get( url, cookies=self.request.COOKIES, headers={'Authorization': self.request.META.get('HTTP_AUTHORIZATION')}, ) if waterbutler_request.status_code == 401: raise PermissionDenied if waterbutler_request.status_code == 404: Comment.update(Q('root_target', 'eq', root_target), data={'root_target': None}) raise NotFound if is_server_error(waterbutler_request.status_code): raise ServiceUnavailableError(detail='Could not retrieve files information at this time.') try: waterbutler_request.json()['data'] except KeyError: raise ServiceUnavailableError(detail='Could not retrieve files information at this time.') if check_permissions: # May raise a permission denied self.check_object_permissions(self.request, comment) return comment
def _render_conference_node(node, idx, conf): try: record = next(x for x in StoredFileNode.find( Q('node', 'eq', node) & Q('is_file', 'eq', True)).limit(1)).wrapped() download_count = record.get_download_count() download_url = node.web_url_for( 'addon_view_or_download_file', path=record.path.strip('/'), provider='osfstorage', action='download', _absolute=True, ) except StopIteration: download_url = '' download_count = 0 author = node.visible_contributors[0] tags = [tag._id for tag in node.tags] return { 'id': idx, 'title': node.title, 'nodeUrl': node.url, 'author': author.family_name, 'authorUrl': node.creator.url, 'category': 'talk' if 'talk' in node.system_tags else 'poster', 'download': download_count, 'downloadUrl': download_url, 'dateCreated': str(node.date_created), 'confName': conf.name, 'confUrl': web_url_for('conference_results', meeting=conf.endpoint), 'tags': ' '.join(tags) }
def test_exclude_correct_file_materialized_path(self): test_file = self.ProviderFile.create( node=self.project, path='/test', name='test', materialized_path='/test', ) test_file.save() targets = StoredFileNode.find(Q('provider', 'ne', 'osfstorage')) assert_not_in(test_file, targets)
def test_fix_incorrect_file_materialized_path(self): test_file = self.ProviderFile.create( node=self.project, path='/path', name='path', materialized_path='path', ) test_file.save() targets = StoredFileNode.find(Q('provider', 'ne', 'osfstorage')) update_file_materialized_paths(targets) test_file.reload() assert_equal(test_file.materialized_path, '/path')
def _render_conference_node(node, idx, conf): try: record = next( x for x in StoredFileNode.find( Q('node', 'eq', node) & Q('is_file', 'eq', True) ).limit(1) ).wrapped() download_count = record.get_download_count() download_url = node.web_url_for( 'addon_view_or_download_file', path=record.path.strip('/'), provider='osfstorage', action='download', _absolute=True, ) except StopIteration: download_url = '' download_count = 0 author = node.visible_contributors[0] tags = [tag._id for tag in node.tags] return { 'id': idx, 'title': node.title, 'nodeUrl': node.url, 'author': author.family_name if author.family_name else author.fullname, 'authorUrl': node.creator.url, 'category': conf.field_names['submission1'] if conf.field_names['submission1'] in node.system_tags else conf.field_names['submission2'], 'download': download_count, 'downloadUrl': download_url, 'dateCreated': node.date_created.isoformat(), 'confName': conf.name, 'confUrl': web_url_for('conference_results', meeting=conf.endpoint), 'tags': ' '.join(tags) }
def get_target(self, node_id, target_id): node = Node.load(target_id) comment = Comment.load(target_id) target_file = StoredFileNode.load(target_id) if node: if node_id == target_id: return node else: raise ValueError('Cannot post comment to another node.') elif comment: if comment.node._id == node_id: return comment else: raise ValueError('Cannot post reply to comment on another node.') elif target_file: if target_file.provider not in osf_settings.ADDONS_COMMENTABLE: raise ValueError('Comments are not supported for this file provider.') elif target_file.node._id != node_id: raise ValueError('Cannot post comment to file on another node.') else: return target_file else: raise ValueError('Invalid comment target.')
def tearDown(self): super(FixMaterializedPathTestMixin, self).tearDown() StoredFileNode.remove()
def main(): files = StoredFileNode.find(Q('provider', 'ne', 'osfstorage')) update_file_materialized_paths(files)