Warning
Starting from CubicWeb version 4.0 all code related to generating html views has been moved to the Cube cubicweb_web.
If you want to migrate a project from 3.38 to 4.* while still using all the
html views you need to both install the cubicweb_web cube AND add it to
your dependencies and run add_cube('web')
.
cubicweb_web can be installed from pypi this way:
pip install cubicweb_web
We donât plan to maintain the features in cubicweb_web in the long run; we are moving to a full javascript frontend using both cubicweb_api (which exposes a HTTP API) and @cubicweb/client as a frontend javascript toolkit.
In the long run cubicweb_api will be merged inside of CubicWeb.
Ajax#
Warning
This approach is deprecated in favor of using cwclientlibjs. If your use react for your UI, try the react components from the cwelements library. The documentation is kept here as reference.
For historical reference of what Ajax is and used to be, one can read the wikipedia article about Ajax.
CubicWeb provides a few helpers to facilitate javascript <-> python communications.
You can, for instance, register some python functions that will become
callable from javascript through ajax calls. All the ajax URLs are handled
by the cubicweb_web.views.ajaxcontroller.AjaxController
controller.
The ajaxcontroller
module defines the AjaxController
controller and the ajax-func
cubicweb registry.
- class cubicweb_web.views.ajaxcontroller.AjaxController(*args, **kwargs)[source]#
AjaxController handles ajax remote calls from javascript
The following javascript function call:
var d = asyncRemoteExec('foo', 12, "hello"); d.addCallback(function(result) { alert('server response is: ' + result); });
will generate an ajax HTTP GET on the following url:
BASE_URL/ajax?fname=foo&arg=12&arg="hello"
The AjaxController controller will therefore be selected to handle those URLs and will itself select the
cubicweb_web.views.ajaxcontroller.AjaxFunction
matching the fname parameter.
ajax-funcs
registry hosts exposed remote functions, that is
functions that can be called from the javascript world.
To register a new remote function, either decorate your function
with the ajaxfunc()
decorator:
from cubicweb.predicates import mactch_user_groups
from cubicweb_web.views.ajaxcontroller import ajaxfunc
@ajaxfunc(output_type='json', selector=match_user_groups('managers'))
def list_users(self):
return [u for (u,) in self._cw.execute('Any L WHERE U login L')]
or inherit from AjaxFunction
and
implement the __call__
method:
from cubicweb_web.views.ajaxcontroller import AjaxFunction
class ListUser(AjaxFunction):
__regid__ = 'list_users' # __regid__ is the name of the exposed function
__select__ = match_user_groups('managers')
output_type = 'json'
def __call__(self):
return [u for (u, ) in self._cw.execute('Any L WHERE U login L')]
- class cubicweb_web.views.ajaxcontroller.AjaxFunction(req, **extra)[source]#
Attributes on this base class are:
- Attr
check_pageid: make sure the pageid received is valid before proceeding
- Attr
output_type:
None: no processing, no change on content-type
- json: serialize with json_dumps and set application/json
content-type
- xhtml: wrap result in an XML node and forces HTML / XHTML
content-type (use
_cw.html_content_type()
)
- cubicweb_web.views.ajaxcontroller.ajaxfunc(implementation=None, selector=<Predicate yes at 7fe7ca03c510>, output_type=None, check_pageid=False, regid=None)[source]#
promote a standard function to an
AjaxFunction
appobject.All parameters are optional:
- Parameters
selector â a custom selector object if needed, default is
yes()
output_type â either None, âjsonâ or âxhtmlâ to customize output content-type. Default is None
check_pageid â whether the function requires a valid pageid or not to proceed. Default is False.
regid â a custom __regid__ for the created
AjaxFunction
object. Default is to keep the wrapped function name.
ajaxfunc
can be used both as a standalone decorator:@ajaxfunc def my_function(self): return 42
or as a parametrizable decorator:
@ajaxfunc(output_type='json') def my_function(self): return 42