Skip to content

Commit

Permalink
WIP: Present sorts as links with palaceproject.io namespaced sort rels.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbernstein committed Oct 15, 2024
1 parent 5155b4d commit 5329e3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/palace/manager/feed/serializer/opds.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from lxml import etree

from palace.manager.core.facets import FacetConstants
from palace.manager.feed.serializer.base import SerializerInterface
from palace.manager.feed.types import (
Acquisition,
Expand All @@ -15,10 +16,11 @@
FeedEntryType,
FeedMetadata,
IndirectAcquisition,
Link,
WorkEntryData,
)
from palace.manager.util.datetime_helpers import utc_now
from palace.manager.util.opds_writer import OPDSFeed, OPDSMessage
from palace.manager.util.opds_writer import AtomFeed, OPDSFeed, OPDSMessage

TAG_MAPPING = {
"indirectAcquisition": f"{{{OPDSFeed.OPDS_NS}}}indirectAcquisition",
Expand All @@ -42,6 +44,7 @@
"facetGroupType": f"{{{OPDSFeed.SIMPLIFIED_NS}}}facetGroupType",
"activeFacet": f"{{{OPDSFeed.OPDS_NS}}}activeFacet",
"ratingValue": f"{{{OPDSFeed.SCHEMA_NS}}}ratingValue",
"activeSort": f"{{{OPDSFeed.PALACE_PROPS_NS}}}active-sort",
}

AUTHOR_MAPPING = {
Expand Down Expand Up @@ -107,11 +110,22 @@ def serialize_feed(
serialized.append(breadcrumbs)

for link in feed.facet_links:
if self.is_sort_link(link):
serialized.append(self._serialize_sort_link(link))
serialized.append(self._serialize_feed_entry("link", link))

etree.indent(serialized)
return self.to_string(serialized)

def is_sort_link(self, link) -> bool:
return (
hasattr(link, "facetGroup")
and link.facetGroup
== FacetConstants.GROUP_DISPLAY_TITLES[
FacetConstants.ORDER_FACET_GROUP_NAME
]
)

def _serialize_feed_metadata(self, metadata: FeedMetadata) -> list[etree._Element]:
tags = []
# Compulsory title
Expand Down Expand Up @@ -390,3 +404,9 @@ def to_string(cls, element: etree._Element) -> str:

def content_type(self) -> str:
return OPDSFeed.ACQUISITION_FEED_TYPE

def _serialize_sort_link(self, link: Link) -> etree._Element:
sort_link = Link(href=link.href, title=link.title, rel=AtomFeed.PALACE_REL_NS)
if link.get("activeFacet", False):
sort_link.add_attributes(dict(activeSort="true"))
return self._serialize_feed_entry("link", sort_link)
4 changes: 3 additions & 1 deletion src/palace/manager/util/opds_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class AtomFeed:
BIB_SCHEMA_NS = "http://bib.schema.org/"

LCP_NS = "http://readium.org/lcp-specs/ns"

PALACE_REL_NS = "http://palaceproject.io/terms/rel/"
PALACE_PROPS_NS = "http://palaceproject.io/terms/properties/"
nsmap = {
None: ATOM_NS,
"app": APP_NS,
Expand All @@ -56,6 +57,7 @@ class AtomFeed:
"bib": BIB_SCHEMA_NS,
"opensearch": OPENSEARCH_NS,
"lcp": LCP_NS,
"palaceproperties": PALACE_PROPS_NS,
}

E = ElementMaker(nsmap=nsmap)
Expand Down

0 comments on commit 5329e3d

Please sign in to comment.