8. Utility functions

There are various useful functions in kay.utils.

kay.utils.set_trace()
Set pdb’s set_trace with appropriate configuration.
kay.utils.raise_on_dev()
Raises A RuntimeError only on development environment.
kay.utils.get_timezone(tzname)

Get the timezone.

Parameter:tzname – The name of a timezone.
Returns:datetime.tzinfo implementation
kay.utils.url_for(endpoint, **args)

Get the URL to an endpoint. There are some special keyword arguments:

_anchor: This string is used as URL anchor.

_external: If set to True the URL will be generated with the full server name and http:// prefix.

Parameter:args – Keyword arguments are used for building a URL
Returns:string representing the URL to an endpoint
kay.utils.create_login_url(url=None)

Get the URL for a login page.

Parameter:url – The URL which user is redirected after the login process. If none supplied, the current URL will be use.
Returns:string representing the login URL.
kay.utils.create_logout_url(url=None)

Get the URL for a logout page.

Parameter:url – The URL which user is redirected after the logout process. If none supplied, the current URL will be use.
Returns:string representing the logout URL.
kay.utils.render_error(e)

Render an instance of werkzeug.exceptions.HTTPException with Jinja2 template.

Parameter:e – An instance of any subclass of werkzeug.exceptions.HTTPException
Returns:An instance of werkzeug.Response
kay.utils.render_to_string(template, context={}, processors=None)

A function for template rendering adding useful variables to context automatically, according to the CONTEXT_PROCESSORS settings.

Parameters:
  • template – The pathname of a template.
  • context – The context dictionary passed to the template.
  • processors – The processors for ondemand use.
Returns:

Rendered string

kay.utils.render_to_response(template, context, mimetype='text/html', processors=None)

A function for render html pages.

Parameters:
  • template – The pathname of a template.
  • context – The context dictionary passed to the template.
  • processors – The processors for ondemand use.
  • mimetype – The mimetype of werkzeug.Response
Returns:

Rendered response

kay.utils.render_json_response(data, mimetype='application/json')

A function for render JSON output.

Parameters:
Returns:

Rendered response

kay.utils.to_local_timezone(datetime, tzname=settings.DEFAULT_TIMEZONE)

Convert a datetime object to the local timezone.

Parameters:
  • datetime – datetime object with UTC timezone
  • tzname – the name of a timezone
Returns:

datetime.datetime object with new timezone

kay.utils.to_utc(datetime, tzname=settings.DEFAULT_TIMEZONE)

Convert a datetime object to UTC and drop tzinfo.

Parameters:
  • datetime – datetime object with local timezone
  • tzname – the name of a timezone
Returns:

datetime.datetime object with UTC timezone

kay.utils.get_by_key_name_or_404(model_class, key_name)

Try to get the data with given key_name and return it or raise werkzeug.exceptions.NotFound when failed.

Parameters:
  • model_class – the model class
  • key_name – the key_name passed to model_class.get_by_key_name
Returns:

an instance of the model class on success

kay.utils.get_by_id_or_404(model_class, id)

Try to get the data with given id and return it or raise werkzeug.exceptions.NotFound when failed.

Parameters:
  • model_class – the model class
  • id – the id passed to model_class.get_by_id
Returns:

an instance of the model class on success

kay.utils.get_or_404(model_class, key)

Try to get the data with given key and return it or raise werkzeug.exceptions.NotFound when failed.

Parameters:
  • model_class – the model class
  • id – the key passed to model_class.get
Returns:

an instance of the model class on success

Previous topic

7. Using Jinja2

Next topic

9. Pagination

This Page