Utility app - General Utilities for All Apps
General utility functions.
- utility.bytes_to_mb(b: int) float
Convert bytes to MB.
- Parameters:
b (int) – Size in bytes.
- Returns:
Size in MB.
- Return type:
float
- utility.count_file_types(file_names: list, accepted_file_groups: dict[str, List[str]]) dict
Tabulate how many files fall into the file groups specified in the ACCEPTED_FILE_FORMATS dictionary.
If a file’s extension does not match any of the accepted file extensions, it is ignored. For that reason, it is important to ensure that the files are accepted before trying to count them.
- Parameters:
file_names (list) – A list of file paths or names with extension intact
accepted_file_groups (dict) – A dictionary of file group names mapping to a list of lowercase file extensions without periods.
- Returns:
A dictionary mapping from group name to number of files in that group.
- Return type:
(dict)
- utility.get_client_ip_address(request: HttpRequest) str
Get the client’s IP address from the request.
- utility.get_human_readable_file_count(file_names: list, accepted_file_groups: dict) str
Count the number of files falling into the accepted file groups, and report the number of files in each group.
- Parameters:
file_names (list) – A list of file paths or names with extension intact
accepted_file_groups (dict) – A dictionary of file group names mapping to a list of lowercase file extensions without periods.
- Returns:
A string reporting the number of files in each group.
- Return type:
(str)
- utility.get_human_readable_size(size_bytes: float, base: int = 1024, precision: int = 2) str
Convert bytes into a human-readable size.
- Parameters:
size_bytes – The number of bytes to convert
base – Either of 1024 or 1000. 1024 for sizes like MiB, 1000 for sizes like MB
precision – The number of decimals on the returned size
- Returns:
The bytes converted to a human readable size
- Return type:
(str)
- utility.get_js_translation_version() str
Return the latest modification time of all djangojs.mo files in the locale directory.
This changes whenever compiled JS translations are updated.
- utility.html_to_text(html: str) str
Convert HTML content to plain text by stripping tags and whitespace.
- utility.is_deployed_environment() bool
Detect if the app is running in a deployed production environment.
Returns True if ALLOWED_HOSTS contains any non-localhost/non-127.0.0.1 hosts, indicating this is a deployed production environment.
- utility.mb_to_bytes(m: int) int
Convert MB to bytes.
- Parameters:
m (int) – Size in MB.
- Returns:
Size in bytes.
- Return type:
int
- utility.zip_directory(directory: str, zipf: ZipFile) None
Zip a directory structure into a zip file.
- Parameters:
directory (str) – The folder to zip
zipf (ZipFile) – A zipfile.ZipFile handle