Skip to content

Commit

Permalink
Add keep_latest_rd_document method
Browse files Browse the repository at this point in the history
  • Loading branch information
ttys0dev committed Jan 6, 2025
1 parent e0c1517 commit 9f74cc1
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions cl/recap/mergers.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,25 +822,35 @@ async def get_or_make_docket_entry(
return de, de_created


async def clean_duplicate_documents(params: dict[str, Any]) -> RECAPDocument:
async def keep_latest_rd_document(queryset: QuerySet) -> RECAPDocument:
"""Removes duplicate RECAPDocuments, keeping the most recent with PDF if
available or otherwise the most recent overall.
:param params: Query parameters to filter the RECAPDocuments.
:param params: RECAPDocument QuerySet to clean duplicates from.
:return: The matched RECAPDocument after cleaning.
"""
duplicate_rd_queryset = RECAPDocument.objects.filter(**params)
rd_with_pdf_queryset = duplicate_rd_queryset.filter(
rd_with_pdf_queryset = queryset.filter(
is_available=True
).exclude(filepath_local="")
if await rd_with_pdf_queryset.aexists():
rd = await rd_with_pdf_queryset.alatest("date_created")
else:
rd = await duplicate_rd_queryset.alatest("date_created")
await duplicate_rd_queryset.exclude(pk=rd.pk).adelete()
rd = await queryset.alatest("date_created")
await queryset.exclude(pk=rd.pk).adelete()
return rd


async def clean_duplicate_documents(params: dict[str, Any]) -> RECAPDocument:
"""Removes duplicate RECAPDocuments, keeping the most recent with PDF if
available or otherwise the most recent overall.
:param params: Query parameters to filter the RECAPDocuments.
:return: The matched RECAPDocument after cleaning.
"""
duplicate_rd_queryset = RECAPDocument.objects.filter(**params)
return await keep_latest_rd_document(duplicate_rd_queryset)


async def add_docket_entries(
d: Docket,
docket_entries: list[dict[str, Any]],
Expand Down Expand Up @@ -1627,14 +1637,7 @@ async def clean_duplicate_attachment_entries(
)
async for dupe in dupes.aiterator():
duplicate_rd_queryset = rds.filter(pacer_doc_id=dupe.pacer_doc_id)
rd_with_pdf_queryset = duplicate_rd_queryset.filter(
is_available=True
).exclude(filepath_local="")
if await rd_with_pdf_queryset.aexists():
keep_rd = await rd_with_pdf_queryset.alatest("date_created")
else:
keep_rd = await duplicate_rd_queryset.alatest("date_created")
await duplicate_rd_queryset.exclude(pk=keep_rd.pk).adelete()
await keep_latest_rd_document(duplicate_rd_queryset)


async def merge_attachment_page_data(
Expand Down

0 comments on commit 9f74cc1

Please sign in to comment.