pywebtools.pyramid.util – Utility Functions for Pyramid

Utility functions for use with the Pyramid framework.

  • request_from_args() is useful with decorators or any other function where you do not know which argument is the Pyramid Request.
  • get_config_setting() provides easy access to configuration settings set in the [app:main] section of the INI file.
  • require_method() is a decorator to enforce HTTP methods.
  • MenuBuilder is a helper class to generate the menu structure used with menubar().
class pywebtools.pyramid.util.MenuBuilder

The MenuBuilder helps with creating the list structure used for creating the icon-menubar used with menubar(). Call group() to start a new group of menu items. Call item() to add a menu item to the current group. generate() then generates the final structure for use in the menubar.


Generate the final menu structure.

Returns:The list of menu groups with their menu items
Return_type:list of menu groups
group(label, icon=None)

Add a new group to the list of groups in this MenuBuilder.

  • label (unicode) – The menu group’s label
  • icon (unicode) – The optional icon for this group. An icon must be provided in order to enable highlighting of menu items
menu(label, href, icon=None, highlight=False, attrs=None)

Add a new menu item to the current group. Will create a new group with an empty label if group() has not been called.

  • label (unicode) – The menu item’s label
  • href (unicode) – The URL that the menu item loads
  • icon (unicode) – The optional icon for this menu item
  • highlight (boolean) – Whether to highlight the menu item by displaying it at the top level
  • attrs (dict) – Additional attributes to set for the menu item link
pywebtools.pyramid.util.confirm_action(title, message, cancel, ok)

Generates a confirmation JSON object for use with the jQuery.postLink() plugin.

  • title (unicode) – The title of the confirmation dialog box
  • message (unicode) – The main message to show
  • cancel (dict or unicode) – The cancel button’s settings
  • ok (dict or unicode) – The ok button’s settings

JSON object

Return type:


pywebtools.pyramid.util.confirm_delete(obj_type, title, has_parts=False)

Generates the confirmation JSON object for use with the jQuery.postLink() plugin.

  • obj_type (unicode) – The type of object that is being deleted
  • title (unicode) – The title of the object that is being delete
  • has_parts (bool) – Whether to add the suffix ” and all its parts”

JSON object

Return type:


pywebtools.pyramid.util.convert_type(value, target_type, default=None)

Attempts to convert the value to the given target_type. Will return default if the conversion fails.

Supported target_type values are:

  • int – Convert to an integer value
  • boolean – Convert to a boolean value (True if the value is the unicode string “true” in any capitalisation
  • list – Convert to a list, splitting on line-breaks and commas
  • value (unicode) – The value to convert
  • target_type (unicode) – The target type to convert to
  • default – The default value if the conversion fails

The converted value

pywebtools.pyramid.util.get_config_setting(request, key, target_type=None, default=None)

Gets a configuration setting from the application configuration. Settings are cached for faster access.

  • request (Request) – The request used to access the configuration settings
  • key (unicode) – The configuration key
  • target_type – If specified, will convert the configuration setting to the given type using convert_type()

The configuration setting value or default

pywebtools.pyramid.util.paginate(request, route_name, query, start, rows, query_params=None)

Generates the list of pages for a query.

  • request (Request) – The request used to generate URLs
  • route_name (str()) – The name of the route to use for URLs
  • query (Query) – The SQLAlchemy query to generate the pagination for
  • start (int()) – The current starting index
  • rows (int()) – The number of rows per page
  • query_params (list() of tuple()) – An optional list of query parameters to include in all URLs that are generated

The list() of pages to use with the “navigation.pagination” helper

Return type:



Returns the Request from the function parameters list args.

Parameters:args – The parameters passed to a function
Returns:The request object