Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#23050 Handle different expiration date format #1571

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/namex/VERSION.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.4'
__version__ = '1.2.5'
113 changes: 55 additions & 58 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 @@ -39,6 +40,8 @@
from namex.utils.auth import cors_preflight
from namex.analytics import SolrQueries, RestrictedWords, VALID_ANALYSIS as ANALYTICS_VALID_ANALYSIS
from namex.utils import queue_util
from .utils import DateUtils


setup_logging() # Important to do this first

Expand Down Expand Up @@ -742,33 +745,26 @@ def put(nr, *args, **kwargs):
existing_nr.save_to_db()

if json_input.get('consent_dt', None):
json_input['consent_dt'] = str(datetime.strptime(
str(json_input['consent_dt'][5:]), '%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):
json_input['submittedDate'] = str(datetime.strptime(
str(json_input['submittedDate'][5:]), '%d %b %Y %H:%M:%S %Z'))
submittedDateStr = json_input['submittedDate']
json_input['submittedDate'] = DateUtils.parse_date_string(submittedDateStr, '%d %b %Y %H:%M:%S %Z')

# convert Expiration Date to correct format
if json_input.get('expirationDate', None):
expiration_str = json_input['expirationDate']

# Convert the date (either UTC or ISO) into a UTC datetime object
if expiration_str.endswith('UTC'):
parsed_date = datetime.strptime(expiration_str[5:], '%d %b %Y %H:%M:%S %Z')
parsed_date = parsed_date.replace(tzinfo=UTC)
else:
parsed_date = datetime.fromisoformat(expiration_str)
if parsed_date.tzinfo is None:
parsed_date = parsed_date.replace(tzinfo=UTC) # If it's naive, assume UTC
else:
parsed_date = parsed_date.astimezone(UTC) # Convert any timezone-aware datetime to UTC

# Convert the UTC datetime object to the end of day in pacific time without milliseconds
pacific_time = parsed_date.astimezone(timezone('US/Pacific'))
end_of_day_pacific = pacific_time.replace(hour=23, minute=59, second=0, microsecond=0)
json_input['expirationDate'] = end_of_day_pacific.strftime('%Y-%m-%d %H:%M:%S%z')
try:
expirationDateStr = json_input['expirationDate']
expirationDate = DateUtils.parse_date(expirationDateStr)
# Convert the UTC datetime object to the end of day in pacific time without milliseconds
pacific_time = expirationDate.astimezone(timezone('US/Pacific'))
end_of_day_pacific = pacific_time.replace(hour=23, minute=59, second=0, microsecond=0)
json_input['expirationDate'] = end_of_day_pacific.strftime('%Y-%m-%d %H:%M:%S%z')
except Exception as e:
current_app.logger.debug(f"Error parsing expirationDate: {str(e)}")
pass

# convert NWPTA dates to correct format
if json_input.get('nwpta', None):
Expand All @@ -777,8 +773,8 @@ def put(nr, *args, **kwargs):
if region['partnerNameDate'] == '':
region['partnerNameDate'] = None
if region['partnerNameDate']:
region['partnerNameDate'] = str(datetime.strptime(
str(region['partnerNameDate']), '%d-%m-%Y'))
partnerNameDateStr = region['partnerNameDate']
region['partnerNameDate'] = DateUtils.parse_date_string(partnerNameDateStr, '%d-%m-%Y')
except ValueError:
pass
# pass on this error and catch it when trying to add to record, to be returned
Expand Down Expand Up @@ -821,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 @@ -1084,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 @@ -1148,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
13 changes: 13 additions & 0 deletions api/namex/resources/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dateutil import parser


class DateUtils:

@staticmethod
def parse_date(date_str):
return parser.parse(date_str)

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