Utilities

app.db.rollback_on_error(session: Session) Generator[None, None, None][source]

Call session.rollback() and re-raise the exception.

app.db.handle_skipped_award(session: Session, msg: str) Generator[None, None, None][source]

Call session.rollback() and, if the exception is SkippedAwardError, commit an EventLog entry. Otherwise, re-raise the exception.

app.db.get_db() Generator[Session, None, None][source]

Get a SQLAlchemy session.

class app.util.Tags(value)[source]
authentication = 'authentication'
applications = 'applications'
lenders = 'lenders'
meta = 'meta'
statistics = 'statistics'
users = 'users'
class app.util.SortOrder(value)[source]
ASC = 'asc'
DESC = 'desc'
class app.util.StatisticRange(value)[source]
CUSTOM_RANGE = 'CUSTOM_RANGE'
LAST_WEEK = 'LAST_WEEK'
LAST_MONTH = 'LAST_MONTH'
app.util.loads(response: Response) Any[source]
app.util.get_object_or_404(session: Session, model: type[T], field: str, value: Any) T[source]
app.util.generate_uuid(string: str) str[source]

Generate a UUID based on the given string.

Parameters:

string – The input string to generate the UUID from.

Returns:

The generated UUID.

app.util.get_secret_hash(string: str) str[source]

Calculate the hash of a string.

app.util.is_valid_email(email: str) bool[source]

Check if the given email is valid.

Parameters:

email – The email address to validate.

Returns:

True if the email is valid, False otherwise.

app.util.validate_file(file: UploadFile = File(PydanticUndefined)) tuple[bytes, str | None][source]

Validate the uploaded file.

This function checks whether the file has an allowed format and whether its size is below the maximum allowed size.

If the file does not pass these checks, raise an HTTPException. Otherwise, return the file and its filename.

Parameters:

file – The uploaded file.

Returns:

A dictionary mapping the file to its filename.

Raises:

HTTPException – If the file format is not allowed or if the file size is too large.

app.util.get_modified_data_fields(session: Session, application: Application) ApplicationWithRelations[source]
app.util.create_award_from_data_source(session: Session, entry: dict[str, Any], borrower_id: int | None = None, *, previous: bool = False) Award[source]

Create a new award and insert it into the database.

Parameters:
  • entry – The dictionary containing the award data.

  • borrower_id – The ID of the borrower associated with the award. (default: None)

  • previous – Whether the award is a previous award or not. (default: False)

Returns:

The inserted award.

app.util.get_previous_awards_from_data_source(borrower_id: int, db_provider: ~collections.abc.Callable[[], ~collections.abc.Generator[~sqlalchemy.orm.session.Session, None, None]] = <function get_db>) None[source]

Fetch previous awards for a borrower that accepted an application.

This won’t generate an application; it will only insert the awards into the database.

Parameters:

borrower_id – The ID of the borrower for whom to fetch and process previous awards.

app.util.create_or_update_borrower_document(session: Session, filename: str | None, application: Application, type: BorrowerDocumentType, file: bytes, *, verified: bool | None = False) BorrowerDocument[source]

Create a new borrower document or update an existing one.

This function first checks if a document of the same type already exists for the application in the session. If it does, it updates the existing document’s file, name, verified status, and submission time with the provided values. If it doesn’t, it creates a new BorrowerDocument with the provided values and adds it to the session.

Parameters:
  • filename – The name of the file to be added or updated.

  • application – The application associated with the document.

  • type – The type of the document.

  • file – The file to be added or updated.

  • verified – The verified status of the document. Defaults to False.

Returns:

The newly created or updated BorrowerDocument.

app.util.handle_external_onboarding(session: Session, application: Application, *, forward: bool = False) RedirectResponse[source]