Пример #1
0
    def asdict(self):
        docpresenter = DocumentSearchIndexPresenter(self.annotation.document)
        userid_parts = split_user(self.annotation.userid)

        tags = self.annotation.tags or []

        result = {
            "authority": userid_parts["domain"],
            "id": self.annotation.id,
            "created": utc_iso8601(self.annotation.created),
            "updated": utc_iso8601(self.annotation.updated),
            "user": self.annotation.userid,
            "user_raw": self.annotation.userid,
            "uri": self.annotation.target_uri,
            "text": self.annotation.text or "",
            "tags": tags,
            "tags_raw": tags,
            "group": self.annotation.groupid,
            "shared": self.annotation.shared,
            "target": self.annotation.target,
            "document": docpresenter.asdict(),
            "thread_ids": self.annotation.thread_ids,
        }

        result["target"][0]["scope"] = [self.annotation.target_uri_normalized]

        if self.annotation.references:
            result["references"] = self.annotation.references

        self._add_hidden(result)
        self._add_nipsa(result, self.annotation.userid)

        return result
Пример #2
0
def _present_debug_token(token):
    data = {'userid': token.userid,
            'expires_at': utc_iso8601(token.expires) if token.expires else None,
            'issued_at': utc_iso8601(token.created),
            'expired': token.expired}

    if token.authclient:
        data['client'] = {'id': token.authclient.id,
                          'name': token.authclient.name}

    return data
Пример #3
0
 def asdict(self):
     return {
         "@context": self.CONTEXT_URL,
         "type": "Annotation",
         "id": self._links_service.get(self.annotation, "jsonld_id"),
         "created": utc_iso8601(self.annotation.created),
         "modified": utc_iso8601(self.annotation.updated),
         "creator": self.annotation.userid,
         "body": self._bodies,
         "target": self._target,
     }
Пример #4
0
def _present_debug_token(token):
    data = {'userid': token.userid,
            'expires_at': utc_iso8601(token.expires) if token.expires else None,
            'issued_at': utc_iso8601(token.created),
            'expired': token.expired}

    if token.authclient:
        data['client'] = {'id': token.authclient.id,
                          'name': token.authclient.name}

    return data
Пример #5
0
    def test_returns_debug_data_for_oauth_token(self, pyramid_request, token_service, oauth_token):
        pyramid_request.auth_token = oauth_token.value
        token_service.fetch.return_value = oauth_token

        result = views.debug_token(pyramid_request)

        assert result == {'userid': oauth_token.userid,
                          'client': {'id': oauth_token.authclient.id,
                                     'name': oauth_token.authclient.name},
                          'issued_at': utc_iso8601(oauth_token.created),
                          'expires_at': utc_iso8601(oauth_token.expires),
                          'expired': oauth_token.expired}
Пример #6
0
def _present_debug_token(token):
    data = {
        "userid": token.userid,
        "expires_at": utc_iso8601(token.expires) if token.expires else None,
        "issued_at": utc_iso8601(token.created),
        "expired": token.expired,
    }

    if token.authclient:
        data["client"] = {"id": token.authclient.id, "name": token.authclient.name}

    return data
Пример #7
0
    def test_returns_debug_data_for_oauth_token(self, pyramid_request,
                                                token_service, oauth_token):
        pyramid_request.auth_token = oauth_token.value
        token_service.fetch.return_value = oauth_token

        result = views.debug_token(pyramid_request)

        assert result == {
            "userid": oauth_token.userid,
            "client": {
                "id": oauth_token.authclient.id,
                "name": oauth_token.authclient.name,
            },
            "issued_at": utc_iso8601(oauth_token.created),
            "expires_at": utc_iso8601(oauth_token.expires),
            "expired": oauth_token.expired,
        }
Пример #8
0
    def test_returns_debug_data_for_oauth_token(
        self, pyramid_request, token_service, oauth_token
    ):
        pyramid_request.auth_token = oauth_token.value
        token_service.fetch.return_value = oauth_token

        result = views.debug_token(pyramid_request)

        assert result == {
            "userid": oauth_token.userid,
            "client": {
                "id": oauth_token.authclient.id,
                "name": oauth_token.authclient.name,
            },
            "issued_at": utc_iso8601(oauth_token.created),
            "expires_at": utc_iso8601(oauth_token.expires),
            "expired": oauth_token.expired,
        }
Пример #9
0
    def present(self, annotation: Annotation):
        """
        Get the JSON presentation of an annotation.

        This representation does not contain any user specific information and
        has only the data applicable to all users. This does not blank content
        for moderated annotations.

        :param annotation: Annotation to present
        :return: A dict suitable for JSON serialisation
        """
        model = deepcopy(annotation.extra) or {}

        model.update(
            {
                "id": annotation.id,
                "created": utc_iso8601(annotation.created),
                "updated": utc_iso8601(annotation.updated),
                "user": annotation.userid,
                "uri": annotation.target_uri,
                "text": annotation.text or "",
                "tags": annotation.tags or [],
                "group": annotation.groupid,
                #  Convert our simple internal annotation storage format into the
                #  legacy complex permissions dict format that is still used in
                #  some places.
                "permissions": {
                    "read": [self._get_read_permission(annotation)],
                    "admin": [annotation.userid],
                    "update": [annotation.userid],
                    "delete": [annotation.userid],
                },
                "target": annotation.target,
                "document": DocumentJSONPresenter(annotation.document).asdict(),
                "links": self._links_service.get_all(annotation),
            }
        )

        model.update(user_info(self._user_service.fetch(annotation.userid)))

        if annotation.references:
            model["references"] = annotation.references

        return model
Пример #10
0
    def test_returns_debug_data_for_developer_token(self, pyramid_request, token_service, developer_token):
        pyramid_request.auth_token = developer_token.value
        token_service.fetch.return_value = developer_token

        result = views.debug_token(pyramid_request)

        assert result == {'userid': developer_token.userid,
                          'issued_at': utc_iso8601(developer_token.created),
                          'expires_at': None,
                          'expired': False}
Пример #11
0
Файл: atom.py Проект: kaydoh/h
def _feed_entry_from_annotation(annotation, annotation_url, annotation_api_url=None):
    """
    Return an Atom feed entry for the given annotation.

    :returns: A logical representation of the Atom feed entry as a dict,
        containing all of the data that a template would need to render the
        feed item to XML.
    :rtype: dict

    """
    try:
        name = util.user.split_user(annotation.userid)["username"]
    except ValueError:
        name = annotation.userid

    entry = {
        "id": h.feeds.util.tag_uri_for_annotation(
            annotation.annotation, annotation_url
        ),
        "author": {"name": name},
        "title": annotation.title,
        "updated": utc_iso8601(annotation.updated),
        "published": utc_iso8601(annotation.created),
        "content": annotation.description,
        "links": [
            {
                "rel": "alternate",
                "type": "text/html",
                "href": annotation_url(annotation.annotation),
            }
        ],
    }
    if annotation_api_url:
        entry["links"].append(
            {
                "rel": "alternate",
                "type": "application/json",
                "href": annotation_api_url(annotation.annotation),
            }
        )

    return entry
Пример #12
0
    def test_returns_debug_data_for_developer_token(
        self, pyramid_request, token_service, developer_token
    ):
        pyramid_request.auth_token = developer_token.value
        token_service.fetch.return_value = developer_token

        result = views.debug_token(pyramid_request)

        assert result == {
            "userid": developer_token.userid,
            "issued_at": utc_iso8601(developer_token.created),
            "expires_at": None,
            "expired": False,
        }
Пример #13
0
Файл: atom.py Проект: kaydoh/h
def feed_from_annotations(  # pylint: disable=too-many-arguments
    annotations,
    atom_url,
    annotation_url,
    annotation_api_url=None,
    html_url=None,
    title=None,
    subtitle=None,
):
    """
    Return an Atom feed for the given list of annotations.

    :returns: A logical representation of an Atom feed as a Python dict
        containing all of the data that a template would need to render the
        feed to XML (including a list of dicts for the feed's entries).
    :rtype: dict

    """
    annotations = [presenters.AnnotationHTMLPresenter(a) for a in annotations]

    links = [{"rel": "self", "type": "application/atom+xml", "href": atom_url}]

    if html_url:
        links.append({"rel": "alternate", "type": "text/html", "href": html_url})

    entries = [
        _feed_entry_from_annotation(a, annotation_url, annotation_api_url)
        for a in annotations
    ]

    feed = {
        "id": atom_url,
        "title": title or _("Hypothesis Stream"),
        "subtitle": subtitle or _("The Web. Annotated"),
        "entries": entries,
        "links": links,
    }

    if annotations:
        feed["updated"] = utc_iso8601(annotations[0].updated)

    return feed
Пример #14
0
 def updated(self):
     if self.annotation.updated:
         return utc_iso8601(self.annotation.updated)
Пример #15
0
 def created(self):
     if self.annotation.created:
         return utc_iso8601(self.annotation.created)
Пример #16
0
 def created(self):
     if self.annotation.created:
         return utc_iso8601(self.annotation.created)
Пример #17
0
def test_utc_iso8601_ignores_timezone():
    t = datetime.datetime(2016, 2, 24, 18, 3, 25, 7685, Berlin())
    assert utc_iso8601(t) == "2016-02-24T18:03:25.007685+00:00"
Пример #18
0
def test_utc_iso8601():
    t = datetime.datetime(2016, 2, 24, 18, 3, 25, 7685)
    assert utc_iso8601(t) == "2016-02-24T18:03:25.007685+00:00"
Пример #19
0
def test_utc_iso8601(date, expected):
    assert utc_iso8601(date) == expected
Пример #20
0
def test_utc_iso8601_ignores_timezone():
    t = datetime.datetime(2016, 2, 24, 18, 3, 25, 7685, Berlin())
    assert utc_iso8601(t) == '2016-02-24T18:03:25.007685+00:00'
Пример #21
0
def test_utc_iso8601():
    t = datetime.datetime(2016, 2, 24, 18, 3, 25, 7685)
    assert utc_iso8601(t) == '2016-02-24T18:03:25.007685+00:00'
Пример #22
0
 def updated(self):
     if self.annotation.updated:
         return utc_iso8601(self.annotation.updated)