コード例 #1
0
ファイル: test_preprints.py プロジェクト: adlius/osf.io
    def setUp(self):
        super(TestSetPreprintFile, self).setUp()

        self.user = AuthUserFactory()
        self.auth = Auth(user=self.user)
        self.read_write_user = AuthUserFactory()
        self.read_write_user_auth = Auth(user=self.read_write_user)

        self.project = ProjectFactory(creator=self.user)
        self.file = OsfStorageFile.create(
            node=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        self.file.save()

        self.file_two = OsfStorageFile.create(
            node=self.project,
            path='/pandapanda.txt',
            name='pandapanda.txt',
            materialized_path='/pandapanda.txt')
        self.file_two.save()

        self.project.add_contributor(self.read_write_user, permissions=[permissions.WRITE])
        self.project.save()

        self.preprint = PreprintFactory(project=self.project, finish=False)
コード例 #2
0
    def setUp(self):
        super(TestSetPreprintFile, self).setUp()

        self.user = AuthUserFactory()
        self.auth = Auth(user=self.user)
        self.read_write_user = AuthUserFactory()
        self.read_write_user_auth = Auth(user=self.read_write_user)

        self.project = ProjectFactory(creator=self.user)
        self.file = OsfStorageFile.create(
            target=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        self.file.save()

        self.file_two = OsfStorageFile.create(
            target=self.project,
            path='/pandapanda.txt',
            name='pandapanda.txt',
            materialized_path='/pandapanda.txt')
        self.file_two.save()

        self.project.add_contributor(self.read_write_user, permissions=[permissions.WRITE])
        self.project.save()

        self.preprint = PreprintFactory(project=self.project, finish=False)
コード例 #3
0
    def test_not_just_primary_file_returned(self):
        filename = 'my second file'
        second_file = OsfStorageFile.create(
            target_object_id=self.preprint.id,
            target_content_type=ContentType.objects.get_for_model(
                self.preprint),
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))

        second_file.save()
        from addons.osfstorage import settings as osfstorage_settings

        second_file.create_version(
            self.user, {
                'object': '06d80e',
                'service': 'cloud',
                osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
            }, {
                'size': 1337,
                'contentType': 'img/png'
            }).save()
        second_file.parent = self.preprint.root_folder
        second_file.save()

        assert len(self.preprint.files.all()) == 2
        res = self.app.get(self.url, auth=self.user.auth)
        assert res.status_code == 200

        data = res.json['data']
        assert len(data) == 2
        assert data[0]['id'] == self.preprint.primary_file._id
コード例 #4
0
    def test_not_just_primary_file_returned(self):
        filename = 'my second file'
        second_file = OsfStorageFile.create(
            target_object_id=self.preprint.id,
            target_content_type=ContentType.objects.get_for_model(self.preprint),
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))

        second_file.save()
        from addons.osfstorage import settings as osfstorage_settings

        second_file.create_version(self.user, {
            'object': '06d80e',
            'service': 'cloud',
            osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
        }, {
            'size': 1337,
            'contentType': 'img/png'
        }).save()
        second_file.parent = self.preprint.root_folder
        second_file.save()

        assert len(self.preprint.files.all()) == 2
        res = self.app.get(self.url, auth=self.user.auth)
        assert res.status_code == 200

        data = res.json['data']
        assert len(data) == 2
        assert data[0]['id'] == self.preprint.primary_file._id
コード例 #5
0
ファイル: factories.py プロジェクト: keale/osf.io
    def _create(cls, target_class, *args, **kwargs):
        update_task_patcher = mock.patch(
            'website.preprints.tasks.on_preprint_updated.si')
        update_task_patcher.start()

        finish = kwargs.pop('finish', True)
        set_doi = kwargs.pop('set_doi', True)
        is_published = kwargs.pop('is_published', True)
        instance = cls._build(target_class, *args, **kwargs)
        file_size = kwargs.pop('file_size', 1337)

        doi = kwargs.pop('doi', None)
        license_details = kwargs.pop('license_details', None)
        filename = kwargs.pop('filename', None) or 'preprint_file.txt'
        subjects = kwargs.pop('subjects', None) or [[SubjectFactory()._id]]
        instance.article_doi = doi

        user = kwargs.pop('creator', None) or instance.creator
        instance.save()

        preprint_file = OsfStorageFile.create(
            target_object_id=instance.id,
            target_content_type=ContentType.objects.get_for_model(instance),
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))

        instance.machine_state = kwargs.pop('machine_state', 'initial')
        preprint_file.save()
        from addons.osfstorage import settings as osfstorage_settings

        preprint_file.create_version(
            user, {
                'object': '06d80e',
                'service': 'cloud',
                osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
            }, {
                'size': file_size,
                'contentType': 'img/png'
            }).save()
        update_task_patcher.stop()
        if finish:
            auth = Auth(user)

            instance.set_primary_file(preprint_file, auth=auth, save=True)
            instance.set_subjects(subjects, auth=auth)
            if license_details:
                instance.set_preprint_license(license_details, auth=auth)
            instance.set_published(is_published, auth=auth)
            create_task_patcher = mock.patch(
                'website.identifiers.utils.request_identifiers')
            mock_create_identifier = create_task_patcher.start()
            if is_published and set_doi:
                mock_create_identifier.side_effect = sync_set_identifiers(
                    instance)
            create_task_patcher.stop()

        instance.save()
        return instance
コード例 #6
0
    def test_redirects(self, app, project, user):
        # test_redirect_to_node_view
        url = '/{}guids/{}/'.format(API_BASE, project._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}nodes/{}/'.format(API_DOMAIN, API_BASE,
                                              project._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_registration_view
        registration = RegistrationFactory()
        url = '/{}guids/{}/'.format(API_BASE, registration._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}registrations/{}/'.format(API_DOMAIN, API_BASE,
                                                      registration._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_collections_view
        collection = CollectionFactory()
        url = '/{}guids/{}/'.format(API_BASE, collection._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}collections/{}/'.format(API_DOMAIN, API_BASE,
                                                    collection._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_file_view
        test_file = OsfStorageFile.create(
            node=ProjectFactory(),
            path='/test',
            name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        url = '/{}guids/{}/'.format(API_BASE, guid._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}files/{}/'.format(API_DOMAIN, API_BASE,
                                              test_file._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_comment_view
        comment = CommentFactory()
        url = '/{}guids/{}/'.format(API_BASE, comment._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}comments/{}/'.format(API_DOMAIN, API_BASE,
                                                 comment._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_throws_404_for_invalid_guids
        url = '/{}guids/{}/'.format(API_BASE, 'fakeguid')
        res = app.get(url, auth=user.auth, expect_errors=True)
        assert res.status_code == 404
コード例 #7
0
ファイル: test_guid_detail.py プロジェクト: leb2dg/osf.io
    def test_redirects(self, app, project, user):
        # test_redirect_to_node_view
        url = '/{}guids/{}/'.format(API_BASE, project._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}nodes/{}/'.format(
            API_DOMAIN, API_BASE, project._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_registration_view
        registration = RegistrationFactory()
        url = '/{}guids/{}/'.format(API_BASE, registration._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}registrations/{}/'.format(
            API_DOMAIN, API_BASE, registration._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_collections_view
        collection = CollectionFactory()
        url = '/{}guids/{}/'.format(API_BASE, collection._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}collections/{}/'.format(
            API_DOMAIN, API_BASE, collection._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_file_view
        test_file = OsfStorageFile.create(
            node=ProjectFactory(),
            path='/test', name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        url = '/{}guids/{}/'.format(API_BASE, guid._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}files/{}/'.format(
            API_DOMAIN, API_BASE, test_file._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_to_comment_view
        comment = CommentFactory()
        url = '/{}guids/{}/'.format(API_BASE, comment._id)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}comments/{}/'.format(
            API_DOMAIN, API_BASE, comment._id)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_throws_404_for_invalid_guids
        url = '/{}guids/{}/'.format(API_BASE, 'fakeguid')
        res = app.get(url, auth=user.auth, expect_errors=True)
        assert res.status_code == 404
コード例 #8
0
ファイル: factories.py プロジェクト: envobe/osf.io
    def _create(cls,
                target_class,
                project=None,
                is_public=True,
                filename='preprint_file.txt',
                provider=None,
                doi=None,
                external_url=None,
                is_published=True,
                subjects=None,
                finish=True,
                *args,
                **kwargs):
        save_kwargs(**kwargs)
        user = None
        if project:
            user = project.creator
        user = kwargs.get('user') or kwargs.get(
            'creator') or user or UserFactory()
        kwargs['creator'] = user
        # Original project to be converted to a preprint
        project = project or AbstractNodeFactory(*args, **kwargs)
        if user._id not in project.permissions:
            project.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=False)
        project.save()
        project.reload()

        file = OsfStorageFile.create(node=project,
                                     path='/{}'.format(filename),
                                     name=filename,
                                     materialized_path='/{}'.format(filename))
        file.save()

        preprint = target_class(node=project, provider=provider)

        auth = Auth(project.creator)

        if finish:
            preprint.set_primary_file(file, auth=auth)
            subjects = subjects or [[SubjectFactory()._id]]
            preprint.set_subjects(subjects, auth=auth)
            preprint.set_published(is_published, auth=auth)

        if not preprint.is_published:
            project._has_abandoned_preprint = True

        project.preprint_article_doi = doi
        project.save()
        preprint.save()

        return preprint
コード例 #9
0
ファイル: factories.py プロジェクト: aaxelb/osf.io
    def _create(cls, target_class, *args, **kwargs):
        update_task_patcher = mock.patch('website.preprints.tasks.on_preprint_updated.si')
        update_task_patcher.start()

        finish = kwargs.pop('finish', True)
        set_doi = kwargs.pop('set_doi', True)
        is_published = kwargs.pop('is_published', True)
        instance = cls._build(target_class, *args, **kwargs)

        doi = kwargs.pop('doi', None)
        license_details = kwargs.pop('license_details', None)
        filename = kwargs.pop('filename', None) or 'preprint_file.txt'
        subjects = kwargs.pop('subjects', None) or [[SubjectFactory()._id]]
        instance.article_doi = doi

        instance.machine_state = kwargs.pop('machine_state', 'initial')
        user = kwargs.pop('creator', None) or instance.creator
        instance.save()

        preprint_file = OsfStorageFile.create(
            target_object_id=instance.id,
            target_content_type=ContentType.objects.get_for_model(instance),
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))

        preprint_file.save()
        from addons.osfstorage import settings as osfstorage_settings

        preprint_file.create_version(user, {
            'object': '06d80e',
            'service': 'cloud',
            osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
        }, {
            'size': 1337,
            'contentType': 'img/png'
        }).save()
        update_task_patcher.stop()
        if finish:
            auth = Auth(user)

            instance.set_primary_file(preprint_file, auth=auth, save=True)
            instance.set_subjects(subjects, auth=auth)
            if license_details:
                instance.set_preprint_license(license_details, auth=auth)
            instance.set_published(is_published, auth=auth)
            create_task_patcher = mock.patch('website.identifiers.utils.request_identifiers')
            mock_create_identifier = create_task_patcher.start()
            if is_published and set_doi:
                mock_create_identifier.side_effect = sync_set_identifiers(instance)
            create_task_patcher.stop()

        instance.save()
        return instance
コード例 #10
0
ファイル: test_guid_detail.py プロジェクト: adlius/osf.io
 def test_redirect_to_file_view(self):
     test_file = OsfStorageFile.create(
         node=ProjectFactory(),
         path='/test',
         name='test',
         materialized_path='/test',
     )
     test_file.save()
     guid = test_file.get_guid(create=True)
     url = '/{}guids/{}/'.format(API_BASE, guid._id)
     res = self.app.get(url, auth=self.user.auth)
     redirect_url = '{}{}files/{}/'.format(API_DOMAIN, API_BASE, test_file._id)
     assert_equal(res.status_code, 302)
     assert_equal(res.location, redirect_url)
コード例 #11
0
    def test_admin_can_set_file(self):
        initial_file = self.preprint.primary_file
        file = OsfStorageFile.create(node=self.project,
                                     path='/panda.txt',
                                     name='panda.txt',
                                     materialized_path='/panda.txt')
        file.save()

        self.preprint.set_primary_file(file, auth=Auth(self.user), save=True)

        self.preprint.reload()
        self.preprint.node.reload()
        assert_not_equal(initial_file._id, self.preprint.primary_file._id)
        assert_equal(file._id, self.preprint.primary_file._id)
コード例 #12
0
    def test_nonadmin_cannot_set_file(self):
        initial_file = self.preprint.primary_file
        file = OsfStorageFile.create(
            target=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        file.save()

        with assert_raises(PermissionsError):
            self.preprint.set_primary_file(file, auth=Auth(self.write_contrib), save=True)

        self.preprint.reload()
        self.preprint.node.reload()
        assert_equal(initial_file._id, self.preprint.primary_file._id)
コード例 #13
0
ファイル: test_preprints.py プロジェクト: adlius/osf.io
    def test_admin_can_set_file(self):
        initial_file = self.preprint.primary_file
        file = OsfStorageFile.create(
            node=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        file.save()

        self.preprint.set_primary_file(file, auth=Auth(self.user), save=True)

        self.preprint.reload()
        self.preprint.node.reload()
        assert_not_equal(initial_file._id, self.preprint.primary_file._id)
        assert_equal(file._id, self.preprint.primary_file._id)
コード例 #14
0
ファイル: test_preprints.py プロジェクト: adlius/osf.io
    def test_nonadmin_cannot_set_file(self):
        initial_file = self.preprint.primary_file
        file = OsfStorageFile.create(
            node=self.project,
            path='/panda.txt',
            name='panda.txt',
            materialized_path='/panda.txt')
        file.save()

        with assert_raises(PermissionsError):
            self.preprint.set_primary_file(file, auth=Auth(self.write_contrib), save=True)

        self.preprint.reload()
        self.preprint.node.reload()
        assert_equal(initial_file._id, self.preprint.primary_file._id)
コード例 #15
0
ファイル: test_guid_detail.py プロジェクト: adlius/osf.io
    def test_redirect_when_viewing_private_project_file_through_view_only_link(self):
        project = ProjectFactory()
        test_file = OsfStorageFile.create(
            node=project,
            path='/test',
            name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        view_only_link = self._add_private_link(project)

        url = '/{}guids/{}/?view_only={}'.format(API_BASE, guid._id, view_only_link.key)
        res = self.app.get(url, auth=AuthUserFactory().auth)
        redirect_url = '{}{}files/{}/?view_only={}'.format(API_DOMAIN, API_BASE, test_file._id, view_only_link.key)
        assert_equal(res.status_code, 302)
        assert_equal(res.location, redirect_url)
コード例 #16
0
ファイル: factories.py プロジェクト: adlius/osf.io
    def _create(cls, target_class, project=None, is_public=True, filename='preprint_file.txt', provider=None, 
                doi=None, external_url=None, is_published=True, subjects=None, finish=True, *args, **kwargs):
        save_kwargs(**kwargs)
        user = None
        if project:
            user = project.creator
        user = kwargs.get('user') or kwargs.get('creator') or user or UserFactory()
        kwargs['creator'] = user
        # Original project to be converted to a preprint
        project = project or AbstractNodeFactory(*args, **kwargs)
        if user._id not in project.permissions:
            project.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=False
            )
        project.save()
        project.reload()

        file = OsfStorageFile.create(
            node=project,
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))
        file.save()

        preprint = target_class(node=project, provider=provider)

        auth = Auth(project.creator)

        if finish:
            preprint.set_primary_file(file, auth=auth)
            subjects = subjects or [[SubjectFactory()._id]]
            preprint.set_subjects(subjects, auth=auth)
            preprint.set_published(is_published, auth=auth)
        
        if not preprint.is_published:
            project._has_abandoned_preprint = True

        project.preprint_article_doi = doi
        project.save()
        preprint.save()

        return preprint
コード例 #17
0
ファイル: test_guid_detail.py プロジェクト: leb2dg/osf.io
    def test_redirects_through_view_only_link(self, app, project, user):

        # test_redirect_when_viewing_private_project_through_view_only_link
        view_only_link = PrivateLinkFactory(anonymous=False)
        view_only_link.nodes.add(project)
        view_only_link.save()

        url = '/{}guids/{}/?view_only={}'.format(
            API_BASE, project._id, view_only_link.key)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}nodes/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, project._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_when_viewing_private_project_file_through_view_only_link
        test_file = OsfStorageFile.create(
            node=project,
            path='/test',
            name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        url = '/{}guids/{}/?view_only={}'.format(
            API_BASE, guid._id, view_only_link.key)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}files/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, test_file._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_when_viewing_private_project_comment_through_view_only_link
        comment = CommentFactory(node=project)
        url = '/{}guids/{}/?view_only={}'.format(
            API_BASE, comment._id, view_only_link.key)
        res = app.get(url, auth=AuthUserFactory().auth)
        redirect_url = '{}{}comments/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, comment._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url
コード例 #18
0
    def test_redirects_through_view_only_link(self, app, project, user):

        # test_redirect_when_viewing_private_project_through_view_only_link
        view_only_link = PrivateLinkFactory(anonymous=False)
        view_only_link.nodes.add(project)
        view_only_link.save()

        url = '/{}guids/{}/?view_only={}'.format(API_BASE, project._id,
                                                 view_only_link.key)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}nodes/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, project._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_when_viewing_private_project_file_through_view_only_link
        test_file = OsfStorageFile.create(
            node=project,
            path='/test',
            name='test',
            materialized_path='/test',
        )
        test_file.save()
        guid = test_file.get_guid(create=True)
        url = '/{}guids/{}/?view_only={}'.format(API_BASE, guid._id,
                                                 view_only_link.key)
        res = app.get(url, auth=user.auth)
        redirect_url = '{}{}files/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, test_file._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url

        # test_redirect_when_viewing_private_project_comment_through_view_only_link
        comment = CommentFactory(node=project)
        url = '/{}guids/{}/?view_only={}'.format(API_BASE, comment._id,
                                                 view_only_link.key)
        res = app.get(url, auth=AuthUserFactory().auth)
        redirect_url = '{}{}comments/{}/?view_only={}'.format(
            API_DOMAIN, API_BASE, comment._id, view_only_link.key)
        assert res.status_code == 302
        assert res.location == redirect_url
コード例 #19
0
    def _build(cls,
               target_class,
               project=None,
               filename='preprint_file.txt',
               provider=None,
               doi=None,
               external_url=None,
               is_published=True,
               subjects=None,
               finish=True,
               *args,
               **kwargs):
        user = None
        if project:
            user = project.creator
        user = kwargs.get('user') or kwargs.get(
            'creator') or user or UserFactory()
        kwargs['creator'] = user
        # Original project to be converted to a preprint
        project = project or ProjectFactory(*args, **kwargs)
        project.save()
        if not project.is_contributor(user):
            project.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=True)

        file = OsfStorageFile.create(node=project,
                                     path='/{}'.format(filename),
                                     name=filename,
                                     materialized_path='/{}'.format(filename))
        file.save()

        from addons.osfstorage import settings as osfstorage_settings

        file.create_version(
            user, {
                'object': '06d80e',
                'service': 'cloud',
                osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
            }, {
                'size': 1337,
                'contentType': 'img/png'
            }).save()

        preprint = target_class(node=project, provider=provider)

        auth = Auth(project.creator)

        if finish:
            preprint.set_primary_file(file, auth=auth)
            subjects = subjects or [[SubjectFactory()._id]]
            preprint.save()
            preprint.set_subjects(subjects, auth=auth)
            preprint.set_published(is_published, auth=auth)

        if not preprint.is_published:
            project._has_abandoned_preprint = True

        project.preprint_article_doi = doi
        project.save()
        return preprint
コード例 #20
0
ファイル: factories.py プロジェクト: ariahendrawan713/osf.io
    def _create(cls, target_class, *args, **kwargs):
        update_task_patcher = mock.patch(
            'website.preprints.tasks.on_preprint_updated.si')
        update_task_patcher.start()

        finish = kwargs.pop('finish', True)
        is_published = kwargs.pop('is_published', True)
        instance = cls._build(target_class, *args, **kwargs)

        doi = kwargs.pop('doi', None)
        license_details = kwargs.pop('license_details', None)
        filename = kwargs.pop('filename', None) or 'preprint_file.txt'
        subjects = kwargs.pop('subjects', None) or [[SubjectFactory()._id]]
        instance.node.preprint_article_doi = doi

        instance.machine_state = kwargs.pop('machine_state', 'initial')

        user = kwargs.pop('creator', None) or instance.node.creator
        if not instance.node.is_contributor(user):
            instance.node.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=True)

        preprint_file = OsfStorageFile.create(
            node=instance.node,
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))
        preprint_file.save()

        from addons.osfstorage import settings as osfstorage_settings

        preprint_file.create_version(
            user, {
                'object': '06d80e',
                'service': 'cloud',
                osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
            }, {
                'size': 1337,
                'contentType': 'img/png'
            }).save()

        if finish:
            auth = Auth(user)

            instance.set_primary_file(preprint_file, auth=auth, save=True)
            instance.set_subjects(subjects, auth=auth)
            if license_details:
                instance.set_preprint_license(license_details, auth=auth)

            create_task_patcher = mock.patch(
                'website.preprints.tasks.get_and_set_preprint_identifiers.si')
            mock_create_identifier = create_task_patcher.start()
            if is_published:
                mock_create_identifier.side_effect = sync_set_identifiers(
                    instance)

            instance.set_published(is_published, auth=auth)
            create_task_patcher.stop()

        if not instance.is_published:
            instance.node._has_abandoned_preprint = True

        instance.node.save()
        instance.save()
        return instance
コード例 #21
0
ファイル: factories.py プロジェクト: leb2dg/osf.io
    def _create(cls, target_class, *args, **kwargs):
        update_task_patcher = mock.patch('website.preprints.tasks.on_preprint_updated.si')
        update_task_patcher.start()

        finish = kwargs.pop('finish', True)
        is_published = kwargs.pop('is_published', True)
        instance = cls._build(target_class, *args, **kwargs)

        doi = kwargs.pop('doi', None)
        license_details = kwargs.pop('license_details', None)
        filename = kwargs.pop('filename', None) or 'preprint_file.txt'
        subjects = kwargs.pop('subjects', None) or [[SubjectFactory()._id]]
        instance.node.preprint_article_doi = doi

        instance.machine_state = kwargs.pop('machine_state', 'initial')

        user = kwargs.pop('creator', None) or instance.node.creator
        if not instance.node.is_contributor(user):
            instance.node.add_contributor(
                contributor=user,
                permissions=permissions.CREATOR_PERMISSIONS,
                log=False,
                save=True
            )

        preprint_file = OsfStorageFile.create(
            node=instance.node,
            path='/{}'.format(filename),
            name=filename,
            materialized_path='/{}'.format(filename))
        preprint_file.save()

        from addons.osfstorage import settings as osfstorage_settings

        preprint_file.create_version(user, {
            'object': '06d80e',
            'service': 'cloud',
            osfstorage_settings.WATERBUTLER_RESOURCE: 'osf',
        }, {
            'size': 1337,
            'contentType': 'img/png'
        }).save()

        if finish:
            auth = Auth(user)

            instance.set_primary_file(preprint_file, auth=auth, save=True)
            instance.set_subjects(subjects, auth=auth)
            if license_details:
                instance.set_preprint_license(license_details, auth=auth)

            create_task_patcher = mock.patch('website.preprints.tasks.get_and_set_preprint_identifiers.si')
            mock_create_identifier = create_task_patcher.start()
            if is_published:
                mock_create_identifier.side_effect = sync_set_identifiers(instance)

            instance.set_published(is_published, auth=auth)
            create_task_patcher.stop()

        if not instance.is_published:
            instance.node._has_abandoned_preprint = True

        instance.node.save()
        instance.save()
        return instance