simple tags tls-sprint
authorsylvain.thenault@logilab.fr
Thu, 19 Feb 2009 18:22:55 +0100
branchtls-sprint
changeset 845 6103d0f55a0b
parent 844 8ab6f64c3750
child 846 e449f1da7d09
simple tags
common/tags.py
web/test/unittest_form.py
web/views/baseforms.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/tags.py	Thu Feb 19 18:22:55 2009 +0100
@@ -0,0 +1,14 @@
+from logilab.mtconverter import html_escape
+from cubicweb.common.uilib import simple_sgml_tag
+class tag(object):
+    def __init__(self, name):
+        self.name = name
+        
+    def __call__(self, __content=None, **attrs):
+        return simple_sgml_tag(self.name, __content, **attrs)
+
+input = tag('input')
+textarea = tag('textarea')
+a = tag('a')
+span = tag('span')
+img = tag('img')
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/test/unittest_form.py	Thu Feb 19 18:22:55 2009 +0100
@@ -0,0 +1,33 @@
+from logilab.common.testlib import TestCase, unittest_main, mock_object
+from cubicweb.web.form import *
+
+class ChangeStateForm(EntityFieldsForm):
+    state = TextField(widget=HiddenInput, eidparam=False)
+    __method = TextField(widget=HiddenInput, initial='set_state', eidparam=False)
+    trcomment = RichTextField()
+
+    def buttons(self):
+        return []
+
+class CustomChangeStateForm(ChangeStateForm):
+    hello = IntField(name='youlou')
+
+class EntityFieldsFormTC(TestCase):
+
+    def test(self):
+        req = mock_object(build_url=lambda *args,**kwargs: 'myurl.com')
+        form = ChangeStateForm(req, redirect_path='perdu.com')
+        entity = mock_object(eid=1)
+        self.assertEquals(form.render(entity, state=123),
+                          '''''')
+
+    def test_form_inheritance(self):
+        req = mock_object(build_url=lambda *args,**kwargs: 'myurl.com')
+        form = CustomChangeStateForm(req, redirect_path='perdu.com')
+        entity = mock_object(eid=1)
+        self.assertEquals(form.render(entity, state=123),
+                          '''''')
+        
+        
+if __name__ == '__main__':
+    unittest_main()
--- a/web/views/baseforms.py	Thu Feb 19 18:21:51 2009 +0100
+++ b/web/views/baseforms.py	Thu Feb 19 18:22:55 2009 +0100
@@ -86,7 +86,16 @@
         self.w(u'</li>')
 
 
-class ChangeStateForm(FormMixIn, EntityView):
+class ChangeStateForm(EntityFieldsForm):
+    state = TextField(widget=HiddenWidget)
+    __method = TextField(widget=HiddenWidget, initial='set_state')
+    trcomment = RichTextField(eidparam=True)
+
+    def buttons(self):
+        return [Button(label=stdmsgs.YES),
+                Button(label=stdmsgs.NO)]
+        
+class ChangeStateFormView(FormMixIn, EntityView):
     id = 'statuschange'
     title = _('status change')
 
@@ -106,44 +115,49 @@
             'st1': _(state.name),
             'st2': _(dest.name)}
         self.w(u'<p>%s</p>\n' % msg)
-        self.w(u'<form action="%s" onsubmit="return freezeFormButtons(\'entityForm\');" method="post" id="entityForm">\n'
-               % self.build_url('edit'))
-        self.w(u'<div id="progress">%s</div>' % _('validating...'))
-        self.w(u'<fieldset>\n')
-        #self.w(u'<input id="errorurl" type="hidden" name="__errorurl" value="%s"/>\n'
-        #       % html_escape(self.req.url()))
-        self.w(u'<input type="hidden" name="__form_id" value="%s"/>\n' % self.id)
-        self.w(u'<input type="hidden" name="eid" value="%s" />' % eid)
-        self.w(u'<input type="hidden" name="%s" value="%s"/>\n'
-               % (eid_param('__type', eid), entity.e_schema))
-        self.w(u'<input type="hidden" name="%s" value="%s"/>\n'
-               % (eid_param('state', eid), dest.eid))
-        self.w(u'<input type="hidden" name="__redirectpath" value="%s"/>\n'
-               % html_escape(self.redirectpath(entity)))
-        self.fill_form(entity, state, dest)
-        self.w(u'<input type="hidden" name="__method" value="set_state"/>\n')
-        self.w(self.button_ok(label=stdmsgs.YES, tabindex=self.req.next_tabindex()))
-        self.w(self.button_cancel(label=stdmsgs.NO, tabindex=self.req.next_tabindex()))
-        self.w(u'</fieldset>\n')
-        self.w(u'</form>')
+
+        form = ChangeStateForm(redirect_path=self.redirectpath(entity)) # self.vreg.select_form('changestateform')
+        self.w(form.render(req, entity, state=dest.eid))
+
         
-    def fill_form(self, entity, state, dest):
-        # hack to use the widget for comment_format
-        trinfo = self.vreg.etype_class('TrInfo')(self.req, None)
-        # widget are cached, copy it since we want to modify its name attribute
-        wdg = trinfo.get_widget('comment_format')
-        wdg.name = 'trcommentformat'
-        # set a value in entity to avoid lookup for a non existant attribute...
-        trinfo['trcommentformat'] = u''
-        # comment format/content have to be grouped using the original entity eid
-        wdg.rname = eid_param('trcommentformat', entity.eid)
-        self.w(wdg.render_label(trinfo))
-        self.w(wdg._edit_render(trinfo))
-        self.w(u'<br/>\n')
-        cformname = eid_param('trcomment', entity.eid)
-        self.w(u'<label for="%s">%s</label>\n' % (cformname, self.req._('comment:')))
-        self.w(u'<textarea rows="10" cols="80" name="%s" tabindex="%s"></textarea><br/>\n'
-               % (cformname, self.req.next_tabindex()))
+#         self.w(u'<form action="%s" onsubmit="return freezeFormButtons(\'entityForm\');" method="post" id="entityForm">\n'
+#                % self.build_url('edit'))
+#         self.w(u'<div id="progress">%s</div>' % _('validating...'))
+#         self.w(u'<fieldset>\n')
+#         #self.w(u'<input id="errorurl" type="hidden" name="__errorurl" value="%s"/>\n'
+#         #       % html_escape(self.req.url()))
+#         self.w(u'<input type="hidden" name="__form_id" value="%s"/>\n' % self.id)
+#         self.w(u'<input type="hidden" name="eid" value="%s" />' % eid)
+#         self.w(u'<input type="hidden" name="%s" value="%s"/>\n'
+#                % (eid_param('__type', eid), entity.e_schema))
+#         self.w(u'<input type="hidden" name="%s" value="%s"/>\n'
+#                % (eid_param('state', eid), dest.eid))
+#         self.w(u'<input type="hidden" name="__redirectpath" value="%s"/>\n'
+#                % html_escape(self.redirectpath(entity)))
+#         self.fill_form(entity, state, dest)
+#         self.w(u'<input type="hidden" name="__method" value="set_state"/>\n')
+#         self.w(self.button_ok(label=stdmsgs.YES, tabindex=self.req.next_tabindex()))
+#         self.w(self.button_cancel(label=stdmsgs.NO, tabindex=self.req.next_tabindex()))
+#         self.w(u'</fieldset>\n')
+#         self.w(u'</form>')
+        
+#     def fill_form(self, entity, state, dest):
+#         # hack to use the widget for comment_format
+#         trinfo = self.vreg.etype_class('TrInfo')(self.req, None)
+#         # widget are cached, copy it since we want to modify its name attribute
+#         wdg = trinfo.get_widget('comment_format')
+#         wdg.name = 'trcommentformat'
+#         # set a value in entity to avoid lookup for a non existant attribute...
+#         trinfo['trcommentformat'] = u''
+#         # comment format/content have to be grouped using the original entity eid
+#         wdg.rname = eid_param('trcommentformat', entity.eid)
+#         self.w(wdg.render_label(trinfo))
+#         self.w(wdg._edit_render(trinfo))
+#         self.w(u'<br/>\n')
+#         cformname = eid_param('trcomment', entity.eid)
+#         self.w(u'<label for="%s">%s</label>\n' % (cformname, self.req._('comment:')))
+#         self.w(u'<textarea rows="10" cols="80" name="%s" tabindex="%s"></textarea><br/>\n'
+#                % (cformname, self.req.next_tabindex()))
 
     def redirectpath(self, entity):
         return entity.rest_path()