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 safe_unescape_html when mako html safe comes in 'title': sanitize.safe_unescape_html(result['title']), 'url': result['url'], 'is_component': False if parent_info is None else True, 'parent_title': sanitize.safe_unescape_html(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'], 'pending_retraction': result['pending_retraction'], 'embargo_end_date': result['embargo_end_date'], 'pending_embargo': result['pending_embargo'], 'description': result['description'] if parent_info is None else None, 'category': result.get('category'), 'date_created': result.get('date_created'), 'date_registered': result.get('registration_date') } return formatted_result
def test_unescape_html(self): assert_equal( sanitize.safe_unescape_html("<> diamonds & diamonds <>"), "<> diamonds & diamonds <>" ) assert_equal(sanitize.safe_unescape_html(["<>&"])[0], "<>&") assert_equal(sanitize.safe_unescape_html(("<>&",))[0], "<>&") assert_equal(sanitize.safe_unescape_html({"key": "<>&"})["key"], "<>&")
def test_unescape_html(self): assert_equal( sanitize.safe_unescape_html( '<> diamonds & diamonds <>'), '<> diamonds & diamonds <>') assert_equal(sanitize.safe_unescape_html(['<>&'])[0], '<>&') assert_equal( sanitize.safe_unescape_html(('<>&', ))[0], '<>&') assert_equal( sanitize.safe_unescape_html({'key': '<>&'})['key'], '<>&')
def _serialize_node(self, node, visited=None): """Returns the rubeus representation of a node folder. """ visited = visited or [] visited.append(node.resolve()._id) can_view = node.can_view(auth=self.auth) if can_view: children = self._collect_addons(node) + self._collect_components(node, visited) else: children = [] return { # TODO: Remove safe_unescape_html when mako html safe comes in 'name': u'{0}: {1}'.format(node.project_or_component.capitalize(), sanitize.safe_unescape_html(node.title)) if can_view else u'Private Component', 'category': node.category, 'kind': FOLDER, 'permissions': { 'edit': node.can_edit(self.auth) and not node.is_registration, 'view': can_view, }, 'urls': { 'upload': None, 'fetch': None, }, 'children': children, 'isPointer': not node.primary, 'isSmartFolder': False, 'nodeType': node.project_or_component, 'nodeID': node.resolve()._id, }
def test_unescape_html(self): assert_equal( sanitize.safe_unescape_html('<> diamonds & diamonds <>'), '<> diamonds & diamonds <>' ) assert_equal( sanitize.safe_unescape_html(['<>&'])[0], '<>&' ) assert_equal( sanitize.safe_unescape_html(('<>&', ))[0], '<>&' ) assert_equal( sanitize.safe_unescape_html({'key': '<>&'})['key'], '<>&' )
def serialize_log(node_log, auth=None, anonymous=False): """Return a dictionary representation of the log.""" return { "id": str(node_log._primary_key), "user": node_log.user.serialize() if isinstance(node_log.user, User) else {"fullname": node_log.foreign_user}, "contributors": [node_log._render_log_contributor(c) for c in node_log.params.get("contributors", [])], "api_key": node_log.api_key.label if node_log.api_key else "", "action": node_log.action, "params": sanitize.safe_unescape_html(node_log.params), "date": utils.iso8601format(node_log.date), "node": node_log.node.serialize(auth) if node_log.node else None, "anonymous": anonymous, }
def serialize_log(node_log, auth=None, anonymous=False): '''Return a dictionary representation of the log.''' return { 'id': str(node_log._primary_key), 'user': node_log.user.serialize() if isinstance(node_log.user, User) else {'fullname': node_log.foreign_user}, 'contributors': [node_log._render_log_contributor(c) for c in node_log.params.get("contributors", [])], 'action': node_log.action, 'params': sanitize.safe_unescape_html(node_log.params), 'date': utils.iso8601format(node_log.date), 'node': node_log.node.serialize(auth) if node_log.node else None, 'anonymous': anonymous }
def _get_node_name(self, node): """Input node object, return the project name to be display. """ can_view = node.can_view(auth=self.auth) if can_view: node_name = u'{0}: {1}'.format(node.project_or_component.capitalize(), sanitize.safe_unescape_html(node.title)) elif node.is_registration: node_name = u'Private Registration' elif node.is_fork: node_name = u'Private Fork' elif not node.primary: node_name = u'Private Link' else: node_name = u'Private Component' return node_name
def _serialize_node(self, node, visited=None, parent_is_folder=False): """Returns the rubeus representation of a node folder for the project organizer. """ visited = visited or [] visited.append(node.resolve()._id) can_edit = node.can_edit(auth=self.auth) and not node.is_registration expanded = node.is_expanded(user=self.auth.user) can_view = node.can_view(auth=self.auth) children = [] modified_delta = delta_date(node.date_modified) date_modified = node.date_modified.isoformat() contributors = [] for contributor in node.contributors: if contributor._id in node.visible_contributor_ids: contributor_name = [ contributor.family_name, contributor.given_name, contributor.fullname, ] contributors.append({ 'name': next(name for name in contributor_name if name), 'url': contributor.url, }) try: user = node.logs[-1].user modified_by = user.family_name or user.given_name except AttributeError: modified_by = '' child_nodes = node.nodes readable_children = [] for child in child_nodes: if child is not None: resolved = child.resolve() if resolved.can_view(auth=self.auth) and not resolved.is_deleted: readable_children.append(child) children_count = len(readable_children) is_pointer = not node.primary is_component = node.category != 'project' is_project = node.category == 'project' is_file = False type_ = 'project' if is_file: type_ = 'file' if is_pointer and not parent_is_folder: type_ = 'pointer' if node.is_folder: type_ = 'folder' if is_component: type_ = 'component' if node.is_dashboard: to_expand = True elif not is_pointer or parent_is_folder: to_expand = expanded else: to_expand = False return { # TODO: Remove safe_unescape_html when mako html safe comes in 'name': sanitize.safe_unescape_html(node.title) if can_view else u'Private Component', 'kind': FOLDER, 'category': node.category, # Once we get files into the project organizer, files would be kind of FILE 'permissions': { 'edit': can_edit, 'view': can_view, 'copyable': not node.is_folder, 'movable': parent_is_folder, 'acceptsFolders': node.is_folder, 'acceptsMoves': node.is_folder, 'acceptsCopies': node.is_folder or is_project, 'acceptsComponents': node.is_folder, }, 'urls': { 'upload': None, 'fetch': node.url if not node.is_folder else None, }, 'type': type_, 'children': children, 'expand': to_expand, # TODO: (bgeiger) replace these flags with a Kind property or something 'isProject': is_project, 'isPointer': is_pointer, 'isComponent': is_component, 'isFolder': node.is_folder, 'isDashboard': node.is_dashboard, 'isFile': is_file, 'dateModified': date_modified, 'modifiedDelta': max(1, modified_delta), 'modifiedBy': modified_by, 'parentIsFolder': parent_is_folder, 'contributors': contributors, 'node_id': node.resolve()._id, 'isSmartFolder': False, 'apiURL': node.api_url, 'isRegistration': node.is_registration, 'description': node.description, 'registeredMeta': node.registered_meta, 'childrenCount': children_count, 'nodeType': node.project_or_component, }
def test_get_node_name(self): user = UserFactory() auth = Auth(user=user) another_user = UserFactory() another_auth = Auth(user=another_user) # Public (Can View) public_project = ProjectFactory(is_public=True) collector = rubeus.NodeFileCollector(node=public_project, auth=another_auth) node_name = u'{0}: {1}'.format(public_project.project_or_component.capitalize(), sanitize.safe_unescape_html(public_project.title)) assert_equal(collector._get_node_name(public_project), node_name) # Private (Can't View) registration_private = RegistrationFactory(creator=user) registration_private.is_public = False registration_private.save() collector = rubeus.NodeFileCollector(node=registration_private, auth=another_auth) assert_equal(collector._get_node_name(registration_private), u'Private Registration') content = ProjectFactory(creator=user) node = ProjectFactory(creator=user) forked_private = node.fork_node(auth=auth) forked_private.is_public = False forked_private.save() collector = rubeus.NodeFileCollector(node=forked_private, auth=another_auth) assert_equal(collector._get_node_name(forked_private), u'Private Fork') pointer_private = node.add_pointer(content, auth=auth) pointer_private.is_public = False pointer_private.save() collector = rubeus.NodeFileCollector(node=pointer_private, auth=another_auth) assert_equal(collector._get_node_name(pointer_private), u'Private Link') private_project = ProjectFactory(is_public=False) collector = rubeus.NodeFileCollector(node=private_project, auth=another_auth) assert_equal(collector._get_node_name(private_project), u'Private Component') private_node = NodeFactory(is_public=False) collector = rubeus.NodeFileCollector(node=private_node, auth=another_auth) assert_equal(collector._get_node_name(private_node), u'Private Component')
def _serialize_node(self, node, visited=None, parent_is_folder=False): """Returns the rubeus representation of a node folder for the project organizer. """ visited = visited or [] visited.append(node.resolve()._id) can_edit = node.can_edit(auth=self.auth) and not node.is_registration expanded = node.is_expanded(user=self.auth.user) can_view = node.can_view(auth=self.auth) children = [] modified_delta = delta_date(node.date_modified) date_modified = node.date_modified.isoformat() contributors = [] for contributor in node.contributors: if contributor._id in node.visible_contributor_ids: contributor_name = [ contributor.family_name, contributor.given_name, contributor.fullname, ] contributors.append({ 'name': next(name for name in contributor_name if name), 'url': contributor.url, }) try: user = node.logs[-1].user modified_by = user.family_name or user.given_name except (AttributeError, IndexError): modified_by = '' child_nodes = node.nodes readable_children = [] for child in child_nodes: if child is not None: resolved = child.resolve() if resolved.can_view(auth=self.auth) and not resolved.is_deleted: readable_children.append(child) children_count = len(readable_children) is_pointer = not node.primary is_component = node.category != 'project' is_project = node.category == 'project' is_file = False type_ = 'project' if is_file: type_ = 'file' if is_pointer and not parent_is_folder: type_ = 'pointer' if node.is_folder: type_ = 'folder' if is_component: type_ = 'component' if node.is_dashboard: to_expand = True elif not is_pointer or parent_is_folder: to_expand = expanded else: to_expand = False return { # TODO: Remove safe_unescape_html when mako html safe comes in 'name': sanitize.safe_unescape_html(node.title) if can_view else u'Private Component', 'kind': FOLDER, 'category': node.category, # Once we get files into the project organizer, files would be kind of FILE 'permissions': { 'edit': can_edit, 'view': can_view, 'copyable': not node.is_folder, 'movable': parent_is_folder, 'acceptsFolders': node.is_folder, 'acceptsMoves': node.is_folder, 'acceptsCopies': node.is_folder or is_project, 'acceptsComponents': node.is_folder, }, 'urls': { 'upload': None, 'fetch': node.url if not node.is_folder else None, }, 'type': type_, 'children': children, 'expand': to_expand, # TODO: (bgeiger) replace these flags with a Kind property or something 'isProject': is_project, 'isPointer': is_pointer, 'isComponent': is_component, 'isFolder': node.is_folder, 'isDashboard': node.is_dashboard, 'isFile': is_file, 'dateModified': date_modified, 'modifiedDelta': max(1, modified_delta), 'modifiedBy': modified_by, 'parentIsFolder': parent_is_folder, 'contributors': contributors, 'node_id': node.resolve()._id, 'isSmartFolder': False, 'apiURL': node.api_url, 'isRegistration': node.is_registration, 'description': node.description, 'registeredMeta': node.registered_meta, 'childrenCount': children_count, 'nodeType': node.project_or_component, 'archiving': node.archive_job and not node.archive_job.done, }
def test_unescape_html(self): assert_equal( sanitize.safe_unescape_html('<> diamonds & diamonds <>'), '<> diamonds & diamonds <>' )