Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add non-image resources properly #25

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading