API

This section of the documentation details the API for flask-require.

Requirement classes

Instances of these classes can be used either as decorators for methods of as functions to pass the the before_request() hook of a blueprint.

class flask_require.Require(check_function, redirect_endpoint, flash_msg=None, msg_category='warning')

A simple requirement decorator that calls a funciton to performs a check. If the check fails then the view is redirected. Optionally a ‘flash’ message can be added.

Parameters:
  • check_function – a callable that needs to return True for processing to proceed

  • redirect_endpoint – the view name, path or URL target if the check fails

  • flash_msg – an optional message to flash to the user

  • msg_category – an optional catagory for the flashed message

check(**kwargs)

Perform the checks as a function, rather than a decorator

class flask_require.SessionRequire(needs, redirect_endpoint, flash_msg=None, msg_category='warning')

A class that checks conditions in the current Flask sessions. If the conditions are not met then a redirect reply is sent to send the user to the given URL. Optionally a ‘flash’ message can be added.

Parameters:
  • needs – Either a single item or a list of items, each of which may either be the name of a key to check or a tuple of a key name and a check, where the check may be a single value, a list of acceptable values or a callable that takes the value and returns True if the value is acceptable.

  • redirect_endpoint – the view name, path or URL target if the check fails

  • flash_msg – an optional message to flash to the user

  • msg_category – an optional catagory for the flashed message

check(**kwargs)

Perform the checks as a function, rather than a decorator

class flask_require.ValueRequire(needs, redirect_endpoint, flash_msg=None, msg_category='warning')

A class that checks conditions in the values in the current Flask request. If the conditions are not met then a redirect reply is sent to send the user to the given URL. Optionally a ‘flash’ message can be added.

Parameters:
  • needs – Either a single item or a list of items, each of which may either be the name of a key to check or a tuple of a key name and a check, where the check may be a single value, a list of acceptable values or a callable that takes the value and returns True if the value is acceptable.

  • redirect_endpoint – the view name, path or URL target if the check fails

  • flash_msg – an optional message to flash to the user

  • msg_category – an optional catagory for the flashed message

check(**kwargs)

Perform the checks as a function, rather than a decorator

class flask_require.ContextRequire(needs, redirect_endpoint, flash_msg=None, msg_category='warning')

A class that checks conditions in the current global request context flask.g. If the conditions are not met then a redirect reply is sent to send the user to the given URL. Optionally a ‘flash’ message can be added.

Parameters:
  • needs – Either a single item or a list of items, each of which may either be the name of a key to check or a tuple of a key name and a check, where the check may be a single value, a list of acceptable values or a callable that takes the value and returns True if the value is acceptable.

  • redirect_endpoint – the view name, path or URL target if the check fails

  • flash_msg – an optional message to flash to the user

  • msg_category – an optional catagory for the flashed message

check(**kwargs)

Perform the checks as a function, rather than a decorator

class flask_require.AllRequire(*sub_reqs: BaseRequire, redirect_endpoint=None, flash_msg=None, msg_category=None)

Succeeds only if all requirements are met. Tests are applied in order and the redirect and flash message are for the first to fail.

Parameters:
  • sub_reqs – requirements that will be tested, in order from left to right

  • redirect_endpoint – if provided then failure of the compound test will redirect to this endpoint instead of the endpoint associated with the first test that failed

  • flash_msg – a flash message that overrides any message in the first test that failed

  • msg_category – a catagory for the flash message

check(**kwargs)

Perform the checks as a function, rather than a decorator

class flask_require.AnyRequire(*sub_reqs: BaseRequire, redirect_endpoint=None, flash_msg=None, msg_category=None)

Succeeds if any of the requirements are met. If none are met the redirect and lash message from the first is used.

Parameters:
  • sub_reqs – requirements that will be tested, in order from left to right

  • redirect_endpoint – if provided then failure of the compound test will redirect to this endpoint instead of the endpoint associated with the first test that failed

  • flash_msg – a flash message that overrides any message in the first test that failed

  • msg_category – a catagory for the flash message

check(**kwargs)

Perform the checks as a function, rather than a decorator

Value test classes

These classes implement various common checks that are often applied to values in the session state or the global context.

class flask_require.Contains(required_value)

A callable to check if a value is a list or tuple that contains a specific value

class flask_require.MatchContext(global_key)

A callable to check if a value is the same as the value stored under some key in the request global

class flask_require.TimeStampAge(age)

A callable class to check the age of the timestamp

Parameters:

age – either an interger number of seconds or a string of an interger count followed by one of “s”, “m”, “h”, “d”, “w”, “M” or “y” for units of second, minute, hour, day, week, month (30 days) or year (365 days) respectively.

Utility functions

flask_require.up()

Return the path to the node one level up from the current view