generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added filing validators class, rearranged code (#530)
Closes #529
- Loading branch information
Showing
13 changed files
with
232 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pkgutil | ||
import importlib | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class ActionValidator(ABC): | ||
""" | ||
Abstract Callable class for action validations, __subclasses__ method leveraged to construct a registry | ||
""" | ||
|
||
name: str | ||
|
||
def __init__(self, name: str): | ||
super().__init__() | ||
self.name = name | ||
|
||
@abstractmethod | ||
def __call__(self, *args, **kwargs): ... | ||
|
||
|
||
_validation_registry = None | ||
|
||
|
||
def get_validation_registry(): | ||
# use package reflection to import all subclasses of the base ActionValidator under the current | ||
# package so that __subclasses__ can find loaded subs. Do this once to keep subsequent requests | ||
# from being impacted in performance. | ||
global _validation_registry | ||
if not _validation_registry: | ||
package = __package__ | ||
p = importlib.import_module(package) | ||
for _, module_name, is_pkg in pkgutil.iter_modules(p.__path__): | ||
if module_name not in __name__: | ||
importlib.import_module(f"{package}.{module_name}") | ||
_validation_registry = { | ||
validator.name: validator for validator in {Validator() for Validator in ActionValidator.__subclasses__()} | ||
} | ||
|
||
return _validation_registry |
42 changes: 42 additions & 0 deletions
42
src/sbl_filing_api/services/validators/filing_validators.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import logging | ||
|
||
from sbl_filing_api.entities.models.dao import FilingDAO | ||
from .base_validator import ActionValidator | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class ValidNoFilingExists(ActionValidator): | ||
def __init__(self): | ||
super().__init__("valid_no_filing_exists") | ||
|
||
def __call__(self, filing: FilingDAO, period_code: str, lei: str, **kwargs): | ||
if filing: | ||
return f"Filing already exists for Filing Period {period_code} and LEI {lei}" | ||
|
||
|
||
class ValidFilingExists(ActionValidator): | ||
def __init__(self): | ||
super().__init__("valid_filing_exists") | ||
|
||
def __call__(self, filing: FilingDAO, lei: str, period_code: str, **kwargs): | ||
if not filing: | ||
return f"There is no Filing for LEI {lei} in period {period_code}, unable to sign a non-existent Filing." | ||
|
||
|
||
class ValidVoluntaryFiler(ActionValidator): | ||
def __init__(self): | ||
super().__init__("valid_voluntary_filer") | ||
|
||
def __call__(self, filing: FilingDAO, **kwargs): | ||
if filing and filing.is_voluntary is None: | ||
return f"Cannot sign filing. Filing for {filing.lei} for period {filing.filing_period} does not have a selection of is_voluntary defined." | ||
|
||
|
||
class ValidContactInfo(ActionValidator): | ||
def __init__(self): | ||
super().__init__("valid_contact_info") | ||
|
||
def __call__(self, filing: FilingDAO, **kwargs): | ||
if filing and not filing.contact_info: | ||
return f"Cannot sign filing. Filing for {filing.lei} for period {filing.filing_period} does not have contact info defined." |
31 changes: 31 additions & 0 deletions
31
src/sbl_filing_api/services/validators/institution_validators.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
import logging | ||
|
||
from typing import Any, Dict | ||
|
||
from .base_validator import ActionValidator | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class ValidLeiStatus(ActionValidator): | ||
def __init__(self): | ||
super().__init__("valid_lei_status") | ||
|
||
def __call__(self, institution: Dict[str, Any], **kwargs): | ||
try: | ||
is_active = institution["lei_status"]["can_file"] | ||
if not is_active: | ||
return f"Cannot sign filing. LEI status of {institution['lei_status_code']} cannot file." | ||
except Exception: | ||
log.exception("Unable to determine lei status: %s", json.dumps(institution)) | ||
return "Unable to determine LEI status." | ||
|
||
|
||
class ValidLeiTin(ActionValidator): | ||
def __init__(self): | ||
super().__init__("valid_lei_tin") | ||
|
||
def __call__(self, institution: Dict[str, Any], **kwargs): | ||
if not (institution and institution.get("tax_id")): | ||
return "Cannot sign filing. TIN is required to file." |
Oops, something went wrong.