def get_metaschemas(*args, **kwargs): """ List metaschemas with which a draft registration may be created. Only fetch the newest version for each schema. :return: serialized metaschemas :rtype: dict """ count = request.args.get('count', 100) include = request.args.get('include', 'latest') meta_schemas = [] if include == 'latest': schema_names = list(MetaSchema.objects.all().values_list('name', flat=True).distinct()) for name in schema_names: meta_schema_set = MetaSchema.find( Q('name', 'eq', name) & Q('schema_version', 'eq', 2) ) meta_schemas = meta_schemas + [s for s in meta_schema_set] else: meta_schemas = MetaSchema.find() meta_schemas = [ schema for schema in meta_schemas if schema.active ] meta_schemas.sort(key=lambda a: METASCHEMA_ORDERING.index(a.name)) return { 'meta_schemas': [ serialize_meta_schema(ms) for ms in meta_schemas[:count] ] }, http.OK
def test_ensure_schemas(self): # Should be zero MetaSchema records to begin with MetaSchema.remove() assert_equal(MetaSchema.find().count(), 0) ensure_schemas() assert_equal(MetaSchema.find().count(), len(OSF_META_SCHEMAS))
def test_ensure_schemas(self): # Should be zero MetaSchema records to begin with MetaSchema.remove() assert_equal( MetaSchema.find().count(), 0 ) ensure_schemas() assert_equal( MetaSchema.find().count(), len(OSF_META_SCHEMAS) )
def test_create_from_node(self): proj = NodeFactory() user = proj.creator schema = MetaSchema.find()[0] data = {'some': 'data'} draft = DraftRegistration.create_from_node( proj, user=user, schema=schema, data=data, ) assert user == draft.initiator assert schema == draft.registration_schema assert data == draft.registration_metadata assert proj == draft.branched_from
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))).order_by( '-schema_version').first() 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 _create(cls, *args, **kwargs): branched_from = kwargs.get('branched_from') initiator = kwargs.get('initiator') registration_schema = kwargs.get('registration_schema') registration_metadata = kwargs.get('registration_metadata') if not branched_from: project_params = {} if initiator: project_params['creator'] = initiator branched_from = ProjectFactory(**project_params) initiator = branched_from.creator registration_schema = registration_schema or MetaSchema.find()[0] registration_metadata = registration_metadata or {} draft = DraftRegistration.create_from_node( branched_from, user=initiator, schema=registration_schema, data=registration_metadata, ) return draft
def test_factory(self): draft = factories.DraftRegistrationFactory() assert draft.branched_from is not None assert draft.initiator is not None assert draft.registration_schema is not None user = factories.UserFactory() draft = factories.DraftRegistrationFactory(initiator=user) assert draft.initiator == user node = factories.ProjectFactory() draft = factories.DraftRegistrationFactory(branched_from=node) assert draft.branched_from == node assert draft.initiator == node.creator # Pick an arbitrary v2 schema schema = MetaSchema.find(Q('schema_version', 'eq', 2))[0] data = {'some': 'data'} draft = factories.DraftRegistrationFactory(registration_schema=schema, registration_metadata=data) assert draft.registration_schema == schema assert draft.registration_metadata == data
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)) ).order_by('-schema_version').first() 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 test_factory(self): draft = factories.DraftRegistrationFactory() assert draft.branched_from is not None assert draft.initiator is not None assert draft.registration_schema is not None user = factories.UserFactory() draft = factories.DraftRegistrationFactory(initiator=user) assert draft.initiator == user node = factories.ProjectFactory() draft = factories.DraftRegistrationFactory(branched_from=node) assert draft.branched_from == node assert draft.initiator == node.creator # Pick an arbitrary v2 schema schema = MetaSchema.find( Q('schema_version', 'eq', 2) )[0] data = {'some': 'data'} draft = factories.DraftRegistrationFactory(registration_schema=schema, registration_metadata=data) assert draft.registration_schema == schema assert draft.registration_metadata == data
def get_default_metaschema(): """This needs to be a method so it gets called after the test database is set up""" return MetaSchema.find()[0]
def test_metaschema_is_fine_with_same_name_but_different_version(self): MetaSchema(name='foo', schema={'foo': 42}, schema_version=1).save() MetaSchema(name='foo', schema={'foo': 42}, schema_version=2).save() assert_equal(MetaSchema.find(Q('name', 'eq', 'foo')).count(), 2)