Esempio n. 1
0
def node_register_template_page_post(auth, **kwargs):
    node = kwargs['node'] or kwargs['project']
    data = request.json

    if settings.DISK_SAVING_MODE:
        raise HTTPError(http.METHOD_NOT_ALLOWED, redirect_url=node.url)

    # Sanitize payload data
    clean_data = process_payload(data)

    template = kwargs['template']
    # TODO: Using json.dumps because node_to_use.registered_meta's values are
    # expected to be strings (not dicts). Eventually migrate all these to be
    # dicts, as this is unnecessary
    schema = MetaSchema.find(Q('name', 'eq',
                               template)).sort('-schema_version')[0]
    register = node.register_node(
        schema,
        auth,
        template,
        json.dumps(clean_data),
    )

    return {
        'status': 'success',
        'result': register.url,
    }, http.CREATED
Esempio n. 2
0
    def setUp(self):
        super(PreregViewsTests, self).setUp()

        ensure_schemas()
        
        self.osf_user = factories.AuthUserFactory()
        password = fake.password(),
        self.user = User.objects.create(
            email=fake.email(),
            first_name=fake.first_name(),
            last_name=fake.last_name(),
            osf_id=self.osf_user._id
        )
        self.user.set_password(password)
        self.user.save()
        self.logged_in = self.client.login(username=self.user.email, password=password)
        PREREG_GROUP.user_set.add(self.user)
        PREREG_GROUP.save()

        self.prereg_schema = MetaSchema.find_one(
            Q('name', 'eq', 'Prereg Challenge') &
            Q('schema_version', 'eq', 2)
        )
        self.other_schema = MetaSchema.find(
            Q('name', 'ne', 'Prereg Challenge') &
            Q('schema_version', 'eq', 2)
        )[0]
Esempio n. 3
0
def node_register_template_page_post(auth, node, **kwargs):
    data = request.json

    if settings.DISK_SAVING_MODE:
        raise HTTPError(
            http.METHOD_NOT_ALLOWED,
            redirect_url=node.url
        )

    # Sanitize payload data
    clean_data = process_payload(data)

    template = kwargs['template']
    # TODO: Using json.dumps because node_to_use.registered_meta's values are
    # expected to be strings (not dicts). Eventually migrate all these to be
    # dicts, as this is unnecessary
    schema = MetaSchema.find(
        Q('name', 'eq', template)
    ).sort('-schema_version')[0]
    register = node.register_node(
        schema, auth, template, json.dumps(clean_data),
    )

    return {
        'status': 'success',
        'result': register.url,
    }, http.CREATED
Esempio n. 4
0
def node_register_template_page_post(auth, node, **kwargs):
    data = request.json

    if settings.DISK_SAVING_MODE:
        raise HTTPError(http.METHOD_NOT_ALLOWED, redirect_url=node.url)

    # Sanitize payload data
    clean_data = process_payload(data)

    template = kwargs['template']
    # TODO: Using json.dumps because node_to_use.registered_meta's values are
    # expected to be strings (not dicts). Eventually migrate all these to be
    # dicts, as this is unnecessary
    schema = MetaSchema.find(Q('name', 'eq',
                               template)).sort('-schema_version')[0]

    # Create the registration
    register = node.register_node(
        schema,
        auth,
        template,
        json.dumps(clean_data),
    )

    if data.get('registrationChoice', 'immediate') == 'embargo':
        embargo_end_date = parse_date(data['embargoEndDate'], ignoretz=True)

        # Initiate embargo
        try:
            register.embargo_registration(auth.user, embargo_end_date)
            register.save()
        except ValidationValueError as err:
            raise HTTPError(http.BAD_REQUEST,
                            data=dict(message_long=err.message))
        if settings.ENABLE_ARCHIVER:
            register.archive_job.meta = {
                'embargo_urls': {
                    contrib._id:
                    project_utils.get_embargo_urls(register, contrib)
                    for contrib in node.active_contributors()
                }
            }
            register.archive_job.save()
    else:
        register.set_privacy('public', auth, log=False)
        for child in register.get_descendants_recursive(lambda n: n.primary):
            child.set_privacy('public', auth, log=False)

    push_status_message(
        'Files are being copied to the newly created registration, and you will receive an email '
        'notification containing a link to the registration when the copying is finished.',
        kind='info',
        trust=False)

    return {
        'status': 'initiated',
        'urls': {
            'registrations': node.web_url_for('node_registrations')
        }
    }, http.CREATED
Esempio n. 5
0
def node_register_template_page(auth, node, metaschema_id, **kwargs):
    if node.is_registration and bool(node.registered_schema):
        try:
            meta_schema = MetaSchema.find_one(
                Q('_id', 'eq', metaschema_id)
            )
        except NoResultsFound:
            # backwards compatability for old urls, lookup by name
            try:
                meta_schema = MetaSchema.find(
                    Q('name', 'eq', _id_to_name(metaschema_id))
                ).sort('-schema_version')[0]
            except IndexError:
                raise HTTPError(http.NOT_FOUND, data={
                    'message_short': 'Invalid schema name',
                    'message_long': 'No registration schema with that name could be found.'
                })
        if meta_schema not in node.registered_schema:
            raise HTTPError(http.BAD_REQUEST, data={
                'message_short': 'Invalid schema',
                'message_long': 'This registration has no registration supplment with that name.'
            })

        ret = _view_project(node, auth, primary=True)
        ret['node']['registered_schema'] = serialize_meta_schema(meta_schema)
        return ret
    else:
        status.push_status_message(
            'You have been redirected to the project\'s registrations page. From here you can initiate a new Draft Registration to complete the registration process',
            trust=False
        )
        return redirect(node.web_url_for('node_registrations', view=kwargs.get('template')))
Esempio n. 6
0
def node_register_template_page(auth, node, **kwargs):

    template_name = kwargs['template'].replace(' ', '_')
    # Error to raise if template can't be found
    not_found_error = HTTPError(
        http.NOT_FOUND,
        data=dict(
            message_short='Template not found.',
            message_long='The registration template you entered '
                         'in the URL is not valid.'
        )
    )

    if node.is_registration and node.registered_meta:
        registered = True
        payload = node.registered_meta.get(to_mongo(template_name))
        payload = json.loads(payload)
        payload = unprocess_payload(payload)

        if node.registered_schema:
            meta_schema = node.registered_schema
        else:
            try:
                meta_schema = MetaSchema.find_one(
                    Q('name', 'eq', template_name) &
                    Q('schema_version', 'eq', 1)
                )
            except NoResultsFound:
                raise not_found_error
    else:
        # Anyone with view access can see this page if the current node is
        # registered, but only admins can view the registration page if not
        # TODO: Test me @jmcarp
        if not node.has_permission(auth.user, ADMIN):
            raise HTTPError(http.FORBIDDEN)
        registered = False
        payload = None
        metaschema_query = MetaSchema.find(
            Q('name', 'eq', template_name)
        ).sort('-schema_version')
        if metaschema_query:
            meta_schema = metaschema_query[0]
        else:
            raise not_found_error
    schema = meta_schema.schema

    # TODO: Notify if some components will not be registered

    ret = {
        'template_name': template_name,
        'schema': json.dumps(schema),
        'metadata_version': meta_schema.metadata_version,
        'schema_version': meta_schema.schema_version,
        'registered': registered,
        'payload': payload,
        'children_ids': node.nodes._to_primary_keys(),
    }
    ret.update(_view_project(node, auth, primary=True))
    return ret
Esempio n. 7
0
def node_register_template_page_post(auth, node, **kwargs):
    data = request.json

    if settings.DISK_SAVING_MODE:
        raise HTTPError(
            http.METHOD_NOT_ALLOWED,
            redirect_url=node.url
        )

    # Sanitize payload data
    clean_data = process_payload(data)

    template = kwargs['template']
    # TODO: Using json.dumps because node_to_use.registered_meta's values are
    # expected to be strings (not dicts). Eventually migrate all these to be
    # dicts, as this is unnecessary
    schema = MetaSchema.find(
        Q('name', 'eq', template)
    ).sort('-schema_version')[0]

    # Create the registration
    register = node.register_node(
        schema, auth, template, json.dumps(clean_data),
    )

    if data.get('registrationChoice', 'immediate') == 'embargo':
        embargo_end_date = parse_date(data['embargoEndDate'], ignoretz=True)

        # Initiate embargo
        try:
            register.embargo_registration(auth.user, embargo_end_date)
            register.save()
        except ValidationValueError as err:
            raise HTTPError(http.BAD_REQUEST, data=dict(message_long=err.message))
        if settings.ENABLE_ARCHIVER:
            register.archive_job.meta = {
                'embargo_urls': {
                    contrib._id: project_utils.get_embargo_urls(register, contrib)
                    for contrib in node.active_contributors()
                }
            }
            register.archive_job.save()
    else:
        register.set_privacy('public', auth, log=False)
        for child in register.get_descendants_recursive(lambda n: n.primary):
            child.set_privacy('public', auth, log=False)

    push_status_message('Files are being copied to the newly created registration, and you will receive an email '
                        'notification containing a link to the registration when the copying is finished.',
                        kind='info',
                        trust=False)

    return {
        'status': 'initiated',
        'urls': {
            'registrations': node.web_url_for('node_registrations')
        }
    }, http.CREATED
Esempio n. 8
0
def node_register_template_page(auth, **kwargs):

    node = kwargs['node'] or kwargs['project']

    template_name = kwargs['template'].replace(' ', '_')
    # Error to raise if template can't be found
    not_found_error = HTTPError(
        http.NOT_FOUND,
        data=dict(message_short='Template not found.',
                  message_long='The registration template you entered '
                  'in the URL is not valid.'))

    if node.is_registration and node.registered_meta:
        registered = True
        payload = node.registered_meta.get(to_mongo(template_name))
        payload = json.loads(payload)
        payload = unprocess_payload(payload)

        if node.registered_schema:
            meta_schema = node.registered_schema
        else:
            try:
                meta_schema = MetaSchema.find_one(
                    Q('name', 'eq', template_name)
                    & Q('schema_version', 'eq', 1))
            except NoResultsFound:
                raise not_found_error
    else:
        # Anyone with view access can see this page if the current node is
        # registered, but only admins can view the registration page if not
        # TODO: Test me @jmcarp
        if not node.has_permission(auth.user, ADMIN):
            raise HTTPError(http.FORBIDDEN)
        registered = False
        payload = None
        metaschema_query = MetaSchema.find(Q(
            'name', 'eq', template_name)).sort('-schema_version')
        if metaschema_query:
            meta_schema = metaschema_query[0]
        else:
            raise not_found_error
    schema = meta_schema.schema

    # TODO: Notify if some components will not be registered

    ret = {
        'template_name': template_name,
        'schema': json.dumps(schema),
        'metadata_version': meta_schema.metadata_version,
        'schema_version': meta_schema.schema_version,
        'registered': registered,
        'payload': payload,
        'children_ids': node.nodes._to_primary_keys(),
    }
    ret.update(_view_project(node, auth, primary=True))
    return ret
Esempio n. 9
0
def main(dev=False, _db=None):
    _db = _db or db
    init_app(routes=False)
    count = 0
    skipped = 0
    scripts_utils.add_file_logger(logger, __file__)
    logger.info("Iterating over all registrations")

    # convert registered_schema to list field
    prepare_nodes()
    ensure_schemas()

    node_documents = _db['node'].find({'is_registration': True})
    for node in node_documents:
        registered_schemas = []
        registered_meta = {}
        schemas = node['registered_meta']
        if not schemas:
            logger.info(
                'Node: {0} is registered but has no registered_meta'.format(
                    node['_id']))
            continue
        for schema_id, schema in schemas.iteritems():
            name = _id_to_name(from_mongo(schema_id))
            schema = from_json_or_fail(schema)
            # append matching schema to node.registered_schema
            try:
                meta_schema = MetaSchema.find(Q(
                    'name', 'eq', name)).sort('-schema_version')[0]
            except IndexError as e:
                logger.error(
                    'No MetaSchema matching name: {0} found for node: {1}.'.
                    format(name, node['_id']))
                # Skip over missing schemas
                skipped += 1
                if dev:
                    continue
                else:
                    raise e
            else:
                registered_meta[meta_schema._id] = {
                    key: {
                        'value': value
                    }
                    for key, value in schema.items()
                }
                registered_schemas.append(meta_schema._id)
        db['node'].update({'_id': node['_id']}, {
            '$set': {
                'registered_meta': registered_meta,
                'registered_schema': registered_schemas
            }
        })
        count = count + 1
    logger.info('Done with {0} nodes migrated and {1} nodes skipped.'.format(
        count, skipped))
Esempio n. 10
0
 def test_get_metaschemas_all(self):
     url = api_url_for('get_metaschemas', include='all')
     res = self.app.get(url)
     assert_equal(res.status_code, http.OK)
     assert_equal(
         len(res.json['meta_schemas']),
         len([
             schema for schema in MetaSchema.find()
             if schema.name in ACTIVE_META_SCHEMAS
         ]))
Esempio n. 11
0
 def test_get_metaschemas_all(self):
     url = api_url_for('get_metaschemas', include='all')
     res = self.app.get(url)
     assert_equal(res.status_code, http.OK)
     assert_equal(len(res.json['meta_schemas']), len(
         [
             schema for schema in MetaSchema.find()
             if schema.name in ACTIVE_META_SCHEMAS
         ]
     ))
Esempio n. 12
0
def main(dev=False, _db=None):
    _db = _db or db
    init_app(routes=False)
    count = 0
    skipped = 0
    scripts_utils.add_file_logger(logger, __file__)
    logger.info("Iterating over all registrations")

    # convert registered_schema to list field
    prepare_nodes()
    ensure_schemas()

    node_documents = _db['node'].find({'is_registration': True})
    for node in node_documents:
        registered_schemas = []
        registered_meta = {}
        schemas = node['registered_meta']
        if not schemas:
            logger.info('Node: {0} is registered but has no registered_meta'.format(node['_id']))
            continue
        for schema_id, schema in schemas.iteritems():
            name = _id_to_name(from_mongo(schema_id))
            schema = from_json_or_fail(schema)
            # append matching schema to node.registered_schema
            try:
                meta_schema = MetaSchema.find(
                    Q('name', 'eq', name)
                ).sort('-schema_version')[0]
            except IndexError as e:
                logger.error('No MetaSchema matching name: {0} found for node: {1}.'.format(name, node['_id']))
                # Skip over missing schemas
                skipped += 1
                if dev:
                    continue
                else:
                    raise e
            else:
                registered_meta[meta_schema._id] = {
                    key: {
                        'value': value
                    }
                    for key, value in schema.items()
                }
                registered_schemas.append(meta_schema._id)
        db['node'].update(
            {'_id': node['_id']},
            {'$set': {
                'registered_meta': registered_meta,
                'registered_schema': registered_schemas
            }}
        )
        count = count + 1
    logger.info('Done with {0} nodes migrated and {1} nodes skipped.'.format(count, skipped))
Esempio n. 13
0
def node_register_template_page_post(auth, node, **kwargs):
    data = request.json

    if settings.DISK_SAVING_MODE:
        raise HTTPError(http.METHOD_NOT_ALLOWED, redirect_url=node.url)

    # Sanitize payload data
    clean_data = process_payload(data)

    template = kwargs['template']
    # TODO: Using json.dumps because node_to_use.registered_meta's values are
    # expected to be strings (not dicts). Eventually migrate all these to be
    # dicts, as this is unnecessary
    schema = MetaSchema.find(Q('name', 'eq',
                               template)).sort('-schema_version')[0]

    # Create the registration
    register = node.register_node(
        schema,
        auth,
        template,
        json.dumps(clean_data),
    )
    register.is_public = False
    for node in register.get_descendants_recursive():
        node.is_public = False
        node.save()
    try:
        if data.get('registrationChoice', 'immediate') == 'embargo':
            # Initiate embargo
            embargo_end_date = parse_date(data['embargoEndDate'],
                                          ignoretz=True)
            register.embargo_registration(auth.user, embargo_end_date)
        else:
            register.require_approval(auth.user)
        register.save()
    except ValidationValueError as err:
        raise HTTPError(http.BAD_REQUEST, data=dict(message_long=err.message))

    push_status_message(language.AFTER_REGISTER_ARCHIVING,
                        kind='info',
                        trust=False)

    return {
        'status': 'initiated',
        'urls': {
            'registrations': node.web_url_for('node_registrations')
        }
    }, http.CREATED
Esempio n. 14
0
def node_register_template_page_post(auth, node, **kwargs):
    data = request.json

    if settings.DISK_SAVING_MODE:
        raise HTTPError(
            http.METHOD_NOT_ALLOWED,
            redirect_url=node.url
        )

    # Sanitize payload data
    clean_data = process_payload(data)

    template = kwargs['template']
    # TODO: Using json.dumps because node_to_use.registered_meta's values are
    # expected to be strings (not dicts). Eventually migrate all these to be
    # dicts, as this is unnecessary
    schema = MetaSchema.find(
        Q('name', 'eq', template)
    ).sort('-schema_version')[0]

    # Create the registration
    register = node.register_node(
        schema, auth, template, json.dumps(clean_data),
    )
    register.is_public = False
    for node in register.get_descendants_recursive():
        node.is_public = False
        node.save()
    try:
        if data.get('registrationChoice', 'immediate') == 'embargo':
            # Initiate embargo
            embargo_end_date = parse_date(data['embargoEndDate'], ignoretz=True)
            register.embargo_registration(auth.user, embargo_end_date)
        else:
            register.require_approval(auth.user)
        register.save()
    except ValidationValueError as err:
        raise HTTPError(http.BAD_REQUEST, data=dict(message_long=err.message))

    push_status_message(language.AFTER_REGISTER_ARCHIVING,
                        kind='info',
                        trust=False)

    return {
        'status': 'initiated',
        'urls': {
            'registrations': node.web_url_for('node_registrations')
        }
    }, http.CREATED
Esempio n. 15
0
def node_register_template_page(auth, node, metaschema_id, **kwargs):
    if node.is_registration and bool(node.registered_schema):
        try:
            meta_schema = MetaSchema.find_one(Q('_id', 'eq', metaschema_id))
        except NoResultsFound:
            # backwards compatability for old urls, lookup by name
            try:
                meta_schema = MetaSchema.find(
                    Q('name', 'eq',
                      _id_to_name(metaschema_id))).sort('-schema_version')[0]
            except IndexError:
                raise HTTPError(
                    http.NOT_FOUND,
                    data={
                        'message_short':
                        'Invalid schema name',
                        'message_long':
                        'No registration schema with that name could be found.'
                    })
        if not node.registered_schema.filter(id=meta_schema.id).exists():
            raise HTTPError(
                http.BAD_REQUEST,
                data={
                    'message_short':
                    'Invalid schema',
                    'message_long':
                    'This registration has no registration supplment with that name.'
                })

        ret = _view_project(node, auth, primary=True)
        my_meta = serialize_meta_schema(meta_schema)
        if has_anonymous_link(node, auth):
            for indx, schema_page in enumerate(my_meta['schema']['pages']):
                for idx, schema_question in enumerate(
                        schema_page['questions']):
                    if schema_question['title'] in settings.ANONYMIZED_TITLES:
                        del my_meta['schema']['pages'][indx]['questions'][idx]
        ret['node']['registered_schema'] = serialize_meta_schema(meta_schema)
        return ret
    else:
        status.push_status_message(
            'You have been redirected to the project\'s registrations page. From here you can initiate a new Draft Registration to complete the registration process',
            trust=False)
        return redirect(
            node.web_url_for('node_registrations',
                             view=kwargs.get('template')))
Esempio n. 16
0
    def test_factory(self):
        draft = DraftRegistrationFactory()
        assert_is_not_none(draft.branched_from)
        assert_is_not_none(draft.initiator)
        assert_is_not_none(draft.registration_schema)

        user = AuthUserFactory()
        draft = DraftRegistrationFactory(initiator=user)
        assert_equal(draft.initiator, user)

        node = ProjectFactory()
        draft = DraftRegistrationFactory(branched_from=node)
        assert_equal(draft.branched_from, node)
        assert_equal(draft.initiator, node.creator)

        # Pick an arbitrary v2 schema
        schema = MetaSchema.find(Q("schema_version", "eq", 2))[0]
        data = {"some": "data"}
        draft = DraftRegistrationFactory(registration_schema=schema, registration_metadata=data)
        assert_equal(draft.registration_schema, schema)
        assert_equal(draft.registration_metadata, data)
Esempio n. 17
0
    def test_factory(self):
        draft = DraftRegistrationFactory()
        assert_is_not_none(draft.branched_from)
        assert_is_not_none(draft.initiator)
        assert_is_not_none(draft.registration_schema)

        user = AuthUserFactory()
        draft = DraftRegistrationFactory(initiator=user)
        assert_equal(draft.initiator, user)

        node = ProjectFactory()
        draft = DraftRegistrationFactory(branched_from=node)
        assert_equal(draft.branched_from, node)
        assert_equal(draft.initiator, node.creator)

        # Pick an arbitrary v2 schema
        schema = MetaSchema.find(Q('schema_version', 'eq', 2))[0]
        data = {'some': 'data'}
        draft = DraftRegistrationFactory(registration_schema=schema,
                                         registration_metadata=data)
        assert_equal(draft.registration_schema, schema)
        assert_equal(draft.registration_metadata, data)
Esempio n. 18
0
def node_register_template_page(auth, node, metaschema_id, **kwargs):
    if node.is_registration and bool(node.registered_schema):
        try:
            meta_schema = MetaSchema.find_one(Q('_id', 'eq', metaschema_id))
        except NoResultsFound:
            # backwards compatability for old urls, lookup by name
            try:
                meta_schema = MetaSchema.find(
                    Q('name', 'eq',
                      _id_to_name(metaschema_id))).sort('-schema_version')[0]
            except IndexError:
                raise HTTPError(
                    http.NOT_FOUND,
                    data={
                        'message_short':
                        'Invalid schema name',
                        'message_long':
                        'No registration schema with that name could be found.'
                    })
        if meta_schema not in node.registered_schema:
            raise HTTPError(
                http.BAD_REQUEST,
                data={
                    'message_short':
                    'Invalid schema',
                    'message_long':
                    'This registration has no registration supplment with that name.'
                })

        ret = _view_project(node, auth, primary=True)
        ret['node']['registered_schema'] = serialize_meta_schema(meta_schema)
        return ret
    else:
        status.push_status_message(
            'You have been redirected to the project\'s registrations page. From here you can initiate a new Draft Registration to complete the registration process',
            trust=False)
        return redirect(
            node.web_url_for('node_registrations',
                             view=kwargs.get('template')))
Esempio n. 19
0
def node_register_template_page_post(auth, **kwargs):
    node = kwargs['node'] or kwargs['project']
    data = request.json

    # Sanitize payload data
    clean_data = process_payload(data)

    template = kwargs['template']
    # TODO: Using json.dumps because node_to_use.registered_meta's values are
    # expected to be strings (not dicts). Eventually migrate all these to be
    # dicts, as this is unnecessary
    schema = MetaSchema.find(
        Q('name', 'eq', template)
    ).sort('-schema_version')[0]
    register = node.register_node(
        schema, auth, template, json.dumps(clean_data),
    )

    return {
        'status': 'success',
        'result': register.url,
    }, http.CREATED
Esempio n. 20
0
    def setUp(self):
        super(PreregViewsTests, self).setUp()

        ensure_schemas()

        self.osf_user = factories.AuthUserFactory()
        password = fake.password(),
        self.user = User.objects.create(email=fake.email(),
                                        first_name=fake.first_name(),
                                        last_name=fake.last_name(),
                                        osf_id=self.osf_user._id)
        self.user.set_password(password)
        self.user.save()
        self.logged_in = self.client.login(username=self.user.email,
                                           password=password)
        PREREG_GROUP.user_set.add(self.user)
        PREREG_GROUP.save()

        self.prereg_schema = MetaSchema.find_one(
            Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2))
        self.other_schema = MetaSchema.find(
            Q('name', 'ne', 'Prereg Challenge')
            & Q('schema_version', 'eq', 2))[0]
Esempio n. 21
0
 def get_queryset(self):
     schemas = MetaSchema.find(Q('name', 'in', ACTIVE_META_SCHEMAS) &
                               Q('schema_version', 'eq', LATEST_SCHEMA_VERSION))
     return schemas