def test_now_ts_offset(): """Assert that adjusting UTC now by a number of days is performing as expected.""" now_ts = model_utils.now_ts() + _timedelta(days=60) test_ts = model_utils.now_ts_offset(60, True) print('Now timestamp + 60 days: ' + model_utils.format_ts(test_ts)) assert test_ts.day == now_ts.day assert test_ts.month == now_ts.month assert test_ts.year == now_ts.year now_ts = model_utils.now_ts() - _timedelta(days=60) test_ts = model_utils.now_ts_offset(60, False) print('Now timestamp - 60 days: ' + model_utils.format_ts(test_ts)) assert test_ts.day == now_ts.day assert test_ts.month == now_ts.month assert test_ts.year == now_ts.year
def create_from_jwt_token(cls, token: dict, account_id: str = None): """Create a user record from the provided JWT token. Use the values found in the vaild JWT for the realm to populate the User audit data """ if token: # TODO: schema doesn't parse from token need to figure that out ... LATER! # s = KeycloakUserSchema() # u = s.load(data=token, partial=True) firstname = token.get('given_name', None) if not firstname: firstname = token.get('firstname', None) lastname = token.get('family_name', None) if not lastname: lastname = token.get('lastname', None) user = User( username=token.get('username', None), firstname=firstname, lastname=lastname, iss=token['iss'], sub=token['sub'], account_id=account_id ) user.creation_date = model_utils.now_ts() current_app.logger.debug('Creating user from JWT:{}; User:{}'.format(token, user)) db.session.add(user) db.session.commit() return user return None
def create(key_id: int, event_type: str, status: int = None, message: str = None): """Create an EventTracking record.""" event_tracking = EventTracking(key_id=key_id, event_tracking_type=event_type, status=status, message=message) event_tracking.event_ts = model_utils.now_ts() event_tracking.save() return event_tracking
def update(cls, request_json, document_number: str = None): """Update an existing draft statement by document number.""" draft = None if request_json and document_number: draft = cls.find_by_document_number(document_number, False) if draft: draft.update_ts = model_utils.now_ts() draft.draft = json.dumps(request_json) if request_json['type'] == 'AMENDMENT_STATEMENT': if 'courtOrderInformation' in request_json: draft.registration_type_cl = 'COURTORDER' else: draft.registration_type_cl = 'AMENDMENT' draft.registration_type_cd = request_json[ 'amendmentStatement']['changeType'] draft.registration_number = request_json['amendmentStatement'][ 'baseRegistrationNumber'] elif request_json['type'] == 'CHANGE_STATEMENT': draft.registration_type_cl = 'CHANGE' draft.registration_type_cd = request_json['changeStatement'][ 'changeType'] draft.registration_number = request_json['changeStatement'][ 'baseRegistrationNumber'] else: draft.registration_type_cl = 'PPSALIEN' draft.registration_type_cd = request_json[ 'financingStatement']['type'] return draft
def save(self): """Save the object to the database immediately.""" if not self.create_ts: self.create_ts = model_utils.now_ts() db.session.add(self) db.session.commit() return self.json
def test_expiry_dt_repairer_lien_now(): """Assert that the computed expiry date for a repairer's lien performs as expected.""" test_ts = model_utils.expiry_dt_repairer_lien() now_ts = model_utils.now_ts() delta = test_ts - now_ts assert delta.days == model_utils.REPAIRER_LIEN_DAYS assert test_ts.hour in (6, 7) assert test_ts.minute == 59 assert test_ts.second == 59
def test_expiry_dt_from_years(): """Assert that generating an expiry date from life years is performing as expected.""" expiry_ts = model_utils.expiry_dt_from_years(5) now_ts = model_utils.now_ts() print('Expiry timestamp: ' + model_utils.format_ts(expiry_ts)) print('Now timestamp: ' + model_utils.format_ts(now_ts)) assert (expiry_ts.year - now_ts.year) == 5 assert expiry_ts.hour in (6, 7) assert expiry_ts.minute == 59 assert expiry_ts.second == 59 assert expiry_ts.day in (1, now_ts.day, (now_ts.day + 1)) assert expiry_ts.month in (now_ts.month, (now_ts.month + 1))
def create_from_json(search_json, account_id: str = None): """Create a search object from dict/json.""" new_search = SearchClient() new_search.request_json = search_json search_type = search_json['type'] new_search.search_type_cd = model_utils.TO_DB_SEARCH_TYPE[search_type] new_search.search_criteria = json.dumps(search_json) new_search.search_ts = model_utils.now_ts() if account_id: new_search.account_id = account_id if 'clientReferenceId' in search_json and search_json[ 'clientReferenceId'].strip() != '': new_search.client_reference_id = search_json['clientReferenceId'] return new_search
def test_draft_json(session): """Assert that the draft renders to a json format correctly.""" json_data = copy.deepcopy(DRAFT_CHANGE_STATEMENT) draft = Draft( document_number='TEST1234', account_id='PS12345', create_ts=now_ts(), registration_type=json_data['changeStatement']['changeType'], registration_type_cl='CHANGE', draft=json_data, # json.dumps(json_data), registration_number=json_data['changeStatement'] ['baseRegistrationNumber']) draft_json = draft.json assert draft_json assert draft_json['type'] == 'CHANGE_STATEMENT'
def test_court_order_json(session): """Assert that the court order model renders to a json format correctly.""" court_order = CourtOrder(court_order_id=1000, court_name='name', court_registry='registry', file_number='file', effect_of_order='effect', court_date=now_ts()) court_json = { 'courtName': court_order.court_name, 'courtRegistry': court_order.court_registry, 'fileNumber': court_order.file_number, 'orderDate': format_ts(court_order.court_date), 'effectOfOrder': court_order.effect_of_order } assert court_order.json == court_json
def validate_query(json_data): """Perform any extra data validation here, either because it is too complicated for the schema. Or because it requires existing data. """ error_msg = '' # validate search type - criteria combinations search_type = json_data['type'] if search_type not in ('INDIVIDUAL_DEBTOR', 'BUSINESS_DEBTOR'): if 'value' not in json_data['criteria']: error_msg += f'Search criteria value is required for search type {search_type}. ' else: if 'debtorName' not in json_data['criteria']: error_msg += f'Search criteria debtorName is required for search type {search_type}. ' elif search_type == 'INDIVIDUAL_DEBTOR' and 'last' not in json_data[ 'criteria']['debtorName']: error_msg += f'Search criteria debtorName last is required for search type {search_type}. ' elif search_type == 'BUSINESS_DEBTOR' and 'business' not in json_data[ 'criteria']['debtorName']: error_msg += f'Search criteria debtorName businessName is required for search type {search_type}. ' # Verify the start and end dates. if 'startDateTime' in json_data or 'startDateTime' in json_data: now = model_utils.now_ts() ts_start = None ts_end = None if 'startDateTime' in json_data: ts_start = model_utils.ts_from_iso_format( json_data['startDateTime']) if ts_start > now: error_msg = error_msg + 'Search startDateTime invalid: it cannot be in the future. ' if 'endDateTime' in json_data: ts_end = model_utils.ts_from_iso_format( json_data['endDateTime']) if ts_end > now: error_msg = error_msg + 'Search endDateTime invalid: it cannot be in the future. ' if ts_start and ts_end and ts_start > ts_end: error_msg = error_msg + 'Search date range invalid: startDateTime cannot be after endDateTime. ' if error_msg != '': raise BusinessException(error=error_msg, status_code=HTTPStatus.BAD_REQUEST)
def create_financing_from_json(json_data, account_id: str = None, user_id: str = None): """Create a registraion object from dict/json.""" registration = Registration() registration.account_id = account_id registration.user_id = user_id registration.registration_ts = model_utils.now_ts() reg_type = json_data['type'] registration.registration_type_cl = model_utils.REG_TYPE_TO_REG_CLASS[reg_type] registration.registration_type = reg_type registration.ver_bypassed = 'Y' if reg_type == model_utils.REG_TYPE_REPAIRER_LIEN: if 'lienAmount' in json_data: registration.lien_value = json_data['lienAmount'].strip() if 'surrenderDate' in json_data: registration.surrender_date = model_utils.ts_from_date_iso_format(json_data['surrenderDate']) registration.life = model_utils.REPAIRER_LIEN_YEARS elif 'lifeInfinite' in json_data and json_data['lifeInfinite']: registration.life = model_utils.LIFE_INFINITE elif registration.registration_type_cl in (model_utils.REG_CLASS_CROWN, model_utils.REG_CLASS_MISC): registration.life = model_utils.LIFE_INFINITE elif reg_type in (model_utils.REG_TYPE_MARRIAGE_SEPARATION, model_utils.REG_TYPE_TAX_MH, model_utils.REG_TYPE_LAND_TAX_MH): registration.life = model_utils.LIFE_INFINITE elif 'lifeYears' in json_data: registration.life = json_data['lifeYears'] if 'clientReferenceId' in json_data: registration.client_reference_id = json_data['clientReferenceId'] # Create or update draft. draft = Registration.find_draft(json_data, registration.registration_type_cl, reg_type) reg_vals = Registration.get_generated_values(draft) registration.id = reg_vals.id registration.registration_num = reg_vals.registration_num if not draft: registration.document_number = reg_vals.document_number draft = Draft.create_from_registration(registration, json_data, user_id) registration.draft = draft return registration
def test_event_tracking_json(session): """Assert that the event tracking model renders to a json format correctly.""" now_ts = model_utils.now_ts() tracking = EventTracking( id=1000, key_id=1000, event_ts=now_ts, event_tracking_type=EventTracking.EventTrackingTypes.SEARCH_REPORT, status=200, message='message', email_id='*****@*****.**') tracking_json = { 'eventTrackingId': tracking.id, 'keyId': tracking.key_id, 'type': tracking.event_tracking_type, 'createDateTime': model_utils.format_ts(tracking.event_ts), 'status': tracking.status, 'message': tracking.message, 'emailAddress': tracking.email_id } assert tracking.json == tracking_json
def test_validate_rl(session, desc, valid, lien_amount, surrender_date, message_content): """Assert that financing statement RL registration type validation works as expected.""" # setup json_data = copy.deepcopy(FINANCING) json_data['type'] = model_utils.REG_TYPE_REPAIRER_LIEN del json_data['lifeYears'] del json_data['lifeInfinite'] del json_data['trustIndenture'] if lien_amount is not None: json_data['lienAmount'] = lien_amount if surrender_date == 'valid': json_data['surrenderDate'] = model_utils.format_ts( model_utils.now_ts()) elif surrender_date == 'old': json_data['surrenderDate'] = model_utils.format_ts( model_utils.today_ts_offset(22, False)) elif surrender_date == '21': json_data['surrenderDate'] = model_utils.format_ts( model_utils.now_ts_offset(21, False)) elif surrender_date == 'junk': json_data['surrenderDate'] = 'invalid date' if desc != DESC_INCLUDES_GC: del json_data['generalCollateral'] if desc == DESC_MISSING_VC: del json_data['vehicleCollateral'] elif desc == DESC_VC_MH: json_data['vehicleCollateral'][0]['type'] = 'MH' error_msg = validator.validate(json_data) if valid: assert error_msg == '' elif message_content: # print(error_msg) assert error_msg != '' assert error_msg.find(message_content) != -1
def create_from_json(json_data, registration_type_cl: str, financing_statement, base_registration_num: str, account_id: str = None): """Create a registration object for an existing financing statement from dict/json.""" # Perform all addtional data validation checks. Registration.validate(json_data, financing_statement, registration_type_cl) # Create or update draft. draft = Registration.find_draft(json_data, None, None) reg_vals = Registration.get_generated_values(draft) registration = Registration() registration.registration_id = reg_vals.registration_id registration.registration_num = reg_vals.registration_num registration.registration_ts = model_utils.now_ts() registration.financing_id = financing_statement.financing_id registration.financing_statement = financing_statement registration.account_id = account_id if not draft: registration.document_number = reg_vals.document_number draft = Draft.create_from_registration(registration, json_data) else: registration.document_number = draft.document_number registration.draft = draft registration.registration_type_cl = registration_type_cl if registration_type_cl in (model_utils.REG_CLASS_AMEND, model_utils.REG_CLASS_AMEND_COURT, model_utils.REG_CLASS_CHANGE): registration.registration_type_cd = json_data['changeType'] if registration.registration_type_cd == model_utils.REG_TYPE_AMEND_COURT: registration.registration_type_cl = model_utils.REG_CLASS_AMEND_COURT if 'description' in json_data: registration.detail_description = json_data['description'] if registration_type_cl == model_utils.REG_CLASS_RENEWAL: registration.registration_type_cd = model_utils.REG_TYPE_RENEWAL elif registration_type_cl == model_utils.REG_CLASS_DISCHARGE: registration.registration_type_cd = model_utils.REG_TYPE_DISCHARGE registration.base_registration_num = base_registration_num registration.ver_bypassed = 'Y' registration.draft.registration_type_cd = registration.registration_type_cd registration.draft.registration_type_cl = registration.registration_type_cl if 'clientReferenceId' in json_data: registration.client_reference_id = json_data['clientReferenceId'] # All registrations have at least one party (registering). registration.parties = Party.create_from_statement_json(json_data, registration_type_cl, registration.financing_id) # If get to here all data should be valid: get reg id to close out updated entities. registration_id = registration.registration_id financing_reg_type = registration.financing_statement.registration[0].registration_type_cd if registration_type_cl == model_utils.REG_CLASS_DISCHARGE: registration.financing_statement.state_type_cd = model_utils.STATE_DISCHARGED registration.financing_statement.discharged = 'Y' elif registration_type_cl == model_utils.REG_CLASS_RENEWAL: if financing_reg_type == model_utils.REG_TYPE_REPAIRER_LIEN: registration.life = model_utils.REPAIRER_LIEN_YEARS registration.financing_statement.expire_date = \ model_utils.now_ts_offset(model_utils.REPAIRER_LIEN_DAYS, True) else: if 'lifeYears' in json_data: registration.life = json_data['lifeYears'] registration.financing_statement.expire_date = model_utils.expiry_dt_from_years(registration.life) elif 'expiryDate' in json_data: new_expiry_date = model_utils.expiry_ts_from_iso_format(json_data['expiryDate']) registration.life = new_expiry_date.year - registration.financing_statement.expire_date.year registration.financing_statement.expire_date = new_expiry_date # TODO: verify this is updated. registration.financing_statement.life = registration.life # Repairer's lien renewal or amendment can have court order information. if (registration.registration_type_cd == model_utils.REG_TYPE_AMEND_COURT or registration.registration_type_cd == model_utils.REG_TYPE_RENEWAL) and \ 'courtOrderInformation' in json_data: registration.court_order = CourtOrder.create_from_json(json_data['courtOrderInformation'], registration_id) if registration_type_cl in (model_utils.REG_CLASS_AMEND, model_utils.REG_CLASS_AMEND_COURT, model_utils.REG_CLASS_CHANGE): # Possibly add vehicle collateral registration.vehicle_collateral = VehicleCollateral.create_from_statement_json(json_data, registration_id, registration.financing_id) # Possibly add general collateral registration.general_collateral = GeneralCollateral.create_from_statement_json(json_data, registration_id, registration.financing_id) # Close out deleted parties and collateral Registration.delete_from_json(json_data, registration, financing_statement) return registration
def create_from_json(json_data, registration_type_cl: str, financing_statement, base_registration_num: str, account_id: str = None): """Create a registration object for an existing financing statement from dict/json.""" # Create or update draft. draft = Registration.find_draft(json_data, None, None) reg_vals = Registration.get_generated_values(draft) registration = Registration() registration.id = reg_vals.id # pylint: disable=invalid-name; allow name of id. registration.registration_num = reg_vals.registration_num registration.registration_ts = model_utils.now_ts() registration.financing_id = financing_statement.id registration.financing_statement = financing_statement registration.account_id = account_id if not draft: registration.document_number = reg_vals.document_number draft = Draft.create_from_registration(registration, json_data) registration.draft = draft registration.registration_type_cl = registration_type_cl if registration_type_cl in (model_utils.REG_CLASS_AMEND, model_utils.REG_CLASS_AMEND_COURT): json_data = model_utils.cleanup_amendment(json_data) registration.registration_type = model_utils.amendment_change_type(json_data) if registration.registration_type == model_utils.REG_TYPE_AMEND_COURT: registration.registration_type_cl = model_utils.REG_CLASS_AMEND_COURT if 'description' in json_data: registration.detail_description = json_data['description'] elif registration_type_cl == model_utils.REG_CLASS_CHANGE: registration.registration_type = json_data['changeType'] elif registration_type_cl == model_utils.REG_CLASS_RENEWAL: registration.registration_type = model_utils.REG_TYPE_RENEWAL elif registration_type_cl == model_utils.REG_CLASS_DISCHARGE: registration.registration_type = model_utils.REG_TYPE_DISCHARGE registration.base_registration_num = base_registration_num registration.ver_bypassed = 'Y' registration.draft.registration_type = registration.registration_type registration.draft.registration_type_cl = registration.registration_type_cl if 'clientReferenceId' in json_data: registration.client_reference_id = json_data['clientReferenceId'] # All registrations have at least one party (registering). registration.parties = Party.create_from_statement_json(json_data, registration_type_cl, registration.financing_id) # If get to here all data should be valid: get reg id to close out updated entities. registration_id = registration.id financing_reg_type = registration.financing_statement.registration[0].registration_type if registration_type_cl == model_utils.REG_CLASS_DISCHARGE: registration.financing_statement.state_type = model_utils.STATE_DISCHARGED registration.financing_statement.discharged = 'Y' elif registration_type_cl == model_utils.REG_CLASS_RENEWAL: if financing_reg_type == model_utils.REG_TYPE_REPAIRER_LIEN: registration.life = model_utils.REPAIRER_LIEN_YEARS # Adding 180 days to existing expiry. registration.financing_statement.expire_date = \ model_utils.expiry_dt_repairer_lien(registration.financing_statement.expire_date) else: if 'lifeInfinite' in json_data and json_data['lifeInfinite']: registration.life = model_utils.LIFE_INFINITE registration.financing_statement.expire_date = None if 'lifeYears' in json_data: registration.life = json_data['lifeYears'] # registration.financing_statement.expire_date = model_utils.expiry_dt_from_years(registration.life) # Replace above line with below: adding years to the existing expiry registration.financing_statement.expire_date = \ model_utils.expiry_dt_add_years(registration.financing_statement.expire_date, registration.life) elif 'expiryDate' in json_data: new_expiry_date = model_utils.expiry_ts_from_iso_format(json_data['expiryDate']) registration.life = new_expiry_date.year - registration.financing_statement.expire_date.year registration.financing_statement.expire_date = new_expiry_date # Verify this is updated. if 'lifeInfinite' in json_data and json_data['lifeInfinite']: registration.financing_statement.life = registration.life else: registration.financing_statement.life += registration.life # Repairer's lien renewal or amendment can have court order information. if (registration.registration_type == model_utils.REG_TYPE_AMEND_COURT or registration.registration_type == model_utils.REG_TYPE_RENEWAL) and \ 'courtOrderInformation' in json_data: registration.court_order = CourtOrder.create_from_json(json_data['courtOrderInformation'], registration_id) if registration_type_cl in (model_utils.REG_CLASS_AMEND, model_utils.REG_CLASS_AMEND_COURT, model_utils.REG_CLASS_CHANGE): # Possibly add vehicle collateral registration.vehicle_collateral = VehicleCollateral.create_from_statement_json(json_data, registration_id, registration.financing_id) # Possibly add general collateral registration.general_collateral = GeneralCollateral.create_from_statement_json(json_data, registration_id, registration.financing_id) # Possibly add/remove a trust indenture if ('addTrustIndenture' in json_data and json_data['addTrustIndenture']) or \ ('removeTrustIndenture' in json_data and json_data['removeTrustIndenture']): registration.trust_indenture = TrustIndenture.create_from_amendment_json(registration.financing_id, registration.id) if 'removeTrustIndenture' in json_data and json_data['removeTrustIndenture']: registration.trust_indenture.trust_indenture = TrustIndenture.TRUST_INDENTURE_NO # Close out deleted parties and collateral, and trust indenture. Registration.delete_from_json(json_data, registration, financing_statement) return registration