From d0733d59148575039b1eb8a763a4cd7e35106b94 Mon Sep 17 00:00:00 2001
From: Bobby Novak <176936850+rnovak338@users.noreply.github.com>
Date: Wed, 11 Dec 2024 10:22:06 -0500
Subject: [PATCH] Resolve 500 error with dissemination search/advanced (#4497)
* Include error-handling
Handled search AND advanced search.
- Add form error labels for start and end dates in search.
- Re-render search template instead of raising validation errors.
* Create error alert template
* Update POST for Search/Advanced
- Removed "duplicate" logic for rendering form when it is not valid.
- Instead, re-uses the logic for success and only applies a search on the tables when `form.is_valid()`.
* Render user selection of audit years on form error
* Linting
* Provide error context
Now displaying list of erroneous fields in error message when the form is invalid.
* Update search-alert-error.html
Fixes padding issue if there are no errors.
---
backend/dissemination/search.py | 18 +++
.../templates/search-alert-error.html | 15 +++
backend/dissemination/templates/search.html | 4 +-
.../search_filters/acceptance-date.html | 26 ++--
.../templates/search_filters/audit-year.html | 18 ++-
backend/dissemination/views.py | 112 ++++++++++--------
6 files changed, 125 insertions(+), 68 deletions(-)
create mode 100644 backend/dissemination/templates/search-alert-error.html
diff --git a/backend/dissemination/search.py b/backend/dissemination/search.py
index 4c3ed69a0a..827dcb1a3d 100644
--- a/backend/dissemination/search.py
+++ b/backend/dissemination/search.py
@@ -1,3 +1,4 @@
+import json
import logging
import time
from .searchlib.search_constants import ORDER_BY, DIRECTION, DAS_LIMIT
@@ -147,3 +148,20 @@ def _make_distinct(results, params):
results = results.distinct("report_id", order_by)
return results
+
+
+def gather_errors(form):
+ """
+ Gather errors based on the form fields inputted by the user.
+ """
+ formatted_errors = []
+ if form.errors:
+ if not form.is_valid():
+ errors = json.loads(form.errors.as_json())
+ for error in reversed(errors):
+ if "start_date" in error:
+ formatted_errors.append("The start date you entered is invalid.")
+ if "end_date" in error:
+ formatted_errors.append("The end date you entered is invalid.")
+
+ return formatted_errors
diff --git a/backend/dissemination/templates/search-alert-error.html b/backend/dissemination/templates/search-alert-error.html
new file mode 100644
index 0000000000..8139d63011
--- /dev/null
+++ b/backend/dissemination/templates/search-alert-error.html
@@ -0,0 +1,15 @@
+{% load humanize %}
+{% load sprite_helper %}
+
+
+
Error
+
The information you entered is invalid. Please double-check your information for errors.