From 0337deefb3b725c55cef5c2b2eed99e8b8de3836 Mon Sep 17 00:00:00 2001
From: Gael Pasgrimaud
Date: Mon, 20 Jun 2011 22:22:43 +0200
Subject: [PATCH] basic template now use new UIButtons. add security check to
actions. fix tests
---
pyramid_formalchemy/actions.py | 21 ++-
pyramid_formalchemy/templates/admin/edit.pt | 14 +-
.../templates/admin/edit.pt.py | 103 +++------------
.../templates/admin/listing.pt | 7 +-
.../templates/admin/listing.pt.py | 93 +++-----------
pyramid_formalchemy/templates/admin/master.pt | 1 +
.../templates/admin/master.pt.py | 53 ++++----
pyramid_formalchemy/templates/admin/new.pt | 10 +-
pyramid_formalchemy/templates/admin/new.pt.py | 95 +++-----------
pyramid_formalchemy/templates/admin/show.pt | 13 +-
.../templates/admin/show.pt.py | 121 +++---------------
pyramidapp/pyramidapp/models.py | 2 +-
pyramidapp/pyramidapp/templates/foolisting.pt | 9 +-
.../pyramidapp/templates/foolisting.pt.py | 95 +++-----------
pyramidapp/pyramidapp/tests.py | 12 +-
15 files changed, 147 insertions(+), 502 deletions(-)
diff --git a/pyramid_formalchemy/actions.py b/pyramid_formalchemy/actions.py
index 97f830a..fa6264e 100644
--- a/pyramid_formalchemy/actions.py
+++ b/pyramid_formalchemy/actions.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from chameleon.zpt.template import PageTemplate
from pyramid.util import DottedNameResolver
+from pyramid.security import has_permission
from pyramid_formalchemy.i18n import TranslationString
from pyramid_formalchemy.i18n import get_localizer
from pyramid_formalchemy.i18n import _
@@ -95,9 +96,10 @@ class Action(object):
"""
- def __init__(self, id, content="", alt="", attrs=None, **rcontext):
+ def __init__(self, id, content="", alt="", permission=None, attrs=None, **rcontext):
self.id = id
self.attrs = attrs or {}
+ self.permission = permission
self.rcontext = rcontext
if 'id' not in self.attrs:
self.attrs['id'] = repr(id)
@@ -275,7 +277,11 @@ class Actions(list):
list.__init__(self, [res.maybe_resolve(a) for a in args])
def render(self, request, **kwargs):
- return self.sep.join([a.render(request, **kwargs) for a in self])
+ allowed_permissions = []
+ for a in self:
+ if a.permission is None or has_permission(a.permission, request.context, request):
+ allowed_permissions.append(a)
+ return self.sep.join([a.render(request, **kwargs) for a in allowed_permissions])
def __add__(self, other):
actions = list(self)+list(other)
@@ -317,7 +323,7 @@ class Languages(Actions):
}
def __init__(self, *args, **kwargs):
- list.__init__(self)
+ Actions.__init__(self)
klass=kwargs.get('class_', ListItem)
for l in args:
self.append(
@@ -331,6 +337,7 @@ class Languages(Actions):
new = UIButton(
id='new',
content=_('New ${model_label}'),
+ permission='new',
icon='ui-icon-circle-plus',
attrs=dict(href="request.fa_url(request.model_name, 'new')"),
)
@@ -339,6 +346,7 @@ new = UIButton(
save = UIButton(
id='save',
content=_('Save'),
+ permission='edit',
icon='ui-icon-check',
attrs=dict(onclick="jQuery(this).parents('form').submit();"),
)
@@ -346,6 +354,7 @@ save = UIButton(
save_and_add_another = UIButton(
id='save_and_add_another',
content=_('Save and add another'),
+ permission='edit',
icon='ui-icon-check',
attrs=dict(onclick=("var f = jQuery(this).parents('form');"
"jQuery('#next', f).val(window.location.href);"
@@ -355,6 +364,7 @@ save_and_add_another = UIButton(
edit = UIButton(
id='edit',
content=_('Edit'),
+ permission='edit',
icon='ui-icon-check',
attrs=dict(href="request.fa_url(request.model_name, request.model_id, 'edit')"),
)
@@ -368,18 +378,17 @@ back = UIButton(
delete = UIButton(
id='delete',
- views='edit',
content=_('Delete'),
+ permission='delete',
state='ui-state-error',
icon='ui-icon-trash',
- attrs=dict(onclick=("string:var f = jQuery(this).parents('form');"
+ attrs=dict(onclick=("var f = jQuery(this).parents('form');"
"f.attr('action', window.location.href.replace('/edit', '/delete'));"
"f.submit();")),
)
cancel = UIButton(
id='cancel',
- views='edit',
content=_('Cancel'),
icon='ui-icon-circle-arrow-w',
attrs=dict(href="request.fa_url(request.model_name)"),
diff --git a/pyramid_formalchemy/templates/admin/edit.pt b/pyramid_formalchemy/templates/admin/edit.pt
index 23faf45..17a4d9a 100644
--- a/pyramid_formalchemy/templates/admin/edit.pt
+++ b/pyramid_formalchemy/templates/admin/edit.pt
@@ -4,18 +4,8 @@
diff --git a/pyramid_formalchemy/templates/admin/master.pt.py b/pyramid_formalchemy/templates/admin/master.pt.py
index 37543a5..46eb61b 100644
--- a/pyramid_formalchemy/templates/admin/master.pt.py
+++ b/pyramid_formalchemy/templates/admin/master.pt.py
@@ -1,24 +1,25 @@
registry = dict(version=0)
def bind():
from cPickle import loads as _loads
+ _attrs_4353373456 = _loads('(dp1\nVrel\np2\nVstylesheet\np3\ns.')
_lookup_attr = _loads('cchameleon.core.codegen\nlookup_attr\np1\n.')
_init_scope = _loads('cchameleon.core.utils\necontext\np1\n.')
_re_amp = _loads("cre\n_compile\np1\n(S'&(?!([A-Za-z]+|#[0-9]+);)'\np2\nI0\ntRp3\n.")
- _attrs_4358968400 = _loads('(dp1\n.')
- _attrs_4358849552 = _loads('(dp1\n.')
- _attrs_4358967760 = _loads('(dp1\nVid\np2\nVheader\np3\nsVclass\np4\nVui-widget-header ui-corner-all\np5\ns.')
- _attrs_4358720592 = _loads('(dp1\nVrel\np2\nVstylesheet\np3\ns.')
- _attrs_4358967504 = _loads('(dp1\nVid\np2\nVcontent\np3\nsVclass\np4\nVui-admin ui-widget\np5\ns.')
- _attrs_4358967888 = _loads('(dp1\nVclass\np2\nVbreadcrumb\np3\ns.')
+ _attrs_4353342480 = _loads('(dp1\n.')
+ _attrs_4345507344 = _loads('(dp1\nVid\np2\nVheader\np3\nsVclass\np4\nVui-widget-header ui-corner-all\np5\ns.')
+ _attrs_4353343184 = _loads('(dp1\nVclass\np2\nVbreadcrumb\np3\ns.')
+ _attrs_4353342544 = _loads('(dp1\n.')
+ _attrs_4353376016 = _loads('(dp1\n.')
_init_stream = _loads('cchameleon.core.generation\ninitialize_stream\np1\n.')
- _attrs_4358968272 = _loads('(dp1\n.')
- _attrs_4358721104 = _loads('(dp1\n.')
- _attrs_4358721040 = _loads('(dp1\n.')
+ _attrs_4353376144 = _loads('(dp1\n.')
+ _attrs_4353376208 = _loads('(dp1\n.')
+ _attrs_4353372432 = _loads('(dp1\nVsrc\np2\nVhttps://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js\np3\ns.')
_init_default = _loads('cchameleon.core.generation\ninitialize_default\np1\n.')
- _attrs_4358721232 = _loads('(dp1\n.')
- _attrs_4358967696 = _loads('(dp1\n.')
+ _attrs_4353372560 = _loads('(dp1\nVid\np2\nVcontent\np3\nsVclass\np4\nVui-admin ui-widget\np5\ns.')
+ _attrs_4345507600 = _loads('(dp1\n.')
+ _attrs_4353342352 = _loads('(dp1\n.')
+ _attrs_4353413200 = _loads('(dp1\n.')
_init_tal = _loads('cchameleon.core.generation\ninitialize_tal\np1\n.')
- _attrs_4358967952 = _loads('(dp1\n.')
def render(econtext, rcontext=None):
macros = econtext.get('macros')
_translate = econtext.get('_translate')
@@ -34,15 +35,15 @@ def bind():
default = None
u'None'
_domain = None
- attrs = _attrs_4358849552
+ attrs = _attrs_4353376016
_write(u'\n ')
- attrs = _attrs_4358721040
+ attrs = _attrs_4353376144
u"''"
_write(u'\n ')
_default.value = default = ''
u"request.model_name or 'root'"
_content = (_lookup_attr(econtext['request'], 'model_name') or 'root')
- attrs = _attrs_4358721104
+ attrs = _attrs_4353413200
u'_content'
_write(u'
')
_tmp1 = _content
@@ -70,7 +71,7 @@ def bind():
_tmp = _tmp.replace('>', '>')
_write(_tmp)
_write(u'\n ')
- attrs = _attrs_4358720592
+ attrs = _attrs_4353373456
u"request.static_url('pyramid_formalchemy:static/admin.css')"
_write(u'
\n \n ')
- attrs = _attrs_4358721232
+ _write(u'>\n ')
+ attrs = _attrs_4353372432
+ _write(u'\n \n ')
+ attrs = _attrs_4353376208
_write(u'\n ')
- attrs = _attrs_4358967504
+ attrs = _attrs_4353372560
_write(u'
\n ')
- attrs = _attrs_4358967760
+ attrs = _attrs_4345507344
_write(u'
diff --git a/pyramid_formalchemy/templates/admin/edit.pt.py b/pyramid_formalchemy/templates/admin/edit.pt.py
index b1843d5..7adcccc 100644
--- a/pyramid_formalchemy/templates/admin/edit.pt.py
+++ b/pyramid_formalchemy/templates/admin/edit.pt.py
@@ -3,19 +3,13 @@ def bind():
from cPickle import loads as _loads
_lookup_attr = _loads('cchameleon.core.codegen\nlookup_attr\np1\n.')
_init_scope = _loads('cchameleon.core.utils\necontext\np1\n.')
- _re_amp = _loads("cre\n_compile\np1\n(S'&(?!([A-Za-z]+|#[0-9]+);)'\np2\nI0\ntRp3\n.")
- _attrs_4359005584 = _loads('(dp1\n.')
- _attrs_4359005840 = _loads('(dp1\nVname\np2\nV_method\np3\nsVtype\np4\nVhidden\np5\nsVvalue\np6\nVPUT\np7\ns.')
- _attrs_4359006416 = _loads('(dp1\nVclass\np2\nVfa_field\np3\ns.')
- _attrs_4359006672 = _loads('(dp1\nVclass\np2\nVui-widget-header ui-widget-link ui-corner-all\np3\ns.')
- _attrs_4359005008 = _loads('(dp1\nVclass\np2\nVui-icon ui-icon-circle-arrow-w\np3\ns.')
+ _attrs_4356242384 = _loads('(dp1\n.')
+ _attrs_4356371600 = _loads('(dp1\n.')
_init_stream = _loads('cchameleon.core.generation\ninitialize_stream\np1\n.')
- _attrs_4359005968 = _loads('(dp1\n.')
+ _attrs_4356243152 = _loads('(dp1\nVclass\np2\nVfa_field\np3\ns.')
_init_default = _loads('cchameleon.core.generation\ninitialize_default\np1\n.')
- _attrs_4359006544 = _loads('(dp1\nVhref\np2\nV#\nsVclass\np3\nVui-widget-header ui-widget-link ui-widget-button ui-corner-all\np4\ns.')
- _attrs_4359005520 = _loads('(dp1\nVaction\np2\nV\nsVmethod\np3\nVPOST\np4\nsVenctype\np5\nVmultipart/form-data\np6\ns.')
- _attrs_4359006736 = _loads("(dp1\nVtype\np2\nVsubmit\np3\nsVvalue\np4\nV${F_('Save')}\np5\ns.")
- _attrs_4359005776 = _loads('(dp1\n.')
+ _attrs_4356370704 = _loads('(dp1\nVaction\np2\nV\nsVmethod\np3\nVPOST\np4\nsVenctype\np5\nVmultipart/form-data\np6\ns.')
+ _attrs_4356239504 = _loads('(dp1\nVname\np2\nV_method\np3\nsVtype\np4\nVhidden\np5\nsVvalue\np6\nVPUT\np7\ns.')
_init_tal = _loads('cchameleon.core.generation\ninitialize_tal\np1\n.')
def render(econtext, rcontext=None):
macros = econtext.get('macros')
@@ -37,15 +31,15 @@ def bind():
def _callback_main(econtext, _repeat, _out=_out, _write=_write, _domain=_domain, **_ignored):
if _repeat:
repeat.update(_repeat)
- attrs = _attrs_4359005584
+ attrs = _attrs_4356371600
_write(u'
\n \n \n')
u"{'main': _callback_main}"
_tmp = {'main': _callback_main, }
u"main.macros['master']"
diff --git a/pyramid_formalchemy/templates/admin/listing.pt b/pyramid_formalchemy/templates/admin/listing.pt
index 66f66dd..e0bf70d 100644
--- a/pyramid_formalchemy/templates/admin/listing.pt
+++ b/pyramid_formalchemy/templates/admin/listing.pt
@@ -3,12 +3,7 @@
diff --git a/pyramid_formalchemy/templates/admin/listing.pt.py b/pyramid_formalchemy/templates/admin/listing.pt.py
index 6a5a24a..173f22a 100644
--- a/pyramid_formalchemy/templates/admin/listing.pt.py
+++ b/pyramid_formalchemy/templates/admin/listing.pt.py
@@ -3,16 +3,13 @@ def bind():
from cPickle import loads as _loads
_lookup_attr = _loads('cchameleon.core.codegen\nlookup_attr\np1\n.')
_init_scope = _loads('cchameleon.core.utils\necontext\np1\n.')
- _re_amp = _loads("cre\n_compile\np1\n(S'&(?!([A-Za-z]+|#[0-9]+);)'\np2\nI0\ntRp3\n.")
- _attrs_4359083472 = _loads('(dp1\nVclass\np2\nVui-widget-header ui-widget-link ui-corner-all\np3\ns.')
- _attrs_4359083344 = _loads('(dp1\n.')
- _attrs_4359083216 = _loads('(dp1\nVclass\np2\nVui-pager\np3\ns.')
+ _attrs_4353960208 = _loads('(dp1\nVclass\np2\nVlayout-grid\np3\ns.')
+ _attrs_4353960144 = _loads('(dp1\nVclass\np2\nVui-pager\np3\ns.')
_init_stream = _loads('cchameleon.core.generation\ninitialize_stream\np1\n.')
- _attrs_4359083600 = _loads('(dp1\nVclass\np2\nVui-icon ui-icon-circle-plus\np3\ns.')
- _attrs_4359083088 = _loads('(dp1\n.')
+ _attrs_4353960272 = _loads('(dp1\nVclass\np2\nVfa_field\np3\ns.')
_init_default = _loads('cchameleon.core.generation\ninitialize_default\np1\n.')
+ _attrs_4353960016 = _loads('(dp1\n.')
_init_tal = _loads('cchameleon.core.generation\ninitialize_tal\np1\n.')
- _attrs_4359083280 = _loads('(dp1\nVclass\np2\nVlayout-grid\np3\ns.')
def render(econtext, rcontext=None):
macros = econtext.get('macros')
_translate = econtext.get('_translate')
@@ -33,13 +30,13 @@ def bind():
def _callback_main(econtext, _repeat, _out=_out, _write=_write, _domain=_domain, **_ignored):
if _repeat:
repeat.update(_repeat)
- attrs = _attrs_4359083088
+ attrs = _attrs_4353960016
u"''"
_write(u'
\n ')
_default.value = default = ''
u'pager'
_content = econtext['pager']
- attrs = _attrs_4359083216
+ attrs = _attrs_4353960144
u'_content'
_write(u'\n')
+ _write(u'\n
\n')
u"{'main': _callback_main}"
_tmp = {'main': _callback_main, }
u"main.macros['master']"
diff --git a/pyramid_formalchemy/templates/admin/master.pt b/pyramid_formalchemy/templates/admin/master.pt
index f0d5d49..cec236a 100644
--- a/pyramid_formalchemy/templates/admin/master.pt
+++ b/pyramid_formalchemy/templates/admin/master.pt
@@ -2,6 +2,7 @@