feat(core): improve XML formatting and validation rules
- update `prettify_xml` to strip XML declaration for more flexibility - prepend XML declaration explicitly in Google Merchant feed generation - adjust pagination `page_size` max limit to 128 for stricter validation
This commit is contained in:
parent
0f408fe5de
commit
82f4381fcb
3 changed files with 9 additions and 4 deletions
|
|
@ -162,7 +162,12 @@ class BaseFeedGenerator(ABC):
|
|||
|
||||
@staticmethod
|
||||
def prettify_xml(elem: Element) -> str:
|
||||
"""Return a pretty-printed XML string for the Element."""
|
||||
"""Return a pretty-printed XML string for the Element (without XML declaration)."""
|
||||
rough_string = tostring(elem, encoding="unicode")
|
||||
reparsed = minidom.parseString(rough_string)
|
||||
return reparsed.toprettyxml(indent=" ", encoding=None)
|
||||
pretty = reparsed.toprettyxml(indent=" ")
|
||||
# Strip the XML declaration added by toprettyxml so callers can add their own
|
||||
lines = pretty.split("\n")
|
||||
if lines and lines[0].startswith("<?xml"):
|
||||
lines = lines[1:]
|
||||
return "\n".join(lines).strip()
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ class GoogleMerchantFeedGenerator(BaseFeedGenerator):
|
|||
item = SubElement(channel, "item")
|
||||
self._add_product_to_xml(item, product_data)
|
||||
|
||||
return self.prettify_xml(rss)
|
||||
return '<?xml version="1.0" encoding="UTF-8"?>\n' + self.prettify_xml(rss)
|
||||
|
||||
def _add_product_to_xml(self, item: Element, product_data: dict[str, Any]) -> None:
|
||||
"""Add a product's data to an XML item element."""
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ class CustomPagination(PageNumberPagination):
|
|||
def get_paginated_response(self, data: list[dict[Any, Any]]) -> Response:
|
||||
if not self.page_size:
|
||||
self.page_size = 88
|
||||
if not 1 <= self.page_size <= 255:
|
||||
if not 1 <= self.page_size <= 128:
|
||||
raise ValueError("Page size must be between 1 and 255")
|
||||
if not self.page:
|
||||
paginator = Paginator(data, self.page_size)
|
||||
|
|
|
|||
Loading…
Reference in a new issue