pyramid_formalchemy provide a way to use some actions in your template. Action are basically links or input button.
By default there is only one category buttons which are the forms buttons but you can add some categories like this:
>>> from pyramid_formalchemy.views import ModelView
>>> from pyramid_formalchemy import actions
>>> class MyView(ModelView):
... # keep default action categorie and add the custom_actions categorie
... actions_categories = ('buttons', 'custom_actions')
... # update the default actions for all models
... defaults_actions = actions.defaults_actions.copy()
... defaults_actions.update(edit_custom_actions=Actions())
Where myactions is an Actions instance
You can also customize the actions per Model:
>>> from sqlalchemy import Column, Integer
>>> from sqlalchemy.ext.declarative import declarative_base
>>> Base = declarative_base()
>>> class MyArticle(Base):
... __tablename__ = 'myarticles'
... edit_buttons = Actions()
... id = Column(Integer, primary_key=True)
The available actions are:
But you can add your own:
>>> from pyramid_formalchemy.views import ModelView
>>> from pyramid_formalchemy import actions
>>> class MyView(ModelView):
... actions.action()
... def extra(self):
... # do stuff
... return self.render(**kw)
Then pyramid_formalchemy will try to load some extra_buttons actions.
A model action is used to add some action in model views. The content and alt parameters should be a pyramid.i18n.TranslationString:
>>> from webob import Request
>>> request = Request.blank('/')
>>> class MyAction(Action):
... body = u'<a tal:attributes="%(attributes)s">${content}</a>'
>>> action = MyAction('myaction', content=_('Click here'),
... attrs={'href': repr('#'), 'onclick': repr('$.click()')})
>>> action.render(request)
u'<a href="#" id="myaction" onclick="$.click()">Click here</a>'
A action list. Can contain pyramid_formalchemy.actions.Action or a dotted name:
>>> actions = Actions('pyramid_formalchemy.actions.delete',
... Link('link1', content=_('A link'), attrs={'href':'request.application_url'}))
>>> actions
<Actions [<UIButton delete>, <Link link1>]>
You must use a request to render them:
>>> from webob import Request
>>> request = Request.blank('/')
>>> print actions.render(request)
<a class="ui-widget-header ...">
<span class="ui-icon ui-icon-trash"></span>
Delete
</a>
<a href="http://localhost" id="link1">A link</a>
You can add actions:
>>> new_actions = Actions('pyramid_formalchemy.actions.new') + actions
>>> new_actions
<Actions [<UIButton new>, <UIButton delete>, <Link link1>]>
An action rendered as an input:
>>> from webob import Request
>>> request = Request.blank('/')
>>> action = Input('myaction',
... content=_('Click here'))
Rendering:
>>> action.render(request)
u'<input value="Click here" type="submit" id="myaction" />'
Languages actions:
>>> langs = Languages('fr', 'en')
>>> langs
<Languages [<ListItem lang_fr>, <ListItem lang_en>]>
It take care about the active language:
>>> from webob import Request
>>> request = Request.blank('/')
>>> request.cookies['_LOCALE_'] = 'fr'
>>> request.route_url = lambda name, _query: 'http://localhost/set_language?_LOCALE_=%(_LOCALE_)s' % _query
>>> print langs.render(request)
<li><a href="http://localhost/set_language?_LOCALE_=fr" class="lang_fr lang_active" id="lang_fr">French</a></li>
<li><a href="http://localhost/set_language?_LOCALE_=en" class="lang_en " id="lang_en">English</a></li>
An action rendered as a link:
>>> from webob import Request
>>> request = Request.blank('/')
>>> action = Link('myaction',
... attrs={'href': 'request.application_url'},
... content=_('Click here'))
>>> action.render(request)
u'<a href="http://localhost" id="myaction">Click here</a>'
An action rendered as a link contained by a list item:
>>> from webob import Request
>>> request = Request.blank('/')
>>> action = ListItem('myaction',
... attrs={'href': 'request.application_url'},
... content=_('Click here'))
>>> action.render(request)
u'<li><a href="http://localhost" id="myaction">Click here</a></li>'
An action rendered as a select option:
>>> from webob import Request
>>> request = Request.blank('/')
>>> action = Option('myaction',
... value='request.application_url',
... content=_('Click here'))
Rendering:
>>> action.render(request)
u'<option id="myaction" value="http://localhost">Click here</option>'
An action container used to store action in requests. Return an empty Actions instance if actions are not found
An action rendered as an jquery.ui aware link:
>>> from webob import Request
>>> request = Request.blank('/')
>>> action = UIButton('myaction', icon='ui-icon-trash',
... content=_("Click here"))
>>> print action.render(request)
<a class="ui-widget-header ui-widget-link ui-widget-button ui-corner-all " id="myaction">
<span class="ui-icon ui-icon-trash"></span>
Click here
</a>
You can use javascript:
>>> action = UIButton('myaction', icon='ui-icon-trash',
... content=_("Click here"), attrs={'onclick':'$(#link).click();'})
>>> print action.render(request)
<a class="ui-widget-header ui-widget-link ui-widget-button ui-corner-all " href="#" id="myaction" onclick="$(#link).click();">
<span class="ui-icon ui-icon-trash"></span>
Click here
</a>