def _set_up_registration_with_comment(self): self.registration = RegistrationFactory(creator=self.user, comment_level='private') self.registration_wiki = NodeWikiFactory(node=self.registration, user=self.user) self.registration_comment = CommentFactory(node=self.registration, target=Guid.load(self.registration_wiki._id), user=self.user) self.comment_url = '/{}comments/{}/'.format(API_BASE, self.registration_comment._id) reply_target = Guid.load(self.registration_comment._id) self.registration_comment_reply = CommentFactory(node=self.registration, target=reply_target, user=self.user)
def test_migrate_project_comment_reply(self): comment = self._set_up_comment_with_target(root_target=self.project, target=self.project) reply = self._set_up_comment_with_target(root_target=self.project, target=comment) update_comment_targets_to_guids() reply.reload() assert_equal(reply.root_target, Guid.load(self.project._id)) assert_equal(reply.target, Guid.load(comment._id))
def _set_up_public_project_with_comment(self): self.public_project = ProjectFactory.create(is_public=True, creator=self.user, comment_level='private') self.public_project.add_contributor(self.contributor, save=True) self.public_wiki = NodeWikiFactory(node=self.public_project, user=self.user) self.public_comment = CommentFactory(node=self.public_project, target=Guid.load(self.public_wiki._id), user=self.user) reply_target = Guid.load(self.public_comment._id) self.public_comment_reply = CommentFactory(node=self.public_project, target=reply_target, user=self.user) self.public_url = '/{}comments/{}/'.format(API_BASE, self.public_comment._id) self.public_comment_payload = self._set_up_payload(self.public_comment._id)
def update_file_guid_referent(self, node, event_type, payload, user=None): if event_type == 'addon_file_moved' or event_type == 'addon_file_renamed': source = payload['source'] destination = payload['destination'] source_node = Node.load(source['node']['_id']) destination_node = node file_guids = FileNode.resolve_class(source['provider'], FileNode.ANY).get_file_guids( materialized_path=source['materialized'] if source['provider'] != 'osfstorage' else source['path'], provider=source['provider'], node=source_node) if event_type == 'addon_file_renamed' and source['provider'] in settings.ADDONS_BASED_ON_IDS: return if event_type == 'addon_file_moved' and (source['provider'] == destination['provider'] and source['provider'] in settings.ADDONS_BASED_ON_IDS) and source_node == destination_node: return for guid in file_guids: obj = Guid.load(guid) if source_node != destination_node and Comment.find(Q('root_target', 'eq', guid)).count() != 0: update_comment_node(guid, source_node, destination_node) if source['provider'] != destination['provider'] or source['provider'] != 'osfstorage': old_file = FileNode.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()
def test_public_node_non_contributor_commenter_can_delete_wiki_comment(self): project = ProjectFactory(is_public=True, comment_level='public') test_wiki = NodeWikiFactory(node=project, user=self.user) comment = CommentFactory(node=project, target=Guid.load(test_wiki), user=self.non_contributor) url = '/{}comments/{}/'.format(API_BASE, comment._id) res = self.app.delete_json_api(url, auth=self.non_contributor.auth) assert_equal(res.status_code, 204)
def test_private_node_only_logged_in_contributor_commenter_can_delete_own_reply(self): self._set_up_private_project_with_comment() reply_target = Guid.load(self.comment._id) reply = CommentFactory(node=self.private_project, target=reply_target, user=self.user) reply_url = '/{}comments/{}/'.format(API_BASE, reply) res = self.app.delete_json_api(reply_url, auth=self.user.auth) assert_equal(res.status_code, 204)
def _set_up_registration_with_comment(self): self.registration = RegistrationFactory(creator=self.user, comment_level='private') self.registration_file = test_utils.create_test_file(self.registration, self.user) self.registration_comment = CommentFactory(node=self.registration, target=self.registration_file.get_guid(), user=self.user) self.comment_url = '/{}comments/{}/'.format(API_BASE, self.registration_comment._id) reply_target = Guid.load(self.registration_comment._id) self.registration_comment_reply = CommentFactory(node=self.registration, target=reply_target, user=self.user)
def migrate_guids(guid_type, provider): cls = models.FileNode.resolve_class(provider, models.FileNode.FILE) for guid in paginated(guid_type): # Note: No metadata is populated here # It will be populated whenever this guid is next viewed if guid.node is None: logger.warning('{}({})\'s node is None; skipping'.format(guid_type, guid._id)) continue if guid.waterbutler_path in ('/{{ revision.osfDownloadUrl }}', '/{{ currentVersion().osfDownloadUrl }}', '/{{ currentVersion().osfDownloadUrl }}', '/{{ node.urls.files }}', '/{{ revision.extra.user.url }}'): logger.warning('{}({})\'s is a googlebot path; skipping'.format(guid_type, guid._id)) continue logger.debug('Migrating guid {} ({})'.format(guid._id, guid.waterbutler_path)) try: file_node = cls( node=guid.node, path=guid.waterbutler_path, name=guid.waterbutler_path, materialized_path=guid.waterbutler_path, ) file_node.save() except exceptions.KeyExistsException: file_node = cls.find_one( Q('node', 'eq', guid.node) & Q('path', 'eq', guid.waterbutler_path) ) logger.warning('{!r}({}) has multiple guids'.format(file_node.wrapped(), guid._id)) actual_guid = Guid.load(guid._id) actual_guid.referent = file_node actual_guid.save()
def get_paginated_response(self, data): """Add number of unread comments to links.meta when viewing list of comments filtered by a target node, file or wiki page.""" response = super(CommentPagination, self).get_paginated_response(data) response_dict = response.data kwargs = self.request.parser_context['kwargs'].copy() if self.request.query_params.get('related_counts', False): target_id = self.request.query_params.get('filter[target]', None) node_id = kwargs.get('node_id', None) node = Node.load(node_id) user = self.request.user if target_id and not user.is_anonymous() and node.is_contributor(user): root_target = Guid.load(target_id) if root_target: page = getattr(root_target.referent, 'root_target_page', None) if page: if not len(data): unread = 0 else: unread = Comment.find_n_unread(user=user, node=node, page=page, root_id=target_id) if self.request.version < '2.1': response_dict['links']['meta']['unread'] = unread else: response_dict['meta']['unread'] = unread return Response(response_dict)
def test_migrate_file_comment_reply(self): test_file = create_test_file(node=self.project, user=self.project.creator) comment = self._set_up_comment_with_target(root_target=test_file, target=test_file) reply = self._set_up_comment_with_target(root_target=test_file, target=comment) update_comment_targets_to_guids() reply.reload() assert_equal(reply.root_target, test_file.get_guid()) assert_equal(reply.target, Guid.load(comment._id))
def _set_up_registration_with_comment(self): self.registration = RegistrationFactory(creator=self.user) self.registration_url = '/{}registrations/{}/'.format(API_BASE, self.registration._id) self.registration_comment = CommentFactory(node=self.registration, user=self.user) self.comment_url = '/{}comments/{}/'.format(API_BASE, self.registration_comment._id) reply_target = Guid.load(self.registration_comment._id) self.registration_comment_reply = CommentFactory(node=self.registration, target=reply_target, user=self.user) self.replies_url = '/{}registrations/{}/comments/?filter[target]={}'.format(API_BASE, self.registration._id, self.registration_comment._id)
def get_redirect_url(self, **kwargs): guid = Guid.load(kwargs['guids']) if guid: referent = guid.referent if getattr(referent, 'absolute_api_v2_url', None): return referent.absolute_api_v2_url else: raise EndpointNotImplementedError() return None
def test_public_node_non_contributor_commenter_can_update_wiki_comment(self): project = ProjectFactory(is_public=True) test_wiki = NodeWikiFactory(node=project, user=self.user) comment = CommentFactory(node=project, target=Guid.load(test_wiki), user=self.non_contributor) url = '/{}comments/{}/'.format(API_BASE, comment._id) payload = self._set_up_payload(comment._id) res = self.app.put_json_api(url, payload, auth=self.non_contributor.auth) assert_equal(res.status_code, 200) assert_equal(payload['data']['attributes']['content'], res.json['data']['attributes']['content'])
def test_find_unread_includes_comment_replies(self): project = ProjectFactory() user = UserFactory() project.add_contributor(user) project.save() comment = CommentFactory(node=project, user=user) reply = CommentFactory(node=project, target=Guid.load(comment._id), user=project.creator) n_unread = Comment.find_n_unread(user=user, node=project, page='node') assert_equal(n_unread, 1)
def update_comment_targets_to_guids(): comments = Comment.find() for comment in comments: # Skip comments on deleted files if not comment.target: continue if isinstance(comment.root_target, StoredFileNode): comment.root_target = comment.root_target.get_guid() elif comment.root_target: comment.root_target = Guid.load(comment.root_target._id) if isinstance(comment.target, StoredFileNode): comment.target = comment.target.get_guid() else: comment.target = Guid.load(comment.target._id) comment.save() logger.info("Migrated root_target and target for comment {0}".format(comment._id))
def get_target(self, node_id, target_id): target = Guid.load(target_id) if not target or not getattr(target.referent, 'belongs_to_node', None): raise ValueError('Invalid comment target.') elif not target.referent.belongs_to_node(node_id): raise ValueError('Cannot post to comment target on another node.') elif isinstance(target.referent, StoredFileNode) and target.referent.provider not in osf_settings.ADDONS_COMMENTABLE: raise ValueError('Comments are not supported for this file provider.') return target
def test_wiki_has_comments_link(self): self._set_up_public_project_with_wiki_page() res = self.app.get(self.public_url) assert_equal(res.status_code, 200) url = res.json['data']['relationships']['comments']['links']['related']['href'] CommentFactory(node=self.public_project, target=Guid.load(self.public_wiki._id), user=self.user) res = self.app.get(url) assert_equal(res.status_code, 200) assert_equal(res.json['data'][0]['type'], 'comments')
def test_private_node_only_logged_in_contributor_commenter_can_delete_own_reply( self): self._set_up_private_project_with_comment() reply_target = Guid.load(self.comment._id) reply = CommentFactory(node=self.private_project, target=reply_target, user=self.user) reply_url = '/{}comments/{}/'.format(API_BASE, reply) res = self.app.delete_json_api(reply_url, auth=self.user.auth) assert_equal(res.status_code, 204)
def test_public_node_non_contributor_commenter_can_delete_wiki_comment( self): project = ProjectFactory(is_public=True, comment_level='public') test_wiki = NodeWikiFactory(node=project, user=self.user) comment = CommentFactory(node=project, target=Guid.load(test_wiki), user=self.non_contributor) url = '/{}comments/{}/'.format(API_BASE, comment._id) res = self.app.delete_json_api(url, auth=self.non_contributor.auth) assert_equal(res.status_code, 204)
def _set_up_public_project_with_comment(self): self.public_project = ProjectFactory.create(is_public=True, creator=self.user, comment_level='private') self.public_project.add_contributor(self.contributor, save=True) self.public_wiki = NodeWikiFactory(node=self.public_project, user=self.user) self.public_comment = CommentFactory(node=self.public_project, target=Guid.load( self.public_wiki._id), user=self.user) reply_target = Guid.load(self.public_comment._id) self.public_comment_reply = CommentFactory(node=self.public_project, target=reply_target, user=self.user) self.public_url = '/{}comments/{}/'.format(API_BASE, self.public_comment._id) self.public_comment_payload = self._set_up_payload( self.public_comment._id)
def update_comment_targets_to_guids(): comments = Comment.find() for comment in comments: # Skip comments on deleted files if not comment.target: continue if isinstance(comment.root_target, StoredFileNode): comment.root_target = comment.root_target.get_guid() elif comment.root_target: comment.root_target = Guid.load(comment.root_target._id) if isinstance(comment.target, StoredFileNode): comment.target = comment.target.get_guid() else: comment.target = Guid.load(comment.target._id) comment.save() logger.info('Migrated root_target and target for comment {0}'.format( comment._id))
def _build(cls, target_class, *args, **kwargs): node = kwargs.pop("node", None) or NodeFactory() user = kwargs.pop("user", None) or node.creator target = kwargs.pop("target", None) or Guid.load(node._id) content = kwargs.pop("content", None) or "Test comment." instance = target_class(node=node, user=user, target=target, content=content, *args, **kwargs) if isinstance(target.referent, target_class): instance.root_target = target.referent.root_target else: instance.root_target = target return instance
def test_public_node_non_contributor_commenter_cannot_update_own_wiki_comment_if_comment_level_private(self): project = ProjectFactory(is_public=True) test_wiki = NodeWikiFactory(node=project, user=self.user) comment = CommentFactory(node=project, target=Guid.load(test_wiki), user=self.non_contributor) project.comment_level = 'private' project.save() url = '/{}comments/{}/'.format(API_BASE, comment._id) payload = self._set_up_payload(comment._id) res = self.app.put_json_api(url, payload, auth=self.non_contributor.auth, expect_errors=True) assert_equal(res.status_code, 403) assert_equal(res.json['errors'][0]['detail'], 'You do not have permission to perform this action.')
def test_wiki_has_comments_link(self): self._set_up_public_project_with_wiki_page() res = self.app.get(self.public_url) assert_equal(res.status_code, 200) url = res.json['data']['relationships']['comments']['links'][ 'related']['href'] CommentFactory(node=self.public_project, target=Guid.load(self.public_wiki._id), user=self.user) res = self.app.get(url) assert_equal(res.status_code, 200) assert_equal(res.json['data'][0]['type'], 'comments')
def test_private_node_only_logged_in_contributor_commenter_can_undelete_own_reply(self): self._set_up_private_project_with_comment() reply_target = Guid.load(self.comment._id) reply = CommentFactory(node=self.private_project, target=reply_target, user=self.user) reply_url = '/{}comments/{}/'.format(API_BASE, reply) reply.is_deleted = True reply.save() payload = self._set_up_payload(reply._id, has_content=False) res = self.app.patch_json_api(reply_url, payload, auth=self.user.auth) assert_equal(res.status_code, 200) assert_false(res.json['data']['attributes']['deleted']) assert_equal(res.json['data']['attributes']['content'], reply.content)
def get_target(self, node_id, target_id): target = Guid.load(target_id) if not target or not getattr(target.referent, 'belongs_to_node', None): raise ValueError('Invalid comment target.') elif not target.referent.belongs_to_node(node_id): raise ValueError('Cannot post to comment target on another node.') elif isinstance( target.referent, StoredFileNode ) and target.referent.provider not in osf_settings.ADDONS_COMMENTABLE: raise ValueError( 'Comments are not supported for this file provider.') return target
def get_object(self): comment = self.get_comment() if isinstance(comment.target.referent, Node): comment_node = comment.target.referent elif isinstance(comment.target.referent, (NodeWikiPage, StoredFileNode)): comment_node = Guid.load(comment.target.referent.node).referent if comment_node.is_registration: self.serializer_class = RegistrationCommentDetailSerializer return comment
def _set_up_public_project_comment_reports(self): self.public_project = ProjectFactory.create(is_public=True, creator=self.user) self.public_project.add_contributor(contributor=self.contributor, save=True) self.public_wiki = NodeWikiFactory(node=self.public_project, user=self.user) self.public_comment = CommentFactory.build(node=self.public_project, target=Guid.load(self.public_wiki._id), user=self.contributor) self.public_comment.reports = {self.user._id: { 'category': 'spam', 'text': 'This is spam', 'date': datetime.utcnow(), 'retracted': False, }} self.public_comment.save() self.public_url = '/{}comments/{}/reports/{}/'.format(API_BASE, self.public_comment._id, self.user._id)
def _set_up_private_project_with_comment(self): self.private_project = ProjectFactory.create(is_public=False, creator=self.user, comment_level='private') self.private_project.add_contributor(self.contributor, save=True) with mock.patch('osf.models.AbstractNode.update_search'): self.wiki = NodeWikiFactory(node=self.private_project, user=self.user) self.comment = CommentFactory(node=self.private_project, target=Guid.load(self.wiki._id), user=self.user) self.private_url = '/{}comments/{}/'.format(API_BASE, self.comment._id) self.payload = self._set_up_payload(self.comment._id)
def _set_up_registration_with_comment(self): self.registration = RegistrationFactory(creator=self.user) self.registration_url = '/{}registrations/{}/'.format( API_BASE, self.registration._id) self.registration_comment = CommentFactory(node=self.registration, user=self.user) self.comment_url = '/{}comments/{}/'.format( API_BASE, self.registration_comment._id) reply_target = Guid.load(self.registration_comment._id) self.registration_comment_reply = CommentFactory( node=self.registration, target=reply_target, user=self.user) self.replies_url = '/{}registrations/{}/comments/?filter[target]={}'.format( API_BASE, self.registration._id, self.registration_comment._id)
def _set_up_registration_with_comment(self): self.registration = RegistrationFactory(creator=self.user, comment_level='private') self.registration_file = test_utils.create_test_file( self.registration, self.user) self.registration_comment = CommentFactory( node=self.registration, target=self.registration_file.get_guid(), user=self.user) self.comment_url = '/{}comments/{}/'.format( API_BASE, self.registration_comment._id) reply_target = Guid.load(self.registration_comment._id) self.registration_comment_reply = CommentFactory( node=self.registration, target=reply_target, user=self.user)
def _set_up_public_project_comment_reports(self, comment_level='public'): self.public_project = ProjectFactory.create(is_public=True, creator=self.user, comment_level=comment_level) self.public_project.add_contributor(contributor=self.contributor, save=True) self.public_wiki = NodeWikiFactory(node=self.public_project, user=self.user) self.public_comment = CommentFactory.build(node=self.public_project, target=Guid.load(self.public_wiki._id), user=self.contributor) self.public_comment.reports = self.public_comment.reports or {} self.public_comment.reports[self.user._id] = { 'category': 'spam', 'text': 'This is spam', 'date': datetime.utcnow(), 'retracted': False, } self.public_comment.save() self.public_url = '/{}comments/{}/reports/'.format(API_BASE, self.public_comment._id)
def migrate_osfstorage_guids(): for guid in paginated(OsfStorageGuidFile): if '{{' in guid.waterbutler_path: logger.warning('OsfStorageGuidFile {} ({}) looks like a google bot link; skipping'.format(guid._id, guid.waterbutler_path.strip('/'))) continue referent = models.StoredFileNode.load(guid.waterbutler_path.strip('/')) if referent is None: logger.warning('OsfStorageGuidFile {} ({}) resolved to None; skipping'.format(guid._id, guid.waterbutler_path.strip('/'))) continue logger.debug('Migrating guid {}'.format(guid._id)) actual_guid = Guid.load(guid._id) assert actual_guid is not None actual_guid.referent = referent actual_guid.save()
def _set_up_public_project_comment_reports(self, comment_level='public'): self.public_project = ProjectFactory.create(is_public=True, creator=self.user, comment_level=comment_level) self.public_project.add_contributor(contributor=self.contributor, save=True) with mock.patch('osf.models.AbstractNode.update_search'): self.public_wiki = NodeWikiFactory(node=self.public_project, user=self.user) self.public_comment = CommentFactory.build(node=self.public_project, target=Guid.load(self.public_wiki._id), user=self.contributor) self.public_comment.reports = self.public_comment.reports or {} self.public_comment.reports[self.user._id] = { 'category': 'spam', 'text': 'This is spam', 'date': timezone.now(), 'retracted': False, } self.public_comment.save() self.public_url = '/{}comments/{}/reports/'.format(API_BASE, self.public_comment._id)
def test_private_node_only_logged_in_contributor_commenter_can_undelete_own_reply( self): self._set_up_private_project_with_comment() reply_target = Guid.load(self.comment._id) reply = CommentFactory(node=self.private_project, target=reply_target, user=self.user) reply_url = '/{}comments/{}/'.format(API_BASE, reply) reply.is_deleted = True reply.save() payload = self._set_up_payload(reply._id, has_content=False) res = self.app.patch_json_api(reply_url, payload, auth=self.user.auth) assert_equal(res.status_code, 200) assert_false(res.json['data']['attributes']['deleted']) assert_equal(res.json['data']['attributes']['content'], reply.content)
def _update_comments_timestamp(auth, node, page=Comment.OVERVIEW, root_id=None): if node.is_contributor(auth.user): enqueue_postcommit_task(ban_url, (node, ), {}, celery=False, once_per_request=True) if root_id is not None: guid_obj = Guid.load(root_id) if guid_obj is not None: enqueue_postcommit_task(ban_url, (guid_obj.referent, ), {}, celery=False, once_per_request=True) # update node timestamp if page == Comment.OVERVIEW: root_id = node._id auth.user.comments_viewed_timestamp[root_id] = datetime.utcnow() auth.user.save() return {root_id: auth.user.comments_viewed_timestamp[root_id].isoformat()} else: return {}
def _build(cls, target_class, *args, **kwargs): node = kwargs.pop('node', None) or NodeFactory() user = kwargs.pop('user', None) or node.creator target = kwargs.pop('target', None) or Guid.load(node._id) content = kwargs.pop('content', None) or 'Test comment.' instance = target_class(node=node, user=user, target=target, content=content, *args, **kwargs) if isinstance(target.referent, target_class): instance.root_target = target.referent.root_target else: instance.root_target = target return instance
def _create(cls, target_class, *args, **kwargs): node = kwargs.pop('node', None) or NodeFactory() user = kwargs.pop('user', None) or node.creator target = kwargs.pop('target', None) or Guid.load(node._id) content = kwargs.pop('content', None) or 'Test comment.' instance = target_class( node=node, user=user, target=target, content=content, *args, **kwargs ) if isinstance(target.referent, target_class): instance.root_target = target.referent.root_target else: instance.root_target = target instance.save() return instance
def migrate_guids(guid_type, provider): cls = models.FileNode.resolve_class(provider, models.FileNode.FILE) for guid in paginated(guid_type): # Note: No metadata is populated here # It will be populated whenever this guid is next viewed if guid.node is None: logger.warning('{}({})\'s node is None; skipping'.format( guid_type, guid._id)) continue if guid.waterbutler_path in ('/{{ revision.osfDownloadUrl }}', '/{{ currentVersion().osfDownloadUrl }}', '/{{ currentVersion().osfDownloadUrl }}', '/{{ node.urls.files }}', '/{{ revision.extra.user.url }}'): logger.warning('{}({})\'s is a googlebot path; skipping'.format( guid_type, guid._id)) continue logger.debug('Migrating guid {} ({})'.format(guid._id, guid.waterbutler_path)) try: file_node = cls( node=guid.node, path=guid.waterbutler_path, name=guid.waterbutler_path, materialized_path=guid.waterbutler_path, ) file_node.save() except exceptions.KeyExistsException: file_node = cls.find_one( Q('node', 'eq', guid.node) & Q('path', 'eq', guid.waterbutler_path)) logger.warning('{!r}({}) has multiple guids'.format( file_node.wrapped(), guid._id)) actual_guid = Guid.load(guid._id) actual_guid.referent = file_node actual_guid.save()
def _update_comments_timestamp(auth, node, page=Comment.OVERVIEW, root_id=None): if node.is_contributor(auth.user): enqueue_postcommit_task((ban_url, (node, ))) if root_id is not None: guid_obj = Guid.load(root_id) if guid_obj is not None: enqueue_postcommit_task((ban_url, (guid_obj.referent, ))) # update node timestamp if page == Comment.OVERVIEW: root_id = node._id auth.user.comments_viewed_timestamp[root_id] = datetime.utcnow() auth.user.save() return { root_id: auth.user.comments_viewed_timestamp[root_id].isoformat() } else: return {}
def get_paginated_response(self, data): """Add number of unread comments to links.meta when viewing list of comments filtered by a target node, file or wiki page.""" response = super(CommentPagination, self).get_paginated_response(data) response_dict = response.data kwargs = self.request.parser_context["kwargs"].copy() if self.request.query_params.get("related_counts", False): target_id = self.request.query_params.get("filter[target]", None) node_id = kwargs.get("node_id", None) node = Node.load(node_id) user = self.request.user if target_id and not user.is_anonymous() and node.is_contributor(user): root_target = Guid.load(target_id) page = getattr(root_target.referent, "root_target_page", None) if page: if not len(data): unread = 0 else: unread = Comment.find_n_unread(user=user, node=node, page=page, root_id=target_id) response_dict["links"]["meta"]["unread"] = unread return Response(response_dict)
def get_target(self, node_id, target_id): target = Guid.load(target_id) if not target: raise ValueError('Invalid comment target.') referent = target.referent if isinstance(referent, Node): if node_id != target_id: raise ValueError('Cannot post comment to another node.') elif isinstance(referent, Comment): if referent.node._id != node_id: raise ValueError('Cannot post reply to comment on another node.') elif isinstance(referent, StoredFileNode): if referent.provider not in osf_settings.ADDONS_COMMENTABLE: raise ValueError('Comments are not supported for this file provider.') elif referent.node._id != node_id: raise ValueError('Cannot post comment to file on another node.') else: raise ValueError('Invalid comment target.') return target
def update_file_guid_referent(self, node, event_type, payload, user=None): if event_type == 'addon_file_moved' or event_type == 'addon_file_renamed': source = payload['source'] destination = payload['destination'] source_node = Node.load(source['node']['_id']) destination_node = node file_guids = FileNode.resolve_class( source['provider'], FileNode.ANY).get_file_guids( materialized_path=source['materialized'] if source['provider'] != 'osfstorage' else source['path'], provider=source['provider'], node=source_node) if event_type == 'addon_file_renamed' and source[ 'provider'] in settings.ADDONS_BASED_ON_IDS: return if event_type == 'addon_file_moved' and ( source['provider'] == destination['provider'] and source['provider'] in settings.ADDONS_BASED_ON_IDS ) and source_node == destination_node: return for guid in file_guids: obj = Guid.load(guid) if source_node != destination_node and Comment.find( Q('root_target', 'eq', guid)).count() != 0: update_comment_node(guid, source_node, destination_node) if source['provider'] != destination['provider'] or source[ 'provider'] != 'osfstorage': old_file = FileNode.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()
def get_has_children(self, obj): return Comment.find(Q('target', 'eq', Guid.load(obj._id))).count() > 0
def get_has_children(self, obj): return Comment.find(Q("target", "eq", Guid.load(obj._id))).count() > 0
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 file_guid is None: node = Node.load(file_info.get('nodeId')) path = file_info['data'].get('path') item = FileNode.resolve_class( provider, FileNode.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: guid = Guid.load(file_guid) item = guid.referent 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 file_guid is None: node = Node.load(file_info.get('nodeId')) path = file_info['data'].get('path') item = FileNode.resolve_class( provider, FileNode.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: guid = Guid.load(file_guid) item = guid.referent if item is None: raise Http404( 'File with guid "{}" in "{}" does not exist'.format( file_guid, question )) yield item
def _set_up_registration_with_comment(self): self.registration = RegistrationFactory(creator=self.user, comment_level='private') self.registration_wiki = NodeWikiFactory(node=self.registration, user=self.user) self.registration_comment = CommentFactory(node=self.registration, target=Guid.load(self.registration_wiki._id), user=self.user) self.registration_url = '/{}comments/{}/'.format(API_BASE, self.registration_comment._id)