bag.web.pyramid.views module¶
Help for Pyramid view development.
- bag.web.pyramid.views.ajax_view(view_function)[source]¶
Decorate AJAX views to…
treat certain exceptions
convert the result to a dictionary if necessary.
This decorator grabs certain exceptions and turns them into an error JSON response that contains:
“error_msg”: the string to be displayed to end users
“error_title”: the string to be displayed as a header
“validation”: a dictionary of validation errors where keys are field names and values are the respective errors
possibly other variables, too
The transaction is not committed because we raise HTTPError.
- Return type
Callable
[[Any
,Any
],Any
]
- bag.web.pyramid.views.get_json_or_raise(request, expect=None, dict_has=None)[source]¶
Obtain and validate incoming JSON, raising Problem if necessary.
If the incoming json cannot be decoded, this is a bad request, so raise 400 instead of 500.
Usage examples:
get_json_or_raise(request) get_json_or_raise(request, expect=list) get_json_or_raise(request, dict_has=[('email', str), ('age', int)]) get_json_or_raise(request, dict_has=[('amount', (int, float))])
The json body, when decoded, may become one of a number of types (usually dict or list). You can validate the type by passing an
expect
argument. If the json decodes to the wrong type, also raise 400 instead of 500.You may also ensure that a decoded dictionary contains some required keys by passing as the
dict_has
argument a sequence of 2-tuples where each elemaint contains 1) the required key names and 2) the accepted value type(s). 400 is raised if a key is missing.
- bag.web.pyramid.views.maybe_raise_unprocessable(exc, **adict)[source]¶
Raise if the provided exception looks like a validation error.
Raise 422 Unprocessable Entity, optionally with additional information.
- Return type
None
- bag.web.pyramid.views.serve_preloaded(config, route_name, route_path, payload, encoding='', content_type='')[source]¶
Read a file (such as robots.txt or favicon.ini) into memory.
…then set up a view that serves it.
Pass no
encoding
if the file is binary. If text, pass the encoding in which the file should be read (usually ‘utf-8’).Usage:
from bag.web.pyramid.views import serve_preloaded serve_preloaded( config, route_name='robots', route_path='robots.txt', payload='my_package:static/robots.txt', encoding='utf-8') serve_preloaded( config, route_name='favicon', route_path='favicon.ico', payload='my_package:static/favicon.ico', content_type='image/x-icon', )
- Return type
None
- bag.web.pyramid.views.xeditable_view(view_function)[source]¶
Decorate AJAX views that need to be friendly towards x-editable.
x-editable is a famous edit-in-place component for AngularJS. x-editable likes text/plain instead of JSON responses; so it likes us to return either an error string or “204 No content”.
- Return type
Callable
[[Any
,Any
],Any
]