Skip to content

Commit

Permalink
WP-720: [APCD][React] Extension form submit hookup (#340)
Browse files Browse the repository at this point in the history
* Submit handling for extension

* Add error message ui element

* - Suggested styles changes for extension form (#342)

* Update apcd-cms/src/client/src/components/Submitter/Extensions/ExtensionsForm.tsx

Co-authored-by: sophia-massie <[email protected]>

* Delete unused templates and css

---------

Co-authored-by: sophia-massie <[email protected]>
  • Loading branch information
chandra-tacc and sophia-massie authored Oct 31, 2024
1 parent 565218a commit e97d3fd
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 400 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

28 changes: 9 additions & 19 deletions apcd-cms/src/apps/extension/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from apps.utils.utils import title_case
from datetime import datetime
import logging
import json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -46,30 +47,19 @@ def post(self, request):
Handle form submission and return JSON response for success/failure
"""
if request.user.is_authenticated and has_apcd_group(request.user):
form = request.POST.copy()
form = json.loads(request.body)
extensions = form['extensions']
errors = []
submitters = request.session.get('submitters')

max_iterations = 1
for i in range(2, 6):
# Check if subsequent fields are present to determine the number of iterations
if form.get(f'requested-target-date_{i}'):
max_iterations += 1
else:
break

# Process the form for each submitter
for iteration in range(max_iterations):
submitter = next(
submitter for submitter in submitters
if int(submitter[0]) == int(form[f'business-name_{iteration + 1}'])
)
exten_resp = apcd_database.create_extension(form, iteration + 1, submitter)
submitters = apcd_database.get_submitter_info(request.user.username)
for extension in extensions:
submitter = next(submitter for submitter in submitters if int(submitter[0] == int(extension['businessName'])))
exten_resp = apcd_database.create_extension(form, extension, submitter)
if self._err_msg(exten_resp):
errors.append(self._err_msg(exten_resp))

# Return success or error as JSON
if errors:
logger.error("Extension request failed. Errors: %s", errors)
return JsonResponse({'status': 'error', 'errors': errors}, status=400)
else:
return JsonResponse({'status': 'success'}, status=200)
Expand All @@ -78,7 +68,7 @@ def post(self, request):

def _set_submitter(self, sub):
"""
Helper function to structure the submitter info
Helper function to structure the submitter info
"""
return {
"submitter_id": sub[0],
Expand Down
40 changes: 12 additions & 28 deletions apcd-cms/src/apps/utils/apcd_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,41 +961,25 @@ def get_all_submissions_and_logs():
if conn is not None:
conn.close()

def create_extension(form, iteration, sub_data):
def create_extension(form, extension, sub_data):
cur = None
conn = None
values = ()
try:
if iteration > 1:
values = (
_clean_value(form['business-name_{}'.format(iteration)]),
_clean_date(form['requested-target-date_{}'.format(iteration)]),
_clean_value(form['extension-type_{}'.format(iteration)]),
int((form['applicable-data-period_{}'.format(iteration)].replace("-", ""))),
_clean_date(form['hidden-current-expected-date_{}'.format(iteration)]),
"pending",
_clean_value(sub_data[1]),
_clean_value(sub_data[2]),
_clean_value(sub_data[3]),
_clean_value(form["requestor-name"]),
_clean_email(form["requestor-email"]),
_clean_value(form["justification"])
)
else:
values = (
_clean_value(form['business-name_{}'.format(iteration)]),
_clean_date(form['requested-target-date_{}'.format(iteration)]),
_clean_value(form['extension-type_{}'.format(iteration)]),
int((form['applicable-data-period_{}'.format(iteration)].replace("-", ""))),
_clean_date(form['hidden-current-expected-date_{}'.format(iteration)]),
values = (
_clean_value(extension['businessName']),
_clean_value(extension['requestedTargetDate']),
_clean_value(extension['extensionType']),
_clean_value(extension['applicableDataPeriod'].replace("-", "")),
_clean_value(extension['currentExpectedDate']),
"pending",
_clean_value(sub_data[1]),
_clean_value(sub_data[2]),
_clean_value(sub_data[3]),
_clean_value(form["requestor-name"]),
_clean_email(form["requestor-email"]),
_clean_value(form["requestorName"]),
_clean_email(form["requestorEmail"]),
_clean_value(form["justification"])
)
)

operation = """INSERT INTO extensions(
submitter_id,
Expand All @@ -1022,10 +1006,10 @@ def create_extension(form, iteration, sub_data):
)
cur = conn.cursor()
cur.execute(operation, values)
conn.commit()
conn.commit()

except Exception as error:
logger.error(error)
logger.error('DB error related to extension request creation: %s', error)
return error

finally:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.requiredText {
color: red;
.isInvalid {
width: 100%; /* To match error message styles from*/
margin-top: 0.25rem;
font-size: 80%;
color: #dc3545;
}

.dateInputField {
Expand Down
Loading

0 comments on commit e97d3fd

Please sign in to comment.