Skip to content

Commit bf7e1e5

Browse files
committed
Fixed: duplicates and sorting for form get linked refs (#344)
1 parent 786a5b8 commit bf7e1e5

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

libreforms_fastapi/app/__init__.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ async def api_form_read_all_needing_action(
15691569

15701570
# Read all forms that reference the given form_name and document_id
15711571
@app.get("/api/form/get_linked_refs/{form_name}/{document_id}", dependencies=[Depends(api_key_auth)])
1572-
async def api_form_read_all_needing_action(
1572+
async def api_form_get_linked_references(
15731573
form_name: str,
15741574
document_id: str,
15751575
background_tasks: BackgroundTasks,
@@ -1658,8 +1658,22 @@ async def api_form_read_all_needing_action(
16581658
)
16591659

16601660
documents.extend(_documents)
1661-
1662-
# Placeholder: drop duplicates and sort!
1661+
1662+
# Drop duplicates and sort!
1663+
unique_documents = {}
1664+
for doc in documents:
1665+
doc_id = doc['__metadata__document_id']
1666+
1667+
# Replace the document if this one is newer
1668+
if doc_id not in unique_documents:
1669+
unique_documents[doc_id] = doc
1670+
1671+
# Now we have a dictionary of unique documents; we need to sort them by 'last_modified'
1672+
sorted_documents = sorted(
1673+
unique_documents.values(),
1674+
key=lambda x: datetime.fromisoformat(x['__metadata__last_modified'].replace('Z', '+00:00')),
1675+
reverse=True
1676+
)
16631677

16641678
# Write this query to the TransactionLog
16651679
if config.COLLECT_USAGE_STATISTICS:
@@ -1675,7 +1689,7 @@ async def api_form_read_all_needing_action(
16751689
query_params={},
16761690
)
16771691

1678-
return {"documents": documents}
1692+
return sorted_documents
16791693

16801694

16811695

0 commit comments

Comments
 (0)