Esempio n. 1
0
    def test_it_renders_replies(self, links_service, pyramid_request,
                                search_run, factories, group_service):
        ann = AnnotationResource(factories.Annotation(userid='luke'),
                                 group_service, links_service)
        reply1 = AnnotationResource(
            factories.Annotation(userid='sarah',
                                 references=[ann.annotation.id]),
            group_service, links_service)
        reply2 = AnnotationResource(
            factories.Annotation(userid='sarah',
                                 references=[ann.annotation.id]),
            group_service, links_service)

        search_run.return_value = SearchResult(
            1, [ann.annotation.id],
            [reply1.annotation.id, reply2.annotation.id], {})

        pyramid_request.params = {'_separate_replies': '1'}

        expected = {
            'total':
            1,
            'rows': [presenters.AnnotationJSONPresenter(ann).asdict()],
            'replies': [
                presenters.AnnotationJSONPresenter(reply1).asdict(),
                presenters.AnnotationJSONPresenter(reply2).asdict(),
            ]
        }

        assert views.search(pyramid_request) == expected
Esempio n. 2
0
    def _get_presenter(self, annotation_resource):
        presenter = presenters.AnnotationJSONPresenter(annotation_resource)

        for formatter in self.formatters:
            presenter.add_formatter(formatter)

        return presenter
Esempio n. 3
0
def _generate_annotation_event(session, request, message, resource):
    """
    Get message about annotation event `message` to be sent to `socket`.

    Inspects the embedded annotation event and decides whether or not the
    passed socket should receive notification of the event.

    Returns None if the socket should not receive any message about this
    annotation event, otherwise a dict containing information about the event.
    """

    if message["action"] == "delete":
        payload = {"id": message["annotation_id"]}
    else:
        user_service = request.find_service(name="user")
        formatters = [AnnotationUserInfoFormatter(session, user_service)]
        payload = presenters.AnnotationJSONPresenter(
            resource, formatters=formatters).asdict()

    return {
        "type": "annotation-notification",
        "options": {
            "action": message["action"]
        },
        "payload": [payload],
    }
Esempio n. 4
0
    def test_it_renders_search_results(self, links_service, pyramid_request,
                                       search_run, factories, group_service):
        ann1 = AnnotationResource(factories.Annotation(userid='luke'),
                                  group_service, links_service)
        ann2 = AnnotationResource(factories.Annotation(userid='sarah'),
                                  group_service, links_service)

        search_run.return_value = SearchResult(
            2, [ann1.annotation.id, ann2.annotation.id], [], {})

        expected = {
            'total':
            2,
            'rows': [
                presenters.AnnotationJSONPresenter(ann1).asdict(),
                presenters.AnnotationJSONPresenter(ann2).asdict(),
            ]
        }

        assert views.search(pyramid_request) == expected
Esempio n. 5
0
def _generate_annotation_event(message, socket, annotation, user_nipsad,
                               group_service, formatters):
    """
    Get message about annotation event `message` to be sent to `socket`.

    Inspects the embedded annotation event and decides whether or not the
    passed socket should receive notification of the event.

    Returns None if the socket should not receive any message about this
    annotation event, otherwise a dict containing information about the event.
    """
    action = message["action"]

    if action == "read":
        return None

    if message["src_client_id"] == socket.client_id:
        return None

    # We don't send anything until we have received a filter from the client
    if socket.filter is None:
        return None

    # Don't sent annotations from NIPSA'd users to anyone other than that
    # user.
    if user_nipsad and socket.authenticated_userid != annotation.userid:
        return None

    notification = {
        "type": "annotation-notification",
        "options": {
            "action": action
        }
    }

    base_url = socket.registry.settings.get("h.app_url",
                                            "http://localhost:8080")
    links_service = LinksService(base_url, socket.registry)
    resource = AnnotationContext(annotation, group_service, links_service)
    serialized = presenters.AnnotationJSONPresenter(
        resource, formatters=formatters).asdict()

    permissions = serialized.get("permissions")
    if not _authorized_to_read(socket.effective_principals, permissions):
        return None

    if not socket.filter.match(serialized, action):
        return None

    notification["payload"] = [serialized]
    if action == "delete":
        notification["payload"] = [{"id": annotation.id}]
    return notification
Esempio n. 6
0
def _generate_annotation_event(message, socket, annotation, user_nipsad,
                               group_service, formatters):
    """
    Get message about annotation event `message` to be sent to `socket`.

    Inspects the embedded annotation event and decides whether or not the
    passed socket should receive notification of the event.

    Returns None if the socket should not receive any message about this
    annotation event, otherwise a dict containing information about the event.
    """
    action = message["action"]

    if action == "read":
        return None

    if message["src_client_id"] == socket.client_id:
        return None

    # Don't sent annotations from NIPSA'd users to anyone other than that
    # user.
    if user_nipsad and socket.authenticated_userid != annotation.userid:
        return None

    # The `prepare` function sets the active registry which is an implicit
    # dependency of some of the authorization logic used to look up annotation
    # and group permissions.
    with pyramid.scripting.prepare(registry=socket.registry):
        notification = {
            "type": "annotation-notification",
            "options": {
                "action": action
            },
        }

        base_url = socket.registry.settings.get("h.app_url",
                                                "http://localhost:5000")
        links_service = LinksService(base_url, socket.registry)
        resource = AnnotationContext(annotation, group_service, links_service)

        # Check whether client is authorized to read this annotation.
        read_principals = principals_allowed_by_permission(resource, "read")
        if not set(read_principals).intersection(socket.effective_principals):
            return None

        serialized = presenters.AnnotationJSONPresenter(
            resource, formatters=formatters).asdict()

        notification["payload"] = [serialized]
        if action == "delete":
            notification["payload"] = [{"id": annotation.id}]
        return notification
Esempio n. 7
0
 def _get_presenter(self, annotation_resource):
     return presenters.AnnotationJSONPresenter(annotation_resource,
                                               self.formatters)