Skip to content

Commit

Permalink
Merge pull request #25 from marph91/add-non-image-resources-properly
Browse files Browse the repository at this point in the history
add non-image resources properly
  • Loading branch information
marph91 committed Jan 17, 2024
2 parents 2330309 + 3214875 commit c87999b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ It's possible to configure the test run via some environment variables:

### Master

- Fix adding non-image ressources (<https://github.com/marph91/joppy/issues/24>).
- Cast `markup_language` to an appropriate enum type.
- Add changelog.

Expand Down
10 changes: 8 additions & 2 deletions joppy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,14 @@ def add_tag_to_note(self, tag_id: str, note_id: str) -> None:
def add_resource_to_note(self, resource_id: str, note_id: str) -> None:
"""Add a resource to a given note."""
note = self.get_note(id_=note_id, fields="body")
resource = self.get_resource(id_=resource_id, fields="title")
body_with_attachment = f"{note.body}\n![{resource.title}](:/{resource_id})"
resource = self.get_resource(id_=resource_id, fields="title,mime")
# TODO: Use "assertIsNotNone()" when
# https://github.com/python/mypy/issues/5528 is resolved.
assert resource.mime is not None
image_prefix = "!" if resource.mime.startswith("image/") else ""
body_with_attachment = (
f"{note.body}\n{image_prefix}[{resource.title}](:/{resource_id})"
)
self.modify_note(note_id, body=body_with_attachment)

def delete_all_notes(self) -> None:
Expand Down
35 changes: 22 additions & 13 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,28 @@ def test_add(self, filename):
def test_add_to_note(self, filename):
"""Add a resource to an existing note."""
self.api.add_notebook()
note_id = self.api.add_note()
resource_id = self.api.add_resource(filename=filename)
self.api.add_resource_to_note(resource_id=resource_id, note_id=note_id)

# Verify the resource is attached to the note.
resources = self.api.get_resources(note_id=note_id).items
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0].id, resource_id)

# TODO: Seems to be not working.
# notes = self.api.get_notes(resource_id=resource_id)["items"]
# self.assertEqual(len(notes), 1)
# self.assertEqual(notes[0]["id"], note_id)
for file_ in ["test/grant_authorization_button.png", filename]:
with self.subTest(file_=file_):
note_id = self.api.add_note()
resource_id = self.api.add_resource(filename=file_)
self.api.add_resource_to_note(resource_id=resource_id, note_id=note_id)

# Verify the resource is attached to the note.
resources = self.api.get_resources(
note_id=note_id, fields="id,mime"
).items
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0].id, resource_id)

# Verify the markdown is correct (prefix "!" for images).
note = self.api.get_note(id_=note_id, fields="body")
# TODO: Use "assertIsNotNone()" when
# https://github.com/python/mypy/issues/5528 is resolved.
assert resources[0].mime is not None
image_prefix = "!" if resources[0].mime.startswith("image/") else ""
self.assertEqual(
f"\n{image_prefix}[{file_}](:/{resource_id})", note.body
)

@with_resource
def test_delete(self, filename):
Expand Down

0 comments on commit c87999b

Please sign in to comment.