[views] use the resourcepicker cube for blogentries draft
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 04 Nov 2013 16:26:01 +0100
changeset 320 7d87fc336b90
parent 316 70bd41a6e80a
[views] use the resourcepicker cube for blogentries This is a bit preliminary.
__pkginfo__.py
views/entry.py
--- a/__pkginfo__.py	Tue Aug 06 11:00:24 2013 +0200
+++ b/__pkginfo__.py	Mon Nov 04 16:26:01 2013 +0100
@@ -28,6 +28,7 @@
                   'cubicweb-seo': None,
                   'feedparser': None,
                   'rdflib': None,
+                  'cubicweb-resourcepicker': None
                   }
 
 
--- a/views/entry.py	Tue Aug 06 11:00:24 2013 +0200
+++ b/views/entry.py	Mon Nov 04 16:26:01 2013 +0100
@@ -1,7 +1,7 @@
 """Secondary views for blogs
 
 :organization: Logilab
-:copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:copyright: 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
 __docformat__ = "restructuredtext en"
@@ -13,10 +13,13 @@
 
 from logilab.mtconverter import xml_escape
 
+from cubicweb.utils import make_uid
+
 from cubicweb.schema import display_name
 from cubicweb.view import EntityView
 from cubicweb.predicates import paginated_rset, sorted_rset, is_instance
-from cubicweb.web.views import uicfg
+from cubicweb.web import formfields
+from cubicweb.web.views import uicfg, formrenderers
 from cubicweb.web.views import primary, baseviews, calendar, navigation, workflow
 from cubicweb.web.views.xmlrss import RSSItemView
 
@@ -252,3 +255,61 @@
         htmlcontent = entity.dc_description(format='text/html')
         htmlcontent = _CLEAN_STYLE_RE.sub('', htmlcontent)
         self._marker('description', htmlcontent)
+
+
+class BlogentryFormRenderer(formrenderers.EntityFormRenderer):
+    __select__ = formrenderers.EntityFormRenderer.__select__ & is_instance('BlogEntry')
+
+    # patches cubes/bootstrap/views/formrenderers.py
+    def _render_fields(self, fields, w, form):
+        """render form fields """
+        byfieldset = {}
+        for field in fields:
+            byfieldset.setdefault(field.fieldset, []).append(field)
+        if form.fieldsets_in_order:
+            fieldsets = form.fieldsets_in_order
+        else:
+            fieldsets = byfieldset.keys()
+        for fieldset in fieldsets:
+            try:
+                fields = byfieldset.pop(fieldset)
+            except KeyError:
+                self.warning('no such fieldset: %s (%s)', fieldset, form)
+                continue
+            w(u'<fieldset class="%s">' % (fieldset or u'default'))
+            if fieldset:
+                w(u'<legend>%s</legend>' % self._cw._(fieldset))
+            for field in fields:
+                error = form.field_error(field)
+                control = not hasattr(field, 'control_field') or field.control_field
+                w(u'<div class="control-group %s-%s_row %s">' % (field.name,
+                                                                 field.role,
+                                                                 'error' if error else ''))
+                if self.display_label and field.label is not None:
+                    w(u'%s' % self.render_label(form, field))
+                if control:
+                    w(u'<div class="controls">')
+                else:
+                    w(u'<div class="controls no-margin">')
+                # PATCH
+                if isinstance(field, formfields.RichTextField):
+                    textareawrapid = make_uid('textarea')
+                    picker = form._cw.vreg['views'].select('resourcepicker', form._cw,
+                                                           textareawrapid=textareawrapid,
+                                                           widgetuid=make_uid('resourcepicker'))
+                    picker.render(w=w)
+                    w(u'<div id="%s">' % textareawrapid)
+                    w(field.render(form, self))
+                    w(u'</div')
+                    # /PATCH
+                else:
+                    w(field.render(form, self))
+                if error:
+                    self.render_error(w, error)
+                if self.display_help:
+                    w(self.render_help(form, field))
+                w(u'</div>')
+                w(u'</div>')
+            w(u'</fieldset>')
+        if byfieldset:
+            self.warning('unused fieldsets: %s', ', '.join(byfieldset))