From 0fae807fa01316d9fff9391d56fa3ed83e49d7c6 Mon Sep 17 00:00:00 2001
From: signebedi
Date: Mon, 26 Aug 2024 08:46:34 -0500
Subject: [PATCH] Added: UI all approval page (#62)
---
libreforms_fastapi/app/__init__.py | 41 +++++++++++++++---
.../app/templates/base.html.jinja | 43 ++++++++++++++++++-
.../templates/review_and_approval.html.jinja | 41 +++++++++---------
3 files changed, 98 insertions(+), 27 deletions(-)
diff --git a/libreforms_fastapi/app/__init__.py b/libreforms_fastapi/app/__init__.py
index 4cb3ca9..73209b7 100644
--- a/libreforms_fastapi/app/__init__.py
+++ b/libreforms_fastapi/app/__init__.py
@@ -170,7 +170,7 @@ def make_immutable_map(nested_dict):
for k, v in nested_dict.items()
})
-# @parameterized_lru_cache(maxsize=128)
+@parameterized_lru_cache(maxsize=128)
def cache_form_stage_data(
form_name: str,
form_stages: Map,
@@ -196,7 +196,7 @@ def cache_form_stage_data(
return stage_dict
# @parameterized_lru_cache(maxsize=128)
-# @lru_cache() # Opt for a standard cache because we probably need to rebuild this entire cache when there are changes
+@lru_cache() # Opt for a standard cache because we probably need to rebuild this entire cache when there are changes
def cache_form_stage_data_for_specified_user(
form_name: str,
form_stages: Map,
@@ -1371,10 +1371,15 @@ async def api_form_read_all_needing_action(
mailer = Depends(get_mailer),
doc_db = Depends(get_doc_db),
session: SessionLocal = Depends(get_db),
- key: str = Depends(X_API_KEY)
+ key: str = Depends(X_API_KEY),
+ return_full_records_flat: bool = False,
+ return_count_only: bool = False,
+
):
"""
- This method returns a dict of all the documents needing action from the current user.
+ This method returns a dict of all the documents needing action from the current user. Pass the
+ `return_full_records_flat` option to get all the records in full, and to flatten these into a
+ list of documents. Pass the `return_count_only` option to return an int count of actions needed.
"""
# Ugh, I'd like to find a more efficient way to get the user data. But alas, that
@@ -1404,7 +1409,7 @@ async def api_form_read_all_needing_action(
# Create a recursive Map of the form_stages
__mapped_form_stages = make_immutable_map(__form_model.form_stages)
- print ('\n\n\n', __mapped_form_stages)
+ # print ('\n\n\n', __mapped_form_stages)
__documents = cache_form_stage_data_for_specified_user(
form_name=form_name,
@@ -1416,6 +1421,30 @@ async def api_form_read_all_needing_action(
dict_of_return_values[form_name] = __documents
+ if return_count_only:
+
+ __record_count = sum(len(records) for records in dict_of_return_values.values())
+
+ return {"record_count": __record_count}
+
+ if return_full_records_flat:
+
+ __temp = []
+ seen_records = set()
+
+ for __form_name, __records in dict_of_return_values.items():
+ for __record_id in __records:
+ if __record_id not in seen_records:
+ __record = doc_db.get_one_document(
+ form_name=__form_name,
+ document_id=__record_id,
+ )
+ __temp.append(__record)
+ seen_records.add(__record_id)
+
+ return {"documents": __temp}
+
+
# Write this query to the TransactionLog
if config.COLLECT_USAGE_STATISTICS:
@@ -1431,7 +1460,7 @@ async def api_form_read_all_needing_action(
)
- return dict_of_return_values
+ return {"documents": dict_of_return_values}
# Read one form
diff --git a/libreforms_fastapi/app/templates/base.html.jinja b/libreforms_fastapi/app/templates/base.html.jinja
index d7c23e8..1289cad 100644
--- a/libreforms_fastapi/app/templates/base.html.jinja
+++ b/libreforms_fastapi/app/templates/base.html.jinja
@@ -81,7 +81,17 @@
{% endif %}
- Review & Approval
+
+
+
+ Review & Approval
+
+
+
+
+
+
+
@@ -426,6 +436,36 @@
{% if request.user.is_authenticated %}
+
+ $.ajax({
+ url: '/api/form/read_all_needing_action?return_count_only=true',
+ type: 'GET',
+ dataType: 'json',
+ beforeSend: function(xhr) {
+ xhr.setRequestHeader('X-API-KEY', "{{ request.user.api_key }}");
+ },
+ success: function(response) {
+
+ // console.log('AJAX response:', response); // Debugging: log the entire response
+
+ // Assuming response.record_count contains the number of actions needed
+ if (response.record_count && response.record_count > 0) {
+ // Create the badge
+ var badge = `${response.record_count}`;
+
+ // Insert the badge into the container
+ $('#actionBadgeContainer').html(badge);
+ } else {
+ // Clear the badge if record_count is 0 or undefined
+ $('#actionBadgeContainer').empty();
+ }
+ },
+ error: function(xhr, status, error) {
+ console.error('Error:', status, error);
+ }
+ });
+
+
if (localStorage.getItem('jwt_token')) {
$.ajax({
url: '/api/auth/refresh',
@@ -444,6 +484,7 @@
});
+
} else {
console.log("No JWT token found in localStorage.");
}
diff --git a/libreforms_fastapi/app/templates/review_and_approval.html.jinja b/libreforms_fastapi/app/templates/review_and_approval.html.jinja
index f5d1ae7..d498519 100644
--- a/libreforms_fastapi/app/templates/review_and_approval.html.jinja
+++ b/libreforms_fastapi/app/templates/review_and_approval.html.jinja
@@ -14,7 +14,7 @@
There are no submissions that require your review or approval at this time.
-{#
+
-#}
+
{% endblock %}
@@ -41,7 +41,7 @@
{% endblock %}