Example #1
0
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': sanitize.unescape_entities(result['title']),
        'url': result['url'],
        'is_component': False if parent_info is None else True,
        'parent_title': sanitize.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': result['description'] if parent_info is None else None,
        'category': result.get('category'),
        'date_created': result.get('date_created'),
        'date_registered': result.get('registered_date'),
        'n_wikis': len(result['wikis'])
    }

    return formatted_result
Example #2
0
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": sanitize.unescape_entities(result["title"]),
        "url": result["url"],
        "is_component": False if parent_info is None else True,
        "parent_title": sanitize.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": result["description"] if parent_info is None else None,
        "category": result.get("category"),
        "date_created": result.get("date_created"),
        "date_registered": result.get("registered_date"),
        "n_wikis": len(result["wikis"]),
        "license": result.get("license"),
        "affiliated_institutions": result.get("affiliated_institutions"),
    }

    return formatted_result
Example #3
0
 def test_unescape_html_additional_safe_characters(self):
     assert_equal(
         sanitize.unescape_entities(
             '<> diamonds & diamonds <>',
             safe={
                 '&lt;': '<',
                 '&gt;': '>'
             }), '<> diamonds & diamonds <>')
     assert_equal(
         sanitize.unescape_entities(['&lt;&gt;&amp;'],
                                    safe={
                                        '&lt;': '<',
                                        '&gt;': '>'
                                    })[0], '<>&')
     assert_equal(
         sanitize.unescape_entities(('&lt;&gt;&amp;', ),
                                    safe={
                                        '&lt;': '<',
                                        '&gt;': '>'
                                    })[0], '<>&')
     assert_equal(
         sanitize.unescape_entities({'key': '&lt;&gt;&amp;'},
                                    safe={
                                        '&lt;': '<',
                                        '&gt;': '>'
                                    })['key'], '<>&')
Example #4
0
 def test_unescape_html(self):
     assert_equal(
         sanitize.unescape_entities(
             '&lt;&gt; diamonds &amp; diamonds &lt;&gt;'),
         '&lt;&gt; diamonds & diamonds &lt;&gt;')
     assert_equal(
         sanitize.unescape_entities(['&lt;&gt;&amp;'])[0], '&lt;&gt;&')
     assert_equal(
         sanitize.unescape_entities(('&lt;&gt;&amp;', ))[0], '&lt;&gt;&')
     assert_equal(
         sanitize.unescape_entities({'key': '&lt;&gt;&amp;'})['key'],
         '&lt;&gt;&')
Example #5
0
    def _serialize_node(self, node, parent=None, grid_root=None, children=[]):
        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']

        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,
        }
Example #6
0
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.unescape_entities(node_log.params),
        'date':
        utils.iso8601format(node_log.date),
        'node':
        node_log.original_node.serialize(auth)
        if node_log.original_node else None,
        'anonymous':
        anonymous
    }
Example #7
0
 def test_unescape_html(self):
     assert_equal(
         sanitize.unescape_entities('&lt;&gt; diamonds &amp; diamonds &lt;&gt;'),
         '&lt;&gt; diamonds & diamonds &lt;&gt;'
     )
     assert_equal(
         sanitize.unescape_entities(['&lt;&gt;&amp;'])[0],
         '&lt;&gt;&'
     )
     assert_equal(
         sanitize.unescape_entities(('&lt;&gt;&amp;', ))[0],
         '&lt;&gt;&'
     )
     assert_equal(
         sanitize.unescape_entities({'key': '&lt;&gt;&amp;'})['key'],
         '&lt;&gt;&'
     )
Example #8
0
    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.unescape_entities(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')
Example #9
0
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.unescape_entities(node_log.params),
        "date": utils.iso8601format(node_log.date),
        "node": node_log.node.serialize(auth) if node_log.node else None,
        "anonymous": anonymous,
    }
Example #10
0
 def to_json(self):
     return {
         'id': self._id,
         'date_created': iso8601format(self.created),
         'key': self.key,
         'name': sanitize.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
     }
Example #11
0
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.unescape_entities(node_log.params),
        'date': utils.iso8601format(node_log.date),
        'node': node_log.node.serialize(auth) if node_log.node else None,
        'anonymous': anonymous
    }
Example #12
0
def find_registration_file(value, node):
    from osf.models import AbstractNode
    orig_sha256 = value['sha256']
    orig_name = sanitize.unescape_entities(value['selectedFileName'],
                                           safe={
                                               '&lt;': '<',
                                               '&gt;': '>'
                                           })
    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
Example #13
0
    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 = sanitize.unescape_entities(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
Example #14
0
def find_registration_file(value, node):
    from osf.models import AbstractNode as Node
    orig_sha256 = value['sha256']
    orig_name = sanitize.unescape_entities(
        value['selectedFileName'],
        safe={
            '&lt;': '<',
            '&gt;': '>'
        }
    )
    orig_node = value['nodeId']
    file_map = get_file_map(node)
    for sha256, value, node_id in file_map:
        registered_from_id = Node.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
Example #15
0
    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 = sanitize.unescape_entities(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
Example #16
0
    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.unescape_entities(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")
Example #17
0
    def _get_node_name(self, node):
        """Input node object, return the project name to be display.
        """
        NodeRelation = apps.get_model('osf.NodeRelation')
        is_node_relation = isinstance(node, NodeRelation)
        node = node.child if is_node_relation else node
        can_view = node.can_view(auth=self.auth)

        if can_view:
            node_name = sanitize.unescape_entities(node.title)
        elif node.is_registration:
            node_name = u'Private Registration'
        elif node.is_fork:
            node_name = u'Private Fork'
        elif is_node_relation:
            node_name = u'Private Link'
        else:
            node_name = u'Private Component'

        return node_name
Example #18
0
    def _get_node_name(self, node):
        """Input node object, return the project name to be display.
        """
        NodeRelation = apps.get_model('osf.NodeRelation')
        is_node_relation = isinstance(node, NodeRelation)
        node = node.child if is_node_relation else node
        can_view = node.can_view(auth=self.auth)

        if can_view:
            node_name = sanitize.unescape_entities(node.title)
        elif node.is_registration:
            node_name = u'Private Registration'
        elif node.is_fork:
            node_name = u'Private Fork'
        elif is_node_relation:
            node_name = u'Private Link'
        else:
            node_name = u'Private Component'

        return node_name
Example #19
0
    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 unescape_entities when mako html safe comes in
            'name': sanitize.unescape_entities(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,
        }
Example #20
0
    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 unescape_entities when mako html safe comes in
            'name': sanitize.unescape_entities(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,
        }