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]
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')))
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
def setUp(self): super(RegistrationsTestBase, self).setUp() self.user = AuthUserFactory() self.auth = Auth(self.user) self.node = ProjectFactory(creator=self.user) self.non_admin = AuthUserFactory() self.node.add_contributor( self.non_admin, permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS, auth=self.auth, save=True ) self.non_contrib = AuthUserFactory() MetaSchema.remove() ensure_schemas() self.meta_schema = MetaSchema.find_one( Q('name', 'eq', 'Open-Ended Registration') & Q('schema_version', 'eq', 2) ) self.draft = DraftRegistrationFactory( initiator=self.user, branched_from=self.node, registration_schema=self.meta_schema, registration_metadata={ 'summary': {'value': 'Some airy'} } ) current_month = dt.datetime.now().strftime("%B") current_year = dt.datetime.now().strftime("%Y") valid_date = dt.datetime.now() + dt.timedelta(days=180) self.embargo_payload = { u'embargoEndDate': unicode(valid_date.strftime('%a, %d, %B %Y %H:%M:%S')) + u' GMT', u'registrationChoice': 'embargo' } self.invalid_embargo_date_payload = { u'embargoEndDate': u"Thu, 01 {month} {year} 05:00:00 GMT".format( month=current_month, year=str(int(current_year) - 1) ), u'registrationChoice': 'embargo' } self.immediate_payload = { 'registrationChoice': 'immediate' } self.invalid_payload = { 'registrationChoice': 'foobar' }
def setUp(self): super(TestDraftRegistrationUpdate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project ) self.prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.prereg_schema, branched_from=self.public_project ) self.registration_metadata = self.prereg_metadata(self.prereg_draft_registration) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.draft_registration._id) self.payload = { "data": { "id": self.draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": { "datacompletion": { "value": "No, data collection has not begun" }, "looked": { "value": "No" }, "comments": { "value": "This is my first registration." } } } } }
def test_required_top_level_questions_must_be_answered_on_draft( self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata['q1'] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "u'q1' is a required property")
def setUp(self): super(TestRegistrationCreate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata={ 'item29': { 'value': 'Yes' }, 'item33': { 'value': 'success' } }) self.url = '/{}nodes/{}/registrations/'.format(API_BASE, self.public_project._id) self.payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "immediate" } } }
def test_required_third_level_questions_must_be_answered_on_draft(self, mock_enqueue): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema, branched_from=self.public_project ) registration_metadata = self.prereg_metadata(prereg_draft_registration) registration_metadata['q11'] = {'value': {"question": {}}} prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "registrations", "attributes": { "registration_choice": "immediate", "draft_registration": prereg_draft_registration._id, } } } res = self.app.post_json_api(self.url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], "'value' is a required property")
def setUp(self): super(TestRegistrationCreate, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project, registration_metadata = { 'item29': {'value': 'Yes'}, 'item33': {'value': 'success'} } ) self.url = '/{}nodes/{}/registrations/'.format(API_BASE, self.public_project._id) self.payload = { "data": { "type": "registrations", "attributes": { "draft_registration": self.draft_registration._id, "registration_choice": "immediate" } } }
def test_update_draft_registration(self): metadata = { 'summary': { 'value': 'updated', 'comments': [] } } assert_not_equal(metadata, self.draft.registration_metadata) payload = { 'schema_data': metadata, 'schema_name': 'OSF-Standard Pre-Data Collection Registration', 'schema_version': 1 } url = self.node.api_url_for('update_draft_registration', draft_id=self.draft._id) res = self.app.put_json(url, payload, auth=self.user.auth) assert_equal(res.status_code, http.OK) open_ended_schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', 1) ) self.draft.reload() assert_equal(open_ended_schema, self.draft.registration_schema) assert_equal(metadata, self.draft.registration_metadata)
def setUp(self): super(TestDraftPreregChallengeRegistrationMetadataValidation, self).setUp() ensure_schemas() self.prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.prereg_schema, branched_from=self.public_project ) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.prereg_draft_registration._id) self.payload = { "data": { "id": self.prereg_draft_registration._id, "type": "draft_registrations", "attributes": { "registration_metadata": {} } } }
def setUp(self): super(TestSearchRegistrations, self).setUp() self.url = '/{}search/registrations/'.format(API_BASE) ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) with mock_archive(self.project, autocomplete=True, autoapprove=True, schema=self.schema) as registration: self.registration = registration with mock_archive(self.project_two, autocomplete=True, autoapprove=True, schema=self.schema) as registration_two: self.registration_two = registration_two with mock_archive(self.private_project, autocomplete=True, autoapprove=True, schema=self.schema) as private_registration: self.private_registration = private_registration self.private_registration.is_public = False self.private_registration.save() # TODO: This shouldn't be necessary, but tests fail if we don't do this. Investigate further. self.private_registration.update_search()
def test_required_metaschema_questions_not_required_on_post(self): prereg_schema = MetaSchema.find_one( Q("name", "eq", "Prereg Challenge") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema._id, branched_from=self.public_project ) url = "/{}nodes/{}/draft_registrations/?embed=initiator&embed=branched_from".format( API_BASE, self.public_project._id ) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata["q1"] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "draft_registrations", "attributes": { "registration_supplement": prereg_schema._id, "registration_metadata": registration_metadata, }, } } res = self.app.post_json_api(url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 201) data = res.json["data"] assert_equal(res.json["data"]["attributes"]["registration_metadata"]["q2"]["value"], "Test response") assert_equal(data["attributes"]["registration_supplement"], prereg_schema._id) assert_equal(data["embeds"]["branched_from"]["data"]["id"], self.public_project._id) assert_equal(data["embeds"]["initiator"]["data"]["id"], self.user._id)
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
def main(dry=True): init_app(set_backends=True, routes=False) if not dry: scripts_utils.add_file_logger(logger, __file__) prereg = MetaSchema.find_one( Q('name', 'eq', "Prereg Challenge")) migrate_drafts_q5_metadata(prereg) migrate_registrations_q5_metadata(prereg)
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')))
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
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.' ) return { 'status': 'initiated', 'urls': { 'registrations': node.web_url_for('node_registrations') } }, http.CREATED
def setUp(self): super(RegistrationsTestBase, self).setUp() self.user = AuthUserFactory() self.auth = Auth(self.user) self.node = ProjectFactory(creator=self.user) self.non_admin = AuthUserFactory() self.node.add_contributor(self.non_admin, permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS, auth=self.auth, save=True) self.non_contrib = AuthUserFactory() MetaSchema.remove() ensure_schemas() self.meta_schema = MetaSchema.find_one( Q('name', 'eq', 'Open-Ended Registration') & Q('schema_version', 'eq', 2)) self.draft = DraftRegistrationFactory( initiator=self.user, branched_from=self.node, registration_schema=self.meta_schema, registration_metadata={'summary': { 'value': 'Some airy' }}) current_month = timezone.now().strftime("%B") current_year = timezone.now().strftime("%Y") valid_date = timezone.now() + dt.timedelta(days=180) self.embargo_payload = { u'embargoEndDate': unicode(valid_date.strftime('%a, %d, %B %Y %H:%M:%S')) + u' GMT', u'registrationChoice': 'embargo' } self.invalid_embargo_date_payload = { u'embargoEndDate': u"Thu, 01 {month} {year} 05:00:00 GMT".format( month=current_month, year=str(int(current_year) - 1)), u'registrationChoice': 'embargo' } self.immediate_payload = {'registrationChoice': 'immediate'} self.invalid_payload = {'registrationChoice': 'foobar'}
def main(dry=True): init_app(set_backends=True, routes=False) if not dry: scripts_utils.add_file_logger(logger, __file__) veer = MetaSchema.find_one( Q('name', 'eq', "Pre-Registration in Social Psychology (van 't Veer & Giner-Sorolla, 2016): Pre-Registration")) migrate_drafts_metadata_key(veer) migrate_registrations_metadata_key(veer)
def get_prereg_schema(campaign='prereg'): from website.models import MetaSchema # noqa if campaign not in PREREG_CAMPAIGNS: raise ValueError('campaign must be one of: {}'.format(', '.join( PREREG_CAMPAIGNS.keys()))) schema_name = PREREG_CAMPAIGNS[campaign] return MetaSchema.find_one( Q('name', 'eq', schema_name) & Q('schema_version', 'eq', 2))
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))
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 ] ))
def get_prereg_schema(campaign='prereg'): from website.models import MetaSchema # noqa if campaign not in PREREG_CAMPAIGNS: raise ValueError('campaign must be one of: {}'.format(', '.join(PREREG_CAMPAIGNS.keys()))) schema_name = PREREG_CAMPAIGNS[campaign] return MetaSchema.find_one( Q('name', 'eq', schema_name) & Q('schema_version', 'eq', 2) )
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 ]))
def setUp(self): super(TestDraftRegistrationApprovals, self).setUp() self.approval = DraftRegistrationApproval( initiated_by=self.user, meta={'registration_choice': 'immediate'}) self.authorizer1 = AuthUserFactory() self.authorizer2 = AuthUserFactory() self.approval.save() self.draft.registration_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2)) self.draft.approval = self.approval self.draft.save()
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))
def setUp(self): super(TestDraftRegistrationList, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q("name", "eq", "Open-Ended Registration") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project ) self.url = "/{}nodes/{}/draft_registrations/".format(API_BASE, self.public_project._id)
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')))
def setUp(self): super(TestDraftRegistrationApprovals, self).setUp() self.approval = DraftRegistrationApproval(initiated_by=self.user, meta={"registration_choice": "immediate"}) self.authorizer1 = AuthUserFactory() self.authorizer2 = AuthUserFactory() for authorizer in [self.authorizer1, self.authorizer2]: self.approval.add_authorizer(authorizer) self.approval.save() self.draft.registration_schema = MetaSchema.find_one( Q("name", "eq", "Prereg Challenge") & Q("schema_version", "eq", 2) ) self.draft.approval = self.approval self.draft.save()
def setUp(self): super(TestDraftRegistrationCreate, self).setUp() self.url = "/{}nodes/{}/draft_registrations/".format(API_BASE, self.public_project._id) ensure_schemas() self.open_ended_metaschema = MetaSchema.find_one( Q("name", "eq", "Open-Ended Registration") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) self.payload = { "data": { "type": "draft_registrations", "attributes": {"registration_supplement": self.open_ended_metaschema._id}, } }
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]
def setUp(self): super(TestDraftRegistrationList, self).setUp() ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Open-Ended Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=self.schema, branched_from=self.public_project) self.url = '/{}nodes/{}/draft_registrations/'.format( API_BASE, self.public_project._id)
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
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
def setUp(self): super(TestDraftRegistrationDelete, self).setUp() ensure_schemas() schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, branched_from=self.public_project) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format( API_BASE, self.public_project._id, self.draft_registration._id)
def setUp(self): super(TestDraftRegistrationDelete, self).setUp() ensure_schemas() schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) self.draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=schema, branched_from=self.public_project ) self.other_project = ProjectFactory(creator=self.user) self.url = '/{}nodes/{}/draft_registrations/{}/'.format(API_BASE, self.public_project._id, self.draft_registration._id)
def test_registration_metadata_question_values_must_be_dictionaries(self): ensure_schemas() self.schema = MetaSchema.find_one( Q("name", "eq", "OSF-Standard Pre-Data Collection Registration") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) self.payload["data"]["attributes"]["registration_supplement"] = self.schema._id self.payload["data"]["attributes"]["registration_metadata"] = {} self.payload["data"]["attributes"]["registration_metadata"][ "datacompletion" ] = "No, data collection has not begun" res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json["errors"][0] assert_equal(res.status_code, 400) assert_equal(errors["detail"], "u'No, data collection has not begun' is not of type 'object'")
def test_question_in_registration_metadata_must_be_in_schema(self): ensure_schemas() self.schema = MetaSchema.find_one( Q("name", "eq", "OSF-Standard Pre-Data Collection Registration") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) self.payload["data"]["attributes"]["registration_supplement"] = self.schema._id self.payload["data"]["attributes"]["registration_metadata"] = {} self.payload["data"]["attributes"]["registration_metadata"]["q11"] = { "value": "No, data collection has not begun" } res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json["errors"][0] assert_equal(res.status_code, 400) assert_equal(errors["detail"], "Additional properties are not allowed (u'q11' was unexpected)")
def setUp(self): super(TestSearchSerializer, self).setUp() self.user = AuthUserFactory() self.project = ProjectFactory(creator=self.user, is_public=True) self.component = NodeFactory(parent=self.project, creator=self.user, is_public=True) self.file = utils.create_test_file(self.component, self.user) ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) with mock_archive(self.project, autocomplete=True, autoapprove=True, schema=self.schema) as registration: self.registration = registration
def setUp(self): super(TestDraftRegistrationApprovals, self).setUp() self.approval = DraftRegistrationApproval( initiated_by=self.user, meta={ 'registration_choice': 'immediate' } ) self.authorizer1 = AuthUserFactory() self.authorizer2 = AuthUserFactory() self.approval.save() self.draft.registration_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2) ) self.draft.approval = self.approval self.draft.save()
def setUp(self): super(TestDraftRegistrationCreate, self).setUp() self.url = '/{}nodes/{}/draft_registrations/'.format( API_BASE, self.public_project._id) ensure_schemas() self.open_ended_metaschema = MetaSchema.find_one( Q('name', 'eq', 'Open-Ended Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.payload = { "data": { "type": "draft_registrations", "attributes": { "registration_supplement": self.open_ended_metaschema._id } } }
def test_registration_supplement_must_be_active_metaschema(self): schema = MetaSchema.find_one( Q('name', 'eq', 'Open-Ended Registration') & Q('schema_version', 'eq', 1)) draft_data = { "data": { "type": "draft_registrations", "attributes": { "registration_supplement": schema._id } } } res = self.app.post_json_api(self.url, draft_data, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['errors'][0]['detail'], 'Registration supplement must be an active schema.')
def test_multiple_choice_question_value_must_match_value_in_schema(self): ensure_schemas() self.schema = MetaSchema.find_one( Q("name", "eq", "OSF-Standard Pre-Data Collection Registration") & Q("schema_version", "eq", LATEST_SCHEMA_VERSION) ) self.payload["data"]["attributes"]["registration_supplement"] = self.schema._id self.payload["data"]["attributes"]["registration_metadata"] = {} self.payload["data"]["attributes"]["registration_metadata"]["datacompletion"] = { "value": "Nope, data collection has not begun" } res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json["errors"][0] assert_equal(res.status_code, 400) assert_equal( errors["detail"], "u'Nope, data collection has not begun' is not one of [u'No, data collection has not begun', u'Yes, data collection is underway or complete']", )
def test_registration_metadata_question_values_must_be_dictionaries(self): ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.payload['data']['attributes'][ 'registration_supplement'] = self.schema._id self.payload['data']['attributes']['registration_metadata'] = {} self.payload['data']['attributes']['registration_metadata'][ 'datacompletion'] = 'No, data collection has not begun' res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal( errors['detail'], "u'No, data collection has not begun' is not of type 'object'")
def test_required_metaschema_questions_not_required_on_post(self): prereg_schema = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) prereg_draft_registration = DraftRegistrationFactory( initiator=self.user, registration_schema=prereg_schema._id, branched_from=self.public_project) url = '/{}nodes/{}/draft_registrations/?embed=initiator&embed=branched_from'.format( API_BASE, self.public_project._id) registration_metadata = self.prereg_metadata(prereg_draft_registration) del registration_metadata['q1'] prereg_draft_registration.registration_metadata = registration_metadata prereg_draft_registration.save() payload = { "data": { "type": "draft_registrations", "attributes": { "registration_supplement": prereg_schema._id, "registration_metadata": registration_metadata } } } res = self.app.post_json_api(url, payload, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 201) data = res.json['data'] assert_equal( res.json['data']['attributes']['registration_metadata']['q2'] ['value'], 'Test response') assert_equal(data['attributes']['registration_supplement'], prereg_schema._id) assert_equal(data['embeds']['branched_from']['data']['id'], self.public_project._id) assert_equal(data['embeds']['initiator']['data']['id'], self.user._id)
def test_question_in_registration_metadata_must_be_in_schema(self): ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.payload['data']['attributes'][ 'registration_supplement'] = self.schema._id self.payload['data']['attributes']['registration_metadata'] = {} self.payload['data']['attributes']['registration_metadata']['q11'] = { "value": "No, data collection has not begun" } res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal( errors['detail'], "Additional properties are not allowed (u'q11' was unexpected)")
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)
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
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)
def setUp(self): super(TestSearchRegistrations, self).setUp() self.url = '/{}search/registrations/'.format(API_BASE) ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'Replication Recipe (Brandt et al., 2013): Post-Completion') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION) ) with mock_archive(self.project, autocomplete=True, autoapprove=True, schema=self.schema) as registration: self.registration = registration with mock_archive(self.project_two, autocomplete=True, autoapprove=True, schema=self.schema) as registration_two: self.registration_two = registration_two with mock_archive(self.private_project, autocomplete=True, autoapprove=True, schema=self.schema) as private_registration: self.private_registration = private_registration self.private_registration.is_public = False self.private_registration.save()
def test_multiple_choice_question_value_must_match_value_in_schema(self): ensure_schemas() self.schema = MetaSchema.find_one( Q('name', 'eq', 'OSF-Standard Pre-Data Collection Registration') & Q('schema_version', 'eq', LATEST_SCHEMA_VERSION)) self.payload['data']['attributes'][ 'registration_supplement'] = self.schema._id self.payload['data']['attributes']['registration_metadata'] = {} self.payload['data']['attributes']['registration_metadata'][ 'datacompletion'] = { "value": "Nope, data collection has not begun" } res = self.app.post_json_api(self.url, self.payload, auth=self.user.auth, expect_errors=True) errors = res.json['errors'][0] assert_equal(res.status_code, 400) assert_equal( errors['detail'], "u'Nope, data collection has not begun' is not one of [u'No, data collection has not begun', u'Yes, data collection is underway or complete']" )
def get_prereg_schema(): from website.models import MetaSchema # noqa return MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2))
def setUp(self): super(TestMigrateRegistrationExtra, self).setUp() self.user = UserFactory() self.node = ProjectFactory(creator=self.user) self.file = self._get_test_file() MetaSchema.remove() self.file_ans = { 'file': { 'data':{ 'kind':'file', 'extra':{ 'checkout': None, 'hashes':{ 'sha256':'1fffe6116ecfa778f9938060d5caab923ba4b8db60bd2dd57f16a72e5ef06292' }, 'downloads':0, 'version':1 }, 'modified':'2016-04-15T18:10:48', 'name':'file.txt', 'provider':'osfstorage', } } } self.complex_metadata = { 'q1': { 'value': 'Answer 1', 'extra': [] }, 'q2': { 'value': 'Answer 2', 'extra': {} }, 'q3': { 'value': 'Answer 3', 'extra': self.file_ans }, 'q4': { 'value': { 'question': { 'value': 'Answer 4', 'extra': {} }, 'uploader': { 'value': '', 'extra': {} } }, }, 'q5': { 'value': 'Answer 5', 'extra': [ { 'viewUrl': '/project/abcdef/files/osfstorage/5723787136b74e1a953d9612/', 'hasSelectedFile': True, 'selectedFileName': 'file.txt' } ] } } self.simple_metadata = { 'Summary': 'Some airy' } self.schema = get_prereg_schema() self.draft1 = DraftRegistrationFactory( registration_metadata=self.complex_metadata, registration_schema=self.schema, approval=None, registered_node=None ) self.draft2 = DraftRegistrationFactory( registration_metadata=self.simple_metadata )
import re from copy import deepcopy from framework.transactions.context import TokuTransaction from website.app import init_app from website.models import (MetaSchema, DraftRegistration, Node) from website.files.models import osfstorage from scripts import utils as script_utils init_app(set_backends=True, routes=False) logger = logging.getLogger(__name__) PREREG_SCHEMA = MetaSchema.find_one( Q('name', 'eq', 'Prereg Challenge') & Q('schema_version', 'eq', 2)) PREREG_QUESTIONS = () for page in PREREG_SCHEMA.schema['pages']: PREREG_QUESTIONS = PREREG_QUESTIONS + tuple(page['questions']) def get_file_sha256(node_id, path): node = Node.load(node_id) try: file_node = osfstorage.OsfStorageFileNode.get(path, node) except NoResultsFound: raise RuntimeError( "Couldn't find OsfStorageFile on node {0} with path {1}. Maybe it was moved or deleted?" .format(node_id, path)) latest_version = file_node.get_version()
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 rv = { '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(), } rv.update(_view_project(node, auth, primary=True)) return rv