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

Ran linting, updated to have a single /cleanup endpoint #39

Merged
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
83 changes: 80 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ regtech-api-commons = {git = "https://github.com/cfpb/regtech-api-commons.git"}
sbl-filing-api = {git = "https://github.com/cfpb/sbl-filing-api.git"}
regtech-user-fi-management = {git = "https://github.com/cfpb/regtech-user-fi-management.git"}
boto3 = "^1.34.149"
black = "^24.8.0"

[tool.black]
line-length = 120

[tool.ruff]
line-length = 120


[tool.poetry.group.dev.dependencies]
Expand Down
4 changes: 1 addition & 3 deletions src/regtech_cleanup_api/entities/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ async def get_filing_session():
echo=institution_settings.db_logging,
poolclass=NullPool,
).execution_options(schema_translate_map={None: institution_settings.inst_db_schema})
InstitutionSessionLocal = sessionmaker(
autocommit=False, autoflush=False, bind=user_fi_engine
)
InstitutionSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=user_fi_engine)


filing_engine = create_engine(
Expand Down
8 changes: 2 additions & 6 deletions src/regtech_cleanup_api/entities/repos/filing_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def get_user_action_ids(
filing_user_action_id = []
if not just_submissions:
filing = submission_repo.get_filing(session, lei, period_code)
filing_user_action_id = (
[filing.creator_id] if filing.creator_id else []
)
filing_user_action_id = [filing.creator_id] if filing.creator_id else []
submissions = submission_repo.get_submissions(session, lei, period_code)
user_action_ids = list(
set(
Expand Down Expand Up @@ -82,7 +80,5 @@ def delete_contact_info(session: Session, lei: str = None, period_code: str = No


def delete_helper(session: Session, table_obj: T, table_id: Any):
session.query(table_obj).filter(table_obj.id == table_id).delete(
synchronize_session="fetch"
)
session.query(table_obj).filter(table_obj.id == table_id).delete(synchronize_session="fetch")
session.commit()
16 changes: 4 additions & 12 deletions src/regtech_cleanup_api/entities/repos/institution_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@
from sqlalchemy import text


def delete_domains_by_lei(
session: Session, lei: str
) -> List[FinancialInstitutionDomainDao] | None:
session.query(FinancialInstitutionDomainDao).filter(
FinancialInstitutionDomainDao.lei == lei
).delete()
def delete_domains_by_lei(session: Session, lei: str) -> List[FinancialInstitutionDomainDao] | None:
session.query(FinancialInstitutionDomainDao).filter(FinancialInstitutionDomainDao.lei == lei).delete()
session.commit()
return {"OK": True}


def delete_sbl_type_by_lei(
session: Session, lei: str
) -> List[SblTypeMappingDao] | None:
def delete_sbl_type_by_lei(session: Session, lei: str) -> List[SblTypeMappingDao] | None:
session.query(SblTypeMappingDao).filter(SblTypeMappingDao.lei == lei).delete()
# deleting from history tables
del_hist_stmt = text("DELETE from fi_to_type_mapping_history where fi_id = :fi_id")
Expand All @@ -30,9 +24,7 @@ def delete_sbl_type_by_lei(


def delete_institution(session: Session, lei: str) -> FinancialInstitutionDao | None:
session.query(FinancialInstitutionDao).filter(
FinancialInstitutionDao.lei == lei
).delete()
session.query(FinancialInstitutionDao).filter(FinancialInstitutionDao.lei == lei).delete()

# deleting from history tables
del_hist_stmt = text("DELETE from financial_institutions_history where lei = :lei")
Expand Down
8 changes: 5 additions & 3 deletions src/regtech_cleanup_api/entities/repos/submission_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
T = TypeVar("T")


def get_filings(session: Session, lei: str) -> List[FilingDAO]:
return query_helper(session, FilingDAO, lei=lei)


def get_filing(session: Session, lei: str, filing_period: str) -> FilingDAO:
result = query_helper(session, FilingDAO, lei=lei, filing_period=filing_period)
return result[0] if result else None
Expand All @@ -25,9 +29,7 @@ def query_helper(session: Session, table_obj: T, **filter_args) -> List[T]:
return session.query(table_obj).all()


def get_submissions(
session: Session, lei: str = None, filing_period: str = None
) -> List[SubmissionDAO]:
def get_submissions(session: Session, lei: str = None, filing_period: str = None) -> List[SubmissionDAO]:
filing_id = None
if lei and filing_period:
filing = get_filing(session, lei=lei, filing_period=filing_period)
Expand Down
6 changes: 2 additions & 4 deletions src/regtech_cleanup_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
)
import uvicorn

from regtech_cleanup_api.routers.filing_cleanup import router as filing_router
from regtech_cleanup_api.routers.institution_cleanup import router as institution_router
from regtech_cleanup_api.routers.cleanup import router as cleanup_router
from regtech_cleanup_api.config import kc_settings

log = logging.getLogger()
Expand Down Expand Up @@ -50,8 +49,7 @@
)


app.include_router(institution_router, prefix="/v1/institution_cleanup")
app.include_router(filing_router, prefix="/v1/filing_cleanup")
app.include_router(cleanup_router, prefix="/v1/cleanup")


if __name__ == "__main__":
Expand Down
67 changes: 67 additions & 0 deletions src/regtech_cleanup_api/routers/cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging

from concurrent.futures import ProcessPoolExecutor
from http import HTTPStatus

from fastapi import Depends, Request, Response, status
from regtech_api_commons.api.exceptions import RegTechHttpException
from regtech_api_commons.api.router_wrapper import Router
from typing import Annotated

from regtech_cleanup_api.entities.engine.engine import (
get_filing_session,
get_institution_session,
)

from sqlalchemy.orm import Session

from regtech_api_commons.api.dependencies import verify_user_lei_relation

import regtech_cleanup_api.entities.repos.submission_repo as submission_repo

from regtech_cleanup_api.routers.institution_cleanup import (
delete_helper as institution_delete_helper,
)
from regtech_cleanup_api.routers.filing_cleanup import (
delete_helper as filing_delete_helper,
)

from regtech_cleanup_api.services.validation import is_valid_cleanup_lei

logger = logging.getLogger(__name__)


def set_institution_db(request: Request, session: Session = Depends(get_institution_session)):
request.state.institution_db_session = session


def set_filing_db(request: Request, session: Annotated[Session, Depends(get_filing_session)]):
request.state.filing_db_session = session


executor = ProcessPoolExecutor()
router = Router(
dependencies=[
Depends(set_filing_db),
Depends(set_institution_db),
Depends(verify_user_lei_relation),
]
)


@router.delete("/{lei}")
def delete_all_things(request: Request, lei: str):
if not is_valid_cleanup_lei(lei):
raise RegTechHttpException(
HTTPStatus.NOT_ACCEPTABLE,
name="Not Test LEI",
detail=f"{lei} not valid test lei.",
)
else:
institution_delete_helper(lei, request.state.institution_db_session)

filings = submission_repo.get_filings(request.state.filing_db_session, lei)
for f in filings:
filing_delete_helper(f.lei, f.filing_period, request.state.filing_db_session)

return Response(status_code=status.HTTP_204_NO_CONTENT)
Loading