--- a/web/form.py Wed Mar 24 10:23:31 2010 +0100
+++ b/web/form.py Wed Apr 28 11:54:13 2010 +0200
@@ -1,9 +1,22 @@
+# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# logilab-common is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
"""abstract form classes for CubicWeb web client
-:organization: Logilab
-:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
-:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
-:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""
__docformat__ = "restructuredtext en"
@@ -14,8 +27,7 @@
from cubicweb.appobject import AppObject
from cubicweb.view import NOINDEX, NOFOLLOW
-from cubicweb.web import httpcache, formfields, controller
-
+from cubicweb.web import httpcache, formfields, controller, formwidgets as fwdgs
class FormViewMixIn(object):
"""abstract form view mix-in"""
@@ -137,8 +149,9 @@
@iclassmethod
def field_by_name(cls_or_self, name, role=None):
- """return field with the given name and role.
- Raise FieldNotFound if the field can't be found.
+ """Return field with the given name and role.
+
+ Raise :exc:`FieldNotFound` if the field can't be found.
"""
for field in cls_or_self._fieldsattr():
if field.name == name and field.role == role:
@@ -147,31 +160,49 @@
@iclassmethod
def fields_by_name(cls_or_self, name, role=None):
- """return a list of fields with the given name and role"""
+ """Return a list of fields with the given name and role."""
return [field for field in cls_or_self._fieldsattr()
if field.name == name and field.role == role]
@iclassmethod
def remove_field(cls_or_self, field):
- """remove a field from form class or instance"""
+ """Remove the given field."""
cls_or_self._fieldsattr().remove(field)
@iclassmethod
def append_field(cls_or_self, field):
- """append a field to form class or instance"""
+ """Append the given field."""
cls_or_self._fieldsattr().append(field)
@iclassmethod
- def insert_field_before(cls_or_self, new_field, name, role='subject'):
- field = cls_or_self.field_by_name(name, role)
+ def insert_field_before(cls_or_self, field, name, role=None):
+ """Insert the given field before the field of given name and role."""
+ bfield = cls_or_self.field_by_name(name, role)
fields = cls_or_self._fieldsattr()
- fields.insert(fields.index(field), new_field)
+ fields.insert(fields.index(bfield), field)
+
+ @iclassmethod
+ def insert_field_after(cls_or_self, field, name, role=None):
+ """Insert the given field after the field of given name and role."""
+ afield = cls_or_self.field_by_name(name, role)
+ fields = cls_or_self._fieldsattr()
+ fields.insert(fields.index(afield)+1, field)
@iclassmethod
- def insert_field_after(cls_or_self, new_field, name, role='subject'):
- field = cls_or_self.field_by_name(name, role)
- fields = cls_or_self._fieldsattr()
- fields.insert(fields.index(field)+1, new_field)
+ def add_hidden(cls_or_self, name, value=None, **kwargs):
+ """Append an hidden field to the form. `name`, `value` and extra keyword
+ arguments will be given to the field constructor. The inserted field is
+ returned.
+ """
+ kwargs.setdefault('ignore_req_params', True)
+ kwargs.setdefault('widget', fwdgs.HiddenInput)
+ field = formfields.StringField(name=name, value=value, **kwargs)
+ if 'id' in kwargs:
+ # by default, hidden input don't set id attribute. If one is
+ # explicitly specified, ensure it will be set
+ field.widget.setdomid = True
+ cls_or_self.append_field(field)
+ return field
def session_key(self):
"""return the key that may be used to store / retreive data about a