def create_schema_block(state, schema_id, block_type, display_text='', required=False, help_text='', registration_response_key=None, schema_block_group_key='', example_text=''): """ For mapping schemas to schema blocks: creates a given block from the specified parameters """ RegistrationSchemaBlock = state.get_model('osf', 'registrationschemablock') return RegistrationSchemaBlock.objects.create( schema_id=schema_id, block_type=block_type, required=required, display_text=unescape_entities( display_text, safe={ '<': '<', '>': '>' } ), help_text=unescape_entities( help_text, safe={ '<': '<', '>': '>' } ), registration_response_key=registration_response_key, schema_block_group_key=schema_block_group_key, example_text=unescape_entities( example_text, safe={ '<': '<', '>': '>' } ) )
def format_preprint_result(result): parent_info = None formatted_result = { 'contributors': result['contributors'], # TODO: Remove unescape_entities when mako html safe comes in 'title': unescape_entities(result['title']), 'url': result['url'], 'is_component': False, 'parent_title': None, 'parent_url': parent_info.get('url') if parent_info is not None else None, 'tags': result['tags'], 'is_registration': False, 'is_retracted': result['is_retracted'], 'is_pending_retraction': False, 'embargo_end_date': None, 'is_pending_embargo': False, 'description': unescape_entities(result['description']), 'category': result.get('category'), 'date_created': result.get('created'), 'date_registered': None, 'n_wikis': 0, 'license': result.get('license'), 'affiliated_institutions': None, } return formatted_result
def test_unescape_html_additional_safe_characters(self): assert_equal( sanitize.unescape_entities( '<> diamonds & diamonds <>', safe={ '<': '<', '>': '>' }), '<> diamonds & diamonds <>') assert_equal( sanitize.unescape_entities(['<>&'], safe={ '<': '<', '>': '>' })[0], '<>&') assert_equal( sanitize.unescape_entities(('<>&', ), safe={ '<': '<', '>': '>' })[0], '<>&') assert_equal( sanitize.unescape_entities({'key': '<>&'}, safe={ '<': '<', '>': '>' })['key'], '<>&')
def format_result(result, parent_id=None): parent_info = load_parent(parent_id) formatted_result = { 'contributors': result['contributors'], 'wiki_link': result['url'] + 'wiki/', # TODO: Remove unescape_entities when mako html safe comes in 'title': unescape_entities(result['title']), 'url': result['url'], 'is_component': False if parent_info is None else True, 'parent_title': unescape_entities(parent_info.get('title')) if parent_info else None, 'parent_url': parent_info.get('url') if parent_info is not None else None, 'tags': result['tags'], 'is_registration': (result['is_registration'] if parent_info is None else parent_info.get('is_registration')), 'is_retracted': result['is_retracted'], 'is_pending_retraction': result['is_pending_retraction'], 'embargo_end_date': result['embargo_end_date'], 'is_pending_embargo': result['is_pending_embargo'], 'description': unescape_entities(result['description']), 'category': result.get('category'), 'date_created': result.get('date_created'), 'date_registered': result.get('registered_date'), 'n_wikis': len(result['wikis'] or []), 'license': result.get('license'), 'affiliated_institutions': result.get('affiliated_institutions'), 'preprint_url': result.get('preprint_url'), } return formatted_result
def test_unescape_html(self): assert_equal( sanitize.unescape_entities( '<> diamonds & diamonds <>'), '<> diamonds & diamonds <>') assert_equal( sanitize.unescape_entities(['<>&'])[0], '<>&') assert_equal( sanitize.unescape_entities(('<>&', ))[0], '<>&') assert_equal( sanitize.unescape_entities({'key': '<>&'})['key'], '<>&')
def csl(self): # formats node information into CSL format for citation parsing """a dict in CSL-JSON schema For details on this schema, see: https://github.com/citation-style-language/schema#csl-json-schema """ csl = { 'id': self._id, 'title': sanitize.unescape_entities(self.title), 'author': [ contributor.csl_name(self._id) # method in auth/model.py which parses the names of authors for contributor in self.visible_contributors ], 'type': 'webpage', 'URL': self.display_absolute_url, 'publisher': 'OSF Preprints' if self.provider.name == 'Open Science Framework' else self.provider.name } article_doi = self.article_doi preprint_doi = self.preprint_doi if article_doi: csl['DOI'] = article_doi elif preprint_doi and self.is_published and self.preprint_doi_created: csl['DOI'] = preprint_doi if self.date_published: csl['issued'] = datetime_to_csl(self.date_published) return csl
def _serialize_node(self, node, parent=None, grid_root=None, children=None): children = children or [] is_pointer = parent and node.is_linked_node can_edit = node.has_write_perm if hasattr(node, 'has_write_perm') else node.can_edit(auth=self.auth) # Determines if `node` is within two levels of `grid_root` # Used to prevent complete serialization of deeply nested projects if parent and grid_root and parent == grid_root: children = self._get_nodes(node)['children'] if not children: children = [] return { # TODO: Remove safe_unescape_html when mako html safe comes in 'name': sanitize.unescape_entities(node.title), 'category': node.category, 'kind': FOLDER, 'permissions': { 'edit': can_edit and not node.is_registration, 'view': True, }, 'urls': { 'upload': None, 'fetch': None, }, 'children': children, 'isPointer': is_pointer, 'isSmartFolder': False, 'nodeType': 'component' if parent else 'project', 'nodeID': node._id, }
def find_registration_file(value, node): """ some annotations: - `value` is the `extra` from a file upload in `registered_meta` (see `Uploader.addFile` in website/static/js/registrationEditorExtensions.js) - `node` is a Registration instance - returns a `(file_info, node_id)` or `(None, None)` tuple, where `file_info` is from waterbutler's api (see `addons.base.models.BaseStorageAddon._get_fileobj_child_metadata` and `waterbutler.core.metadata.BaseMetadata`) """ from osf.models import AbstractNode orig_sha256 = value['sha256'] orig_name = unescape_entities( value['selectedFileName'], safe={ '<': '<', '>': '>' } ) orig_node = value['nodeId'] file_map = get_file_map(node) for sha256, file_info, node_id in file_map: registered_from_id = AbstractNode.load(node_id).registered_from._id if sha256 == orig_sha256 and registered_from_id == orig_node and orig_name == file_info['name']: return file_info, node_id return None, None
def test_unescape_html(self): assert_equal( sanitize.unescape_entities('<> diamonds & diamonds <>'), '<> diamonds & diamonds <>' ) assert_equal( sanitize.unescape_entities(['<>&'])[0], '<>&' ) assert_equal( sanitize.unescape_entities(('<>&', ))[0], '<>&' ) assert_equal( sanitize.unescape_entities({'key': '<>&'})['key'], '<>&' )
def test_unescape_html_additional_safe_characters(self): assert_equal( sanitize.unescape_entities( '<> diamonds & diamonds <>', safe={ '<': '<', '>': '>' } ), '<> diamonds & diamonds <>' ) assert_equal( sanitize.unescape_entities( ['<>&'], safe={ '<': '<', '>': '>' } )[0], '<>&' ) assert_equal( sanitize.unescape_entities( ('<>&', ), safe={ '<': '<', '>': '>' } )[0], '<>&' ) assert_equal( sanitize.unescape_entities( {'key': '<>&'}, safe={ '<': '<', '>': '>' } )['key'], '<>&' )
def to_json(self): return { 'id': self._id, 'date_created': iso8601format(self.created), 'key': self.key, 'name': unescape_entities(self.name), 'creator': {'fullname': self.creator.fullname, 'url': self.creator.profile_url}, 'nodes': [{'title': x.title, 'url': x.url, 'scale': str(self.node_scale(x)) + 'px', 'category': x.category} for x in self.nodes.filter(is_deleted=False)], 'anonymous': self.anonymous }
def find_registration_file(value, node): from osf.models import AbstractNode orig_sha256 = value['sha256'] orig_name = unescape_entities(value['selectedFileName'], safe={ '<': '<', '>': '>' }) orig_node = value['nodeId'] file_map = get_file_map(node) for sha256, value, node_id in file_map: registered_from_id = AbstractNode.load(node_id).registered_from._id if sha256 == orig_sha256 and registered_from_id == orig_node and orig_name == value[ 'name']: return value, node_id return None, None
def find_registration_file(value, node): from osf.models import AbstractNode orig_sha256 = value['sha256'] orig_name = unescape_entities( value['selectedFileName'], safe={ '<': '<', '>': '>' } ) orig_node = value['nodeId'] file_map = get_file_map(node) for sha256, value, node_id in file_map: registered_from_id = AbstractNode.load(node_id).registered_from._id if sha256 == orig_sha256 and registered_from_id == orig_node and orig_name == value['name']: return value, node_id return None, None
def to_json(self): return { 'id': self._id, 'date_created': iso8601format(self.created), 'key': self.key, 'name': unescape_entities(self.name), 'creator': { 'fullname': self.creator.fullname, 'url': self.creator.profile_url }, 'nodes': [{ 'title': x.title, 'url': x.url, 'scale': str(self.node_scale(x)) + 'px', 'category': x.category } for x in self.nodes.filter(is_deleted=False)], 'anonymous': self.anonymous }