apicrud.utils¶
utilities.py
- Utilities
Miscellaneous utility functions that don’t fit elsewhere
created 11-apr-2020 by richb@instantlinux.net
Functions
|
Record IDs are random 48-bit RFC-4648 radix-64 with a fixed prefix to make them (somewhat) more human-recognizable |
|
Initialize the Flask app defined by openapi.yaml |
|
Render function to provide message in dict as required for react-admin to display text of message for exception connexion.ProblemException |
|
Render function to provide message in dict as required for react-admin to display text of message for 400 error codes |
|
Replace the last comma with the word ‘and’, dealing with translation. |
Report request duration as milliseconds |
|
|
Convert html to plain-text by stripping tags |
|
For mocking: unittest.mock can’t patch out datetime.utcnow directly |
Classes
-
class
apicrud.utils.
HtmlStripper
¶ -
get_data
()¶ return the stripped html
Returns: str
-
handle_data
(d)¶ add a string to the html object
- Parameters
d (str) – chunk of html
-
-
apicrud.utils.
gen_id
(length=8, prefix='x-', chars='-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz')¶ Record IDs are random 48-bit RFC-4648 radix-64 with a fixed prefix to make them (somewhat) more human-recognizable
First 15 bits are generated from Unix epoch to make them sortable by date (granularity 24 hours); rolls over after year 2107
- Parameters
length (int) – length of generated portion after prefix
prefix (str) – prefix to distinguish ID type
chars (str) – set of characters to choose from for random portion
-
apicrud.utils.
initialize_app
(application)¶ Initialize the Flask app defined by openapi.yaml
- Parameters
application (obj) – a connexion object
init_func (function) – any other function to call
- Returns
Flask app
- Return type
obj
-
apicrud.utils.
render_problem
(error)¶ Render function to provide message in dict as required for react-admin to display text of message for exception connexion.ProblemException
- Parameters
error (obj) – the error object with name and description
-
apicrud.utils.
render_status_400
(error)¶ Render function to provide message in dict as required for react-admin to display text of message for 400 error codes
- Parameters
error (obj) – the error object with name and description
-
apicrud.utils.
replace_last_comma_and
(string)¶ Replace the last comma with the word ‘and’, dealing with translation. The string is presumed to be a text array joined by ‘, ‘ – including the space.
- Parameters
string (str) – comma-separated utf8 content
-
apicrud.utils.
req_duration
()¶ Report request duration as milliseconds
Convert html to plain-text by stripping tags
- Parameters
html (str) – an html document
-
apicrud.utils.
utcnow
()¶ For mocking: unittest.mock can’t patch out datetime.utcnow directly