apicrud.utils

utilities.py

Utilities

Miscellaneous utility functions that don’t fit elsewhere

created 11-apr-2020 by richb@instantlinux.net

Functions

gen_id([length, prefix, chars])

Record IDs are random 48-bit RFC-4648 radix-64 with a fixed prefix to make them (somewhat) more human-recognizable

initialize_app(application)

Initialize the Flask app defined by openapi.yaml

render_problem(error)

Render function to provide message in dict as required for react-admin to display text of message for exception connexion.ProblemException

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

replace_last_comma_and(string)

Replace the last comma with the word ‘and’, dealing with translation.

req_duration()

Report request duration as milliseconds

strip_tags(html)

Convert html to plain-text by stripping tags

utcnow()

For mocking: unittest.mock can’t patch out datetime.utcnow directly

Classes

HtmlStripper()

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

apicrud.utils.strip_tags(html)

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