Skip to content

Commit

Permalink
Added time tracking links to feed entries
Browse files Browse the repository at this point in the history
  • Loading branch information
RishiDiwanTT committed Jul 28, 2023
1 parent 22bcfe5 commit d735181
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/opds.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
Patron,
Session,
)
from core.model.constants import EditionConstants, LinkRelations
from core.model.formats import FormatPriorities
from core.model.integration import IntegrationConfiguration
from core.opds import AcquisitionFeed, Annotator, UnfulfillableWork
Expand Down Expand Up @@ -786,6 +787,19 @@ def annotate_work_entry(
),
)

if edition.medium == EditionConstants.AUDIO_MEDIUM:
feed.add_link_to_entry(
entry,
rel=LinkRelations.TIME_TRACKING,
href=self.url_for(
"track_playtime_events",
identifier_type=identifier.type,
identifier=identifier.identifier,
library_short_name=self.library.short_name,
_external=True,
),
)

@classmethod
def related_books_available(cls, work, library):
""":return: bool asserting whether related books might exist for a particular Work"""
Expand Down
2 changes: 2 additions & 0 deletions core/model/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ class LinkRelations:
DRM_ENCRYPTED_DOWNLOAD = "http://opds-spec.org/acquisition/"
BORROW = "http://opds-spec.org/acquisition/borrow"

TIME_TRACKING = "http://palaceproject.io/terms/timeTracking"

CIRCULATION_ALLOWED = [
OPEN_ACCESS_DOWNLOAD,
DRM_ENCRYPTED_DOWNLOAD,
Expand Down
31 changes: 31 additions & 0 deletions tests/api/test_opds.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
Work,
get_one,
)
from core.model.constants import EditionConstants, LinkRelations
from core.model.formats import FormatPriorities
from core.model.integration import IntegrationConfiguration
from core.model.licensing import LicensePool
Expand Down Expand Up @@ -925,6 +926,36 @@ def test_annotate_work_entry(self, annotator_fixture: LibraryAnnotatorFixture):
assert feed_link["href"] == link.resource.url
assert feed_link["type"] == link.resource.representation.media_type

# Annotate time tracking
work = annotator_fixture.db.work()
edition = work.presentation_edition
edition.medium = EditionConstants.AUDIO_MEDIUM

entry = feed._make_entry_xml(work, edition)
annotator.annotate_work_entry(work, None, edition, identifier, feed, entry)

time_tracking_links = entry.findall(
f"link[@rel='{LinkRelations.TIME_TRACKING}']"
)
assert len(time_tracking_links) == 1
assert time_tracking_links[0].get("href") == annotator.url_for(
"track_playtime_events",
identifier_type=identifier.type,
identifier=identifier.identifier,
library_short_name=annotator.library.short_name,
_external=True,
)

# Book mediums don't get time tracking
edition.medium = EditionConstants.BOOK_MEDIUM
entry = feed._make_entry_xml(work, edition)
annotator.annotate_work_entry(work, None, edition, identifier, feed, entry)

time_tracking_links = entry.findall(
f"link[@rel='{LinkRelations.TIME_TRACKING}']"
)
assert len(time_tracking_links) == 0

def test_annotate_feed(self, annotator_fixture: LibraryAnnotatorFixture):
lane = annotator_fixture.db.lane()
linksets = []
Expand Down

0 comments on commit d735181

Please sign in to comment.