Skip to content

Commit

Permalink
Inline the DocumentJSONPresenter into the annotation JSON service
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Betts committed Apr 5, 2023
1 parent 100bbd6 commit 4696df6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 90 deletions.
14 changes: 0 additions & 14 deletions h/presenters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@
from h.presenters.annotation_jsonld import AnnotationJSONLDPresenter
from h.presenters.annotation_searchindex import AnnotationSearchIndexPresenter
from h.presenters.document_html import DocumentHTMLPresenter
from h.presenters.document_json import DocumentJSONPresenter
from h.presenters.document_searchindex import DocumentSearchIndexPresenter
from h.presenters.group_json import GroupJSONPresenter, GroupsJSONPresenter
from h.presenters.user_json import TrustedUserJSONPresenter, UserJSONPresenter

__all__ = (
"AnnotationHTMLPresenter",
"AnnotationJSONLDPresenter",
"AnnotationSearchIndexPresenter",
"DocumentHTMLPresenter",
"DocumentJSONPresenter",
"DocumentSearchIndexPresenter",
"GroupJSONPresenter",
"GroupsJSONPresenter",
"UserJSONPresenter",
"TrustedUserJSONPresenter",
)
14 changes: 0 additions & 14 deletions h/presenters/document_json.py

This file was deleted.

7 changes: 5 additions & 2 deletions h/services/annotation_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from h import storage
from h.models import Annotation, User
from h.presenters import DocumentJSONPresenter
from h.security import Identity, identity_permits
from h.security.permissions import Permission
from h.session import user_info
Expand Down Expand Up @@ -42,6 +41,10 @@ def present(self, annotation: Annotation):
"""
model = deepcopy(annotation.extra) or {}

document = {}
if annotation.document and annotation.document.title:
document["title"] = annotation.document.title

model.update(
{
"id": annotation.id,
Expand All @@ -62,7 +65,7 @@ def present(self, annotation: Annotation):
"delete": [annotation.userid],
},
"target": annotation.target,
"document": DocumentJSONPresenter(annotation.document).asdict(),
"document": document,
"links": self._links_service.get_all(annotation),
}
)
Expand Down
49 changes: 0 additions & 49 deletions tests/h/presenters/document_json_test.py

This file was deleted.

27 changes: 16 additions & 11 deletions tests/h/services/annotation_json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@


class TestAnnotationJSONService:
def test_present(
self, service, annotation, links_service, user_service, DocumentJSONPresenter
):
def test_present(self, service, annotation, links_service, user_service):
annotation.created = datetime(2016, 2, 24, 18, 3, 25, 768)
annotation.updated = datetime(2016, 2, 29, 10, 24, 5, 564)
annotation.references = ["referenced-id-1", "referenced-id-2"]
Expand All @@ -41,24 +39,35 @@ def test_present(
"delete": [annotation.userid],
},
"target": annotation.target,
"document": DocumentJSONPresenter.return_value.asdict.return_value,
"document": {"title": annotation.document.title},
"links": links_service.get_all.return_value,
"references": annotation.references,
"extra-1": "foo",
"extra-2": "bar",
"user_info": {"display_name": user_service.fetch.return_value.display_name},
}

DocumentJSONPresenter.assert_called_once_with(annotation.document)
DocumentJSONPresenter.return_value.asdict.assert_called_once_with()

def test_present_without_references(self, service, annotation):
annotation.references = None

result = service.present(annotation)

assert "references" not in result

def test_present_without_a_document(self, service, annotation):
annotation.document = None

result = service.present(annotation)

assert not result["document"]

def test_present_without_a_document_title(self, service, annotation):
annotation.document.title = None

result = service.present(annotation)

assert not result["document"]

def test_present_extra_inherits_correctly(self, service, annotation):
annotation.extra = {"id": "DIFFERENT"}

Expand Down Expand Up @@ -259,10 +268,6 @@ def Identity(self, patch):
def identity_permits(self, patch):
return patch("h.services.annotation_json.identity_permits")

@pytest.fixture(autouse=True)
def DocumentJSONPresenter(self, patch):
return patch("h.services.annotation_json.DocumentJSONPresenter")


class TestFactory:
def test_it(
Expand Down

0 comments on commit 4696df6

Please sign in to comment.