diff --git a/src/palace/manager/feed/serializer/opds.py b/src/palace/manager/feed/serializer/opds.py index 6eba3c7f7..c700ba344 100644 --- a/src/palace/manager/feed/serializer/opds.py +++ b/src/palace/manager/feed/serializer/opds.py @@ -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, @@ -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", @@ -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 = { @@ -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 @@ -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) diff --git a/src/palace/manager/util/opds_writer.py b/src/palace/manager/util/opds_writer.py index 1f84b6d7e..4dd33622f 100644 --- a/src/palace/manager/util/opds_writer.py +++ b/src/palace/manager/util/opds_writer.py @@ -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, @@ -56,6 +57,7 @@ class AtomFeed: "bib": BIB_SCHEMA_NS, "opensearch": OPENSEARCH_NS, "lcp": LCP_NS, + "palaceproperties": PALACE_PROPS_NS, } E = ElementMaker(nsmap=nsmap)