Building your URLs in cubicweb
In cubicweb, you often have to build url's that redirect the current view to
a specific entity view or allow the execution of a given action. Moreover, you
often want also to fallback to the previous view once the specific action or
edition is done, or redirect also to another entity's specific view.
To do so, cubicweb provides you with a set of powerful tools, however as
there is often more than one way to do it, this blog entry is here to help
you in choosing the preferred way.
This case often appears when you want to create a link to a startup view or a
controller. It the first case, you simply build you URL like this:
self._cw.build_url('view', vid='my_view_id')
The latter case appears when you want to call a controller directly without
having to define a form in your view. This can happen for instance when you
want to create a URL that will set a relation between 2 objects and do not need
any confirmation for that. The URL construction is done like this:
self._cw.build_url('my_controller_id', arg1=value1, arg2=value2, ...)
Any extra arguments passed to the build_url method will be available in the
controller as key, values pairs of the self._cw.forms dictionary. This is
especially useful when you want to define some kind of hidden attributes
but there is not form to put them into.
And, last but not least, a convenient way to get the root URL of the instance:
self._cw.base_url()
There are other ways to create a link to registered actions than using
build_url, mostly by accessing them via the registry vreg.
For instance, the action registry holds effectively all possible actions in a
given context: a specific action can be selected using the select_or_none()
method, or even using the possible_action() method which will return a list of
categorized actions. The url of the action is then available as
action.url(). For contextual components (e.g. boxes), you can even directly
get a link to the selected action(s) using the self.action_link(this_action)
method.
If the action corresponds to the creation of a new entity, there is an even
faster and elegant way to do it, using the schema of your cube:
url = self._cw.vreg["etypes"].etype_class('MyEntity').cw_create_url(self._cw)
Get the URL of the outofcontext view of an entity:
link = entity.absolute_url(vid='outofcontext')
Create a link to a given controller then fall back to the current view:
self.w(u'<a href="%s">Click me</a>' % xml_escape(
self._cw.build_url('mycontrollerid',
arg1=value1, arg2=value2,
rql=self.cw_rset.printable_rql(),
__redirectvid=self._cw.form.get('vid',''))))
def publish(self, rset):
value1, value2 = self._cw.form['arg1'], self._cw.form['arg2']
# do some stuff with value1 and value2 here...
raise Redirect(self._cw.build_url(rql=self._cw.form['rql'],
vid=self._cw.form['__redirectvid'],
__message=_('you message')))
Create a link to add a given entity and relate this entity to the current one
with a relation 'child_of', then go back to the current entity's view:
entity = self.cw_rset.get_entity(0,0)
self.w(u'<a href="%s">Click me</a>' % xml_escape(
self._cw.build_url('add/Mychildentity',
__linkto='child_of:%s:object' % entity.eid,
__redirectpath=entity.rest_path(),
__redirectvid=self._cw.form.get('vid', ''))))
Same example, but we suppose that we are in a multiple rset entity view, and we
want to go back afterwards to this view:
entity = self.cw_rset.get_entity(0,0)
self.w(u'<a href="%s">Click me</a>' % xml_escape(
self._cw.build_url('add/Mychildentity',
rql=self.cw_rset.printable_rql(),
__linkto='child_of:%s:object' % entity.eid,
__redirectvid=self._cw.form.get('vid', ''))))
Create links to all 'menuactions' in a view:
actions = self._cw.vreg['actions'].possible_actions(self._cw, rset=self.cw_rset)
action_links = [unicode(self.action_link(x)) for x in actions.get('menuactions', ())]
self.w( u' | '.join(action_links))