From c0703d3321c8148da2c4a09bfb757b9460fd6d63 Mon Sep 17 00:00:00 2001 From: signebedi Date: Fri, 6 Sep 2024 14:26:27 -0500 Subject: [PATCH] Added: UI repr for linked references (#344) --- libreforms_fastapi/app/__init__.py | 12 +-- .../app/templates/read_one_form.html.jinja | 101 +++++++++++++++++- 2 files changed, 101 insertions(+), 12 deletions(-) diff --git a/libreforms_fastapi/app/__init__.py b/libreforms_fastapi/app/__init__.py index e210673..0dc1fce 100644 --- a/libreforms_fastapi/app/__init__.py +++ b/libreforms_fastapi/app/__init__.py @@ -1628,7 +1628,6 @@ async def api_form_get_linked_references( for _form_name, _linked_fields in dict_of_relevant_links.items(): - # read_all IS THE HIGHER PRIVILEGE OF THE TWO - SO WE SHOULD CHECK FOR THAT FIRST, AS IT # INCLUDES read_own. https://github.com/signebedi/libreforms-fastapi/issues/307. try: @@ -1644,7 +1643,6 @@ async def api_form_get_linked_references( raise HTTPException(status_code=403, detail=f"{e}") - for _linked_field in _linked_fields: _documents = [] # This query param will only return that matches the given document_id @@ -1654,7 +1652,7 @@ async def api_form_get_linked_references( form_name=_form_name, limit_users=limit_query_to, exclude_journal=True, - collapse_data=True, + # collapse_data=True, # sort_by_last_edited=True, # newest_first=True, query_params=query_params, @@ -1665,7 +1663,7 @@ async def api_form_get_linked_references( # Drop duplicates and sort! unique_documents = {} for doc in documents: - doc_id = doc['__metadata__document_id'] + doc_id = doc['metadata']['document_id'] # Replace the document if this one is newer if doc_id not in unique_documents: @@ -1674,7 +1672,7 @@ async def api_form_get_linked_references( # Now we have a dictionary of unique documents; we need to sort them by 'last_modified' sorted_documents = sorted( unique_documents.values(), - key=lambda x: datetime.fromisoformat(x['__metadata__last_modified'].replace('Z', '+00:00')), + key=lambda x: datetime.fromisoformat(x['metadata']['last_modified'].replace('Z', '+00:00')), reverse=True ) @@ -3548,8 +3546,8 @@ async def api_auth_get( 'related_user_id': x['user']['id'], } for x in _received_relationships] - print("\n\n\n", profile_data['relationships']) - print("\n\n\n", profile_data['received_relationships']) + # print("\n\n\n", profile_data['relationships']) + # print("\n\n\n", profile_data['received_relationships']) # Write this query to the TransactionLog diff --git a/libreforms_fastapi/app/templates/read_one_form.html.jinja b/libreforms_fastapi/app/templates/read_one_form.html.jinja index 20fb4a3..dc49381 100644 --- a/libreforms_fastapi/app/templates/read_one_form.html.jinja +++ b/libreforms_fastapi/app/templates/read_one_form.html.jinja @@ -60,13 +60,31 @@
-
- +
+ + - - - {% endblock %} {% block scripts %} @@ -150,6 +168,20 @@ function restoreVersion(){ } +// https://www.geeksforgeeks.org/how-to-truncate-a-string-in-javascript/ +function GFG(str, maxLength) { + if (str.length > maxLength) { + return str.substring(0, maxLength) + '...'; + } + return str; +} + +function formatObject(obj) { + return Object.entries(obj) + .map(([key, value]) => `${key}: ${value}`) + .join(', '); +} + $(document).ready(function() { @@ -454,7 +486,66 @@ $(document).ready(function() { }); } + function fetchLinkedReferences() { + $.ajax({ + url: `/api/form/get_linked_refs/${formName}/${documentId}`, + type: 'GET', + headers: { + 'X-API-KEY': apiKey, + }, + dataType: 'json', + success: function(response) { + if (response.length > 0) { + $('#linkedRefsSection').show(); // Show the section + $('#linkedRefsTable').show(); // Show the table + $('#noLinkedRefsMessage').hide(); // Hide the no data message + response.forEach(doc => { + let row = ` + + ${doc.metadata.document_id} + + ${doc.metadata.form_name.replace(/_/g, ' ')} + ${GFG(formatObject(doc['data']), 150)} + + + ${doc['metadata']['last_editor']} + + + ${prettifyTimeDiff(doc['metadata']['last_modified'], "{{config['TIMEZONE']|string}}")} + + + ${doc['metadata']['created_by']} + + + ${prettifyTimeDiff(doc['metadata']['created_at'], "{{config['TIMEZONE']|string}}")} + + + `; + $('#linkedRefsTable tbody').append(row); + }); + $('#linkedRefsTable').DataTable({ + "ordering": true, + "order": [[4, "desc"]], + "lengthChange": false, + "info": false, + // "searching": false, + "paging": false + }); + } else { + $('#linkedRefsSection').hide(); // Hide the section + $('#noLinkedRefsMessage').show(); // Show the no data message + } + }, + error: function(xhr) { + console.error('Failed to fetch linked references', xhr.responseText); + $('#linkedRefsSection').hide(); // Also hide on error + $('#noLinkedRefsMessage').show(); // Show the no data message + } + }); + } + fetchDocument(); + fetchLinkedReferences(); });