Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
eve-git committed Sep 6, 2024
1 parent cbac6cc commit 30733ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 88 deletions.
76 changes: 39 additions & 37 deletions api/namex/resources/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flask import request, jsonify, g, current_app, make_response
from flask_restx import Namespace, Resource, fields, cors
from flask_jwt_oidc import AuthError
from marshmallow import ValidationError

from namex.constants import DATE_TIME_FORMAT_SQL
from namex.models.request import RequestsAuthSearchSchema
Expand Down Expand Up @@ -744,8 +745,8 @@ def put(nr, *args, **kwargs):
existing_nr.save_to_db()

if json_input.get('consent_dt', None):
consentDate = json_input['consent_dt']
json_input['consent_dt'] = DateUtils.parse_date_string(consentDate, '%d %b %Y %H:%M:%S %Z')
consentDateStr = json_input['consent_dt']
json_input['consent_dt'] = DateUtils.parse_date_string(consentDateStr, '%d %b %Y %H:%M:%S %Z')

# convert Submitted Date to correct format
if json_input.get('submittedDate', None):
Expand Down Expand Up @@ -816,7 +817,8 @@ def put(nr, *args, **kwargs):

try:
previousNr = json_input['previousNr']
nrd.previousRequestId = RequestDAO.find_by_nr(previousNr).requestId
if previousNr:
nrd.previousRequestId = RequestDAO.find_by_nr(previousNr).requestId
except AttributeError:
nrd.previousRequestId = None
except KeyError:
Expand Down Expand Up @@ -1079,39 +1081,40 @@ def put(nr, *args, **kwargs):
is_changed__nwpta_ab = False
is_changed__nwpta_sk = False

for nrd_nwpta in nrd.partnerNS.all():

orig_nwpta = nrd_nwpta.as_dict()

for in_nwpta in json_input['nwpta']:
if nrd_nwpta.partnerJurisdictionTypeCd == in_nwpta['partnerJurisdictionTypeCd']:

errors = nwpta_schema.validate(in_nwpta, partial=False)
if errors:
MessageServices.add_message(MessageServices.ERROR, 'nwpta_validation', errors)
# return make_response(jsonify(errors), 400

nwpta_schema.load(in_nwpta, instance=nrd_nwpta, partial=False)

# convert data to ascii, removing data that won't save to Oracle
nrd_nwpta.partnerName = convert_to_ascii(in_nwpta.get('partnerName'))
nrd_nwpta.partnerNameNumber = convert_to_ascii(in_nwpta.get('partnerNameNumber'))

# check if any of the Oracle db fields have changed, so we can send them back
tmp_is_changed = False
if nrd_nwpta.partnerNameTypeCd != orig_nwpta['partnerNameTypeCd']:
tmp_is_changed = True
if nrd_nwpta.partnerNameNumber != orig_nwpta['partnerNameNumber']:
tmp_is_changed = True
if nrd_nwpta.partnerNameDate != orig_nwpta['partnerNameDate']:
tmp_is_changed = True
if nrd_nwpta.partnerName != orig_nwpta['partnerName']:
tmp_is_changed = True
if tmp_is_changed:
if nrd_nwpta.partnerJurisdictionTypeCd == 'AB':
is_changed__nwpta_ab = True
if nrd_nwpta.partnerJurisdictionTypeCd == 'SK':
is_changed__nwpta_sk = True
if nrd.partnerNS.count() > 0:
for nrd_nwpta in nrd.partnerNS.all():

orig_nwpta = nrd_nwpta.as_dict()

for in_nwpta in json_input['nwpta']:
if nrd_nwpta.partnerJurisdictionTypeCd == in_nwpta['partnerJurisdictionTypeCd']:

errors = nwpta_schema.validate(in_nwpta, partial=False)
if errors:
MessageServices.add_message(MessageServices.ERROR, 'nwpta_validation', errors)
# return make_response(jsonify(errors), 400

nwpta_schema.load(in_nwpta, instance=nrd_nwpta, partial=False)

# convert data to ascii, removing data that won't save to Oracle
nrd_nwpta.partnerName = convert_to_ascii(in_nwpta.get('partnerName'))
nrd_nwpta.partnerNameNumber = convert_to_ascii(in_nwpta.get('partnerNameNumber'))

# check if any of the Oracle db fields have changed, so we can send them back
tmp_is_changed = False
if nrd_nwpta.partnerNameTypeCd != orig_nwpta['partnerNameTypeCd']:
tmp_is_changed = True
if nrd_nwpta.partnerNameNumber != orig_nwpta['partnerNameNumber']:
tmp_is_changed = True
if nrd_nwpta.partnerNameDate != orig_nwpta['partnerNameDate']:
tmp_is_changed = True
if nrd_nwpta.partnerName != orig_nwpta['partnerName']:
tmp_is_changed = True
if tmp_is_changed:
if nrd_nwpta.partnerJurisdictionTypeCd == 'AB':
is_changed__nwpta_ab = True
if nrd_nwpta.partnerJurisdictionTypeCd == 'SK':
is_changed__nwpta_sk = True

### END nwpta ###

Expand Down Expand Up @@ -1143,7 +1146,6 @@ def put(nr, *args, **kwargs):
'is_changed_consent': is_changed_consent
}

# if any data has changed from an NR Details edit, update it in Oracle
if any(value is True for value in change_flags.values()):
nrd.save_to_db()

Expand Down
54 changes: 3 additions & 51 deletions api/namex/resources/utils.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,13 @@
import re
from datetime import datetime
from dateutil import parser


class DateUtils:
# Regex patterns to match the date string formats
# example: "Mon, 18 Sep 2023 23:13:36 UTC"
date_pattern1 = r'^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} UTC$'
# example: "2023-09-18T23:13:36+00:00"
date_pattern2 = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\+\-]\d{2}:\d{2}$'
# example: "2023-09-18 23:13:36.186029+00"
date_pattern3 = r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}[\+\-]\d{2}$'

class DateParseException(Exception):
"""Custom exception for date parsing errors"""
pass

@staticmethod
def parse_date(date_str):
parsed_date = None

# Check if the date string matches the first pattern
if re.match(DateUtils.date_pattern1, date_str):
try:
# If it matches, parse the date string with the first format
parsed_date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S %Z')
except ValueError as e:
raise DateUtils.DateParseException(f"Error parsing date with format '%a, %d %b %Y %H:%M:%S %Z': {e}")

# Check if the date string matches the second pattern
elif re.match(DateUtils.date_pattern2, date_str):
try:
# If it matches, parse the date string with the second format
parsed_date = datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S%z')
except ValueError as e:
raise DateUtils.DateParseException(f"Error parsing date with format '%Y-%m-%dT%H:%M:%S%z': {e}")

# Check if the date string matches the third pattern
elif re.match(DateUtils.date_pattern3, date_str):
try:
# If it matches, parse the date string with the third format
parsed_date = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S.%f%z')
except ValueError as e:
raise DateUtils.DateParseException(f"Error parsing date with format '%Y-%m-%d %H:%M:%S.%f%z': {e}")

# If no match for the predefined patterns, try to parse with datetime.fromisoformat
else:
try:
parsed_date = datetime.fromisoformat(date_str)
except ValueError as e:
raise DateUtils.DateParseException(f"Error parsing date with datetime.fromisoformat: {e}")

if parsed_date is None:
raise DateUtils.DateParseException(f"Unable to parse date: {date_str}")

return parsed_date
return parser.parse(date_str)

@staticmethod
def parse_date_string(date_str, output_date_format):
parsed_date = DateUtils.parse_date(date_str)
parsed_date = parser.parse(date_str)
return parsed_date.strftime(output_date_format)

0 comments on commit 30733ef

Please sign in to comment.