Skip to content

Commit

Permalink
fix double string linting
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonSharratt committed Jul 19, 2024
1 parent 1695dd3 commit 4212768
Show file tree
Hide file tree
Showing 6 changed files with 507 additions and 407 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ repos:
- id: fix-byte-order-marker
- id: trailing-whitespace
- id: end-of-file-fixer
- id: double-quote-string-fixer
154 changes: 77 additions & 77 deletions btr-api/src/btr_api/resources/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,39 @@
from flask import request
from flask_cors import cross_origin

bp = Blueprint("submission", __name__)
bp = Blueprint('submission', __name__)

REDACT_MONONYM = "mono"
REDACT_MONONYM_FN = "mono_fn"
REDACT_EMAIL = "mono_email"
REDACT_PHONE = "mono_phone"
REDACT_FULL = "full"
REDACT_EMPTY = "empty"
REDACT_DATE = "date"
REDACT_IDENTIFIER = "identifier"
REDACT_MONONYM = 'mono'
REDACT_MONONYM_FN = 'mono_fn'
REDACT_EMAIL = 'mono_email'
REDACT_PHONE = 'mono_phone'
REDACT_FULL = 'full'
REDACT_EMPTY = 'empty'
REDACT_DATE = 'date'
REDACT_IDENTIFIER = 'identifier'

# the lack of a rule means it shows fully
REDACT_RULES = {
btr_auth.USER_PUBLIC: {
"prefName": REDACT_EMPTY,
"email": REDACT_EMPTY,
"phone": REDACT_EMPTY,
"postal": REDACT_EMPTY,
"street": REDACT_EMPTY,
"streetAdditional": REDACT_EMPTY,
"locationDescription": REDACT_EMPTY,
"birthDate": REDACT_EMPTY,
"identifiers": REDACT_EMPTY,
'prefName': REDACT_EMPTY,
'email': REDACT_EMPTY,
'phone': REDACT_EMPTY,
'postal': REDACT_EMPTY,
'street': REDACT_EMPTY,
'streetAdditional': REDACT_EMPTY,
'locationDescription': REDACT_EMPTY,
'birthDate': REDACT_EMPTY,
'identifiers': REDACT_EMPTY,
},
btr_auth.USER_STAFF: {
"email": REDACT_EMAIL,
"phone": REDACT_PHONE,
"postal": REDACT_EMPTY,
"street": REDACT_EMPTY,
"streetAdditional": REDACT_EMPTY,
"locationDescription": REDACT_EMPTY,
"birthDate": REDACT_DATE,
"identifiers": REDACT_IDENTIFIER,
'email': REDACT_EMAIL,
'phone': REDACT_PHONE,
'postal': REDACT_EMPTY,
'street': REDACT_EMPTY,
'streetAdditional': REDACT_EMPTY,
'locationDescription': REDACT_EMPTY,
'birthDate': REDACT_DATE,
'identifiers': REDACT_IDENTIFIER,
},
}

Expand All @@ -105,69 +105,69 @@ def redactInformation(request, payload):
return payload
# otherwise PUBLIC or STAFF
redactionToUse = REDACT_RULES[role]
for person in payload["payload"]["personStatements"]:
for name in person["names"]:
if name["type"] == "alternative":
name["fullName"] = redactField(name["fullName"], redactionToUse.get("prefName"))

person["email"] = redactField(person["email"], redactionToUse.get("email"))
person["phoneNumber"]["number"] = redactField(person["phoneNumber"]["number"], redactionToUse.get("phone"))
for address in person["addresses"]:
address["postalCode"] = redactField(address["postalCode"], redactionToUse.get("postal"))
address["street"] = redactField(address["street"], redactionToUse.get("street"))
address["streetAdditional"] = redactField(
address["streetAdditional"], redactionToUse.get("streetAdditional")
for person in payload['payload']['personStatements']:
for name in person['names']:
if name['type'] == 'alternative':
name['fullName'] = redactField(name['fullName'], redactionToUse.get('prefName'))

person['email'] = redactField(person['email'], redactionToUse.get('email'))
person['phoneNumber']['number'] = redactField(person['phoneNumber']['number'], redactionToUse.get('phone'))
for address in person['addresses']:
address['postalCode'] = redactField(address['postalCode'], redactionToUse.get('postal'))
address['street'] = redactField(address['street'], redactionToUse.get('street'))
address['streetAdditional'] = redactField(
address['streetAdditional'], redactionToUse.get('streetAdditional')
)
address["locationDescription"] = redactField(
address["locationDescription"], redactionToUse.get("locationDescription")
address['locationDescription'] = redactField(
address['locationDescription'], redactionToUse.get('locationDescription')
)

person["placeOfResidence"]["postalCode"] = redactField(
person["placeOfResidence"]["postalCode"], redactionToUse.get("postal")
person['placeOfResidence']['postalCode'] = redactField(
person['placeOfResidence']['postalCode'], redactionToUse.get('postal')
)
person["placeOfResidence"]["street"] = redactField(
person["placeOfResidence"]["street"], redactionToUse.get("street")
person['placeOfResidence']['street'] = redactField(
person['placeOfResidence']['street'], redactionToUse.get('street')
)
person["placeOfResidence"]["streetAdditional"] = redactField(
person["placeOfResidence"]["streetAdditional"], redactionToUse.get("streetAdditional")
person['placeOfResidence']['streetAdditional'] = redactField(
person['placeOfResidence']['streetAdditional'], redactionToUse.get('streetAdditional')
)
person["placeOfResidence"]["locationDescription"] = redactField(
person["placeOfResidence"]["locationDescription"], redactionToUse.get("locationDescription")
person['placeOfResidence']['locationDescription'] = redactField(
person['placeOfResidence']['locationDescription'], redactionToUse.get('locationDescription')
)
person["birthDate"] = redactField(person["birthDate"], redactionToUse.get("birthDate"))
for identifier in person["identifiers"]:
identifier["id"] = redactField(identifier["id"], redactionToUse.get("identifiers"))
person['birthDate'] = redactField(person['birthDate'], redactionToUse.get('birthDate'))
for identifier in person['identifiers']:
identifier['id'] = redactField(identifier['id'], redactionToUse.get('identifiers'))
return jsonify(payload)


def redactField(field, redactType):
if redactType == REDACT_MONONYM:
return field[0:1] + "***"
return field[0:1] + '***'
elif redactType == REDACT_MONONYM_FN:
rv = ""
rv = ''
for word in field.split():
if not (rv == ""):
rv += " "
rv += word[0:1] + "***"
if not (rv == ''):
rv += ' '
rv += word[0:1] + '***'
return rv
elif redactType == REDACT_EMAIL:
return field.split("@")[0][0:1] + "***" + "@***." + field.split(".")[-1]
return field.split('@')[0][0:1] + '***' + '@***.' + field.split('.')[-1]
elif redactType == REDACT_PHONE:
return field[0:-7] + "***"
return field[0:-7] + '***'
elif redactType == REDACT_FULL:
return "***"
return '***'
# This is a space instead of blank because if it's empty the ui shows undefined in some spots
elif redactType == REDACT_EMPTY:
return " "
return ' '
elif redactType == REDACT_DATE:
return field.split("-")[0]
return field.split('-')[0]
elif redactType == REDACT_IDENTIFIER:
return "*** **" + field[6:]
return '*** **' + field[6:]

return field


@bp.route("/<id>", methods=("GET",))
@bp.route('/<id>', methods=('GET',))
def registers(id: int | None = None): # pylint: disable=redefined-builtin
"""Get the submissions, or specific submission by id."""
try:
Expand All @@ -183,13 +183,13 @@ def registers(id: int | None = None): # pylint: disable=redefined-builtin
return exception_response(exception)


@bp.route("/entity/<business_identifier>", methods=("GET",))
@bp.route('/entity/<business_identifier>', methods=('GET',))
def get_entity_submission(business_identifier: str):
"""Get the current submission for specified business identifier."""

try:
btr_auth.is_authorized(request=request, business_identifier=business_identifier)
account_id = request.args.get("account_id")
account_id = request.args.get('account_id')
btr_auth.product_authorizations(request=request, account_id=account_id)

submission = SubmissionModel.find_by_business_identifier(business_identifier)
Expand All @@ -204,8 +204,8 @@ def get_entity_submission(business_identifier: str):
return exception_response(exception)


@bp.route("/", methods=("POST",))
@cross_origin(origin="*")
@bp.route('/', methods=('POST',))
@cross_origin(origin='*')
@jwt.requires_auth
def create_register():
"""
Expand All @@ -216,27 +216,27 @@ def create_register():
"""
try:
user = UserModel.get_or_create_user_by_jwt(g.jwt_oidc_token_info)
account_id = request.headers.get("Account-Id", None)
account_id = request.headers.get('Account-Id', None)

json_input = request.get_json()

# validate payload; TODO: implement business rules validations
schema_name = "btr-filing.schema.json"
schema_name = 'btr-filing.schema.json'
schema_service = SchemaService()
[valid, errors] = schema_service.validate(schema_name, json_input)
if not valid:
return error_request_response("Invalid schema", HTTPStatus.BAD_REQUEST, errors)
return error_request_response('Invalid schema', HTTPStatus.BAD_REQUEST, errors)

business_identifier = json_input.get("businessIdentifier")
business_identifier = json_input.get('businessIdentifier')
btr_auth.is_authorized(request=request, business_identifier=business_identifier)

# get entity
identifier = json_input["businessIdentifier"]
identifier = json_input['businessIdentifier']
entity: dict = btr_entity.get_entity_info(jwt, identifier).json()

# validate entity; return FORBIDDEN for historial and frozen companies
if entity_errors := validate_entity(entity):
return error_request_response("Invalid entity", HTTPStatus.FORBIDDEN, entity_errors)
return error_request_response('Invalid entity', HTTPStatus.FORBIDDEN, entity_errors)

# create submission
submission = SubmissionService.create_submission(json_input, user.id)
Expand All @@ -246,11 +246,11 @@ def create_register():
# NOTE: this will be moved out of this api once lear filings are linked
# create invoice in pay system
invoice_resp = btr_pay.create_invoice(account_id, jwt, json_input)
submission.invoice_id = invoice_resp.json()["id"]
submission.invoice_id = invoice_resp.json()['id']
except ExternalServiceException as err:
# Log error and continue to return successfully (does NOT block the submission)
current_app.logger.info(err.error)
current_app.logger.error("Error creating invoice for submission: %s", submission.id)
current_app.logger.error('Error creating invoice for submission: %s', submission.id)

submission.save()
try:
Expand All @@ -260,13 +260,13 @@ def create_register():
entity_addresses: dict[str, dict[str, dict]] = btr_entity.get_entity_info(
jwt, f"{identifier}/addresses"
).json()
entity["business"]["addresses"] = [entity_addresses.get("registeredOffice", {}).get("deliveryAddress")]
entity['business']['addresses'] = [entity_addresses.get('registeredOffice', {}).get('deliveryAddress')]
btr_bor.update_owners(submission, entity, token)

except ExternalServiceException as err:
# Log error and continue to return successfully (does NOT block the submission)
current_app.logger.info(err.error)
current_app.logger.error("Error updating record in BOR for submission: %s", submission.id)
current_app.logger.error('Error updating record in BOR for submission: %s', submission.id)

return jsonify(id=submission.id), HTTPStatus.CREATED

Expand Down
Loading

0 comments on commit 4212768

Please sign in to comment.