app.utilities.helpers

Helper utility functions for the Mail List Shield application.

This module provides general-purpose helper functions including file size formatting, code generation, and API key generation.

Functions

readable_file_size(size_in_bytes[,Β significant_digits])

Convert a file size in bytes to a human-readable string.

generate_n_digit_code(n)

Generate a random n-digit numeric code.

generate_api_key_and_hash()

Generate a new API key and its bcrypt hash.

Module Contents

app.utilities.helpers.readable_file_size(size_in_bytes, significant_digits=0)[source]

Convert a file size in bytes to a human-readable string.

Parameters:
  • size_in_bytes – The size in bytes to convert.

  • significant_digits – Number of decimal places to include.

Returns:

Human-readable size string (e.g., β€˜5 MB’, β€˜1.5 GB’).

Return type:

str

app.utilities.helpers.generate_n_digit_code(n)[source]

Generate a random n-digit numeric code.

Each digit is between 1-9 (no zeros) for better readability.

Parameters:

n – The number of digits in the code.

Returns:

A string of n random digits.

Return type:

str

app.utilities.helpers.generate_api_key_and_hash()[source]

Generate a new API key and its bcrypt hash.

Creates a 64-character random API key and hashes it for secure storage.

Returns:

A tuple containing (plaintext_key, hashed_key).

The plaintext key should be shown to the user once, while the hash is stored in the database.

Return type:

tuple