Skip to content

Commit 330ef3e

Browse files
committed
Add pagination for attachments
1 parent 7375bcc commit 330ef3e

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

confluence_markdown_exporter/confluence.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,31 @@ def from_json(cls, data: JsonResponse) -> "Attachment":
317317
version=Version.from_json(data.get("version", {})),
318318
)
319319

320+
@classmethod
321+
def from_page_id(cls, page_id: int) -> list["Attachment"]:
322+
attachments = []
323+
start = 0
324+
paging_limit = 50
325+
size = paging_limit # Initialize to limit to enter the loop
326+
327+
while size >= paging_limit:
328+
response = cast(
329+
JsonResponse,
330+
confluence.get_attachments_from_content(
331+
page_id,
332+
start=start,
333+
limit=paging_limit,
334+
expand="container.ancestors,version",
335+
),
336+
)
337+
338+
attachments.extend([cls.from_json(att) for att in response.get("results", [])])
339+
340+
size = response.get("size", 0)
341+
start += size
342+
343+
return attachments
344+
320345
def export(self, export_path: StrPath) -> None:
321346
filepath = Path(export_path) / self.export_path
322347
if filepath.exists():
@@ -477,14 +502,6 @@ def get_attachments_by_title(self, title: str) -> list[Attachment]:
477502

478503
@classmethod
479504
def from_json(cls, data: JsonResponse) -> "Page":
480-
attachments = cast(
481-
JsonResponse,
482-
confluence.get_attachments_from_content(
483-
data.get("id", 0),
484-
limit=1000,
485-
expand="container.ancestors,version",
486-
),
487-
)
488505
return cls(
489506
id=data.get("id", 0),
490507
title=data.get("title", ""),
@@ -496,9 +513,7 @@ def from_json(cls, data: JsonResponse) -> "Page":
496513
Label.from_json(label)
497514
for label in data.get("metadata", {}).get("labels", {}).get("results", [])
498515
],
499-
attachments=[
500-
Attachment.from_json(attachment) for attachment in attachments.get("results", [])
501-
],
516+
attachments=Attachment.from_page_id(data.get("id", 0)),
502517
ancestors=[ancestor.get("id") for ancestor in data.get("ancestors", [])][1:],
503518
)
504519

0 commit comments

Comments
 (0)