recordtransfer.utils - Miscellaneous utilities
- recordtransfer.utils.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
- recordtransfer.utils.snake_to_camel_case(string: str) str
Convert a snake_case string to camelCase.
- recordtransfer.utils.html_to_text(html: str) str
Convert HTML content to plain text by stripping tags and whitespace.
- recordtransfer.utils.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)
- recordtransfer.utils.get_human_readable_file_count(file_names: list, accepted_file_groups: dict) str
Count the number of files falling into the ACCEPTED_FILE_FORMATS 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)
- recordtransfer.utils.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)
- recordtransfer.utils.mb_to_bytes(m: int) int
Convert MB to bytes.
- Parameters:
m (int) – Size in MB.
- Returns:
Size in bytes.
- Return type:
int
- recordtransfer.utils.bytes_to_mb(b: int) float
Convert bytes to MB.
- Parameters:
b (int) – Size in bytes.
- Returns:
Size in MB.
- Return type:
float
- recordtransfer.utils.accept_file(filename: str, filesize: int, file: UploadedFile) dict
Determine if a new file should be accepted.
- Parameters:
filename – The name of the file to check
filesize – The size of the file in bytes
file – The object containing the file data
These checks are applied: - The file name is safe and not malicious - The file name is not empty - The file has an accepted extension - The file’s MIME type matches the expected MIME type for the extension - The file’s size is an integer greater than zero - The file’s size is less than or equal to the maximum allowed size for one file
- Returns:
- A dictionary containing an ‘accepted’ key that contains True if
the session is valid, or False if not. The dictionary also contains an ‘error’ and ‘verboseError’ key if ‘accepted’ is False.
- Return type:
(dict)
- recordtransfer.utils.accept_session(filename: str, filesize: str | int, session: UploadSession) dict
Determine if a new file should be accepted as part of the session.
These checks are applied: - The session has room for more files according to the MAX_TOTAL_UPLOAD_COUNT - The session has room for more files according to the MAX_TOTAL_UPLOAD_SIZE_MB - A file with the same name has not already been uploaded
- Parameters:
filename (str) – The name of the file
filesize (Union[str, int]) – A string or integer representing the size of the file (in bytes)
session (UploadSession) – The session files are being uploaded to
- Returns:
- A dictionary containing an ‘accepted’ key that contains True if
the session is valid, or False if not. The dictionary also contains an ‘error’ and ‘verboseError’ key if ‘accepted’ is False.
- Return type:
(dict)
- recordtransfer.utils.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.
- recordtransfer.utils.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.
- recordtransfer.utils.get_client_ip_address(request: HttpRequest) str
Get the client’s IP address from the request.