[Mpuls-commits] r4599 - in base/trunk: . mpulsweb/controllers mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Feb 9 19:05:09 CET 2011


Author: bh
Date: 2011-02-09 19:05:08 +0100 (Wed, 09 Feb 2011)
New Revision: 4599

Added:
   base/trunk/mpulsweb/lib/formedrenderer.py
Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/controllers/formularpage.py
Log:
Switch to libformed's renderer. Requires libformed >= 343:4d20baece0e5

* mpulsweb/controllers/formularpage.py (get_rendered_page): Use
the renderers from libformed and formedrenderer.py. As part of
this change, htmlfill is not needed anymore here.

* mpulsweb/lib/formedrenderer.py: New. Customizations of the
libformed renderers for the web application.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2011-02-09 17:12:40 UTC (rev 4598)
+++ base/trunk/ChangeLog	2011-02-09 18:05:08 UTC (rev 4599)
@@ -1,5 +1,16 @@
 2011-02-09  Bernhard Herzog  <bh at intevation.de>
 
+	Switch to libformed's renderer. Requires libformed > 343:4d20baece0e5
+
+	* mpulsweb/lib/formedrenderer.py: New. Customizations of the
+	libformed renderers for the web application.
+
+	* mpulsweb/controllers/formularpage.py (get_rendered_page): Use
+	the renderers from libformed and formedrenderer.py. As part of
+	this change, htmlfill is not needed anymore here.
+
+2011-02-09  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/controllers/formularpage.py (get_rendered_page): Remove
 	superfluous assignment to c.formularerrors.  It will always be set
 	to a different value later.

Modified: base/trunk/mpulsweb/controllers/formularpage.py
===================================================================
--- base/trunk/mpulsweb/controllers/formularpage.py	2011-02-09 17:12:40 UTC (rev 4598)
+++ base/trunk/mpulsweb/controllers/formularpage.py	2011-02-09 18:05:08 UTC (rev 4599)
@@ -1,32 +1,29 @@
 # -*- coding: utf-8 -*-
 
 import logging
-from cgi import escape
 import datetime
-import itertools
 
-import formencode
-
 from pylons import request, c, g, h
 from pylons.controllers.util import abort, redirect_to
+from pylons.i18n.translation import get_lang
 
-from formed.instance.backends.postgres import DBFactory as InstanceFactory, \
-     DBRepeatInstanceNode as RepeatInstanceNode
+from formed.instance.backends.postgres import DBFactory as InstanceFactory
 from formed.instance.tree import SetDataException, RepeatInstanceContainerNode
+from formed.renderer.html import apply_hidden_booleans, RepeatGroupRenderer, \
+     ErrorRenderer, TreeFormData
 
-
 from mpulsweb.lib.translation import _
 from mpulsweb.lib.security import checkRole
 from mpulsweb.lib.db import  PostgresDBInterface
 from mpulsweb.lib.navigation import  mark_folderopen
 
 from mpulsweb.lib.base import BaseController, render, session
-from mpulsweb.lib.renderer import ViewRenderer, RepeatGroupRenderer, \
-     ErrorRenderer, apply_hidden_booleans, get_child_num, icon
-from mpulsweb.lib.helpers import url_for, dd_mm_YYYY as format_date, tag
+from mpulsweb.lib.helpers import url_for, dd_mm_YYYY as format_date
 from mpulsweb.model.phase import PhaseFactory, PC_MANUAL, PC_FULLAUTO
 
+from mpulsweb.lib.formedrenderer import ViewRenderer, UrlFactory
 
+
 log = logging.getLogger(__name__)
 
 
@@ -87,78 +84,44 @@
 
     page_label = page.getMeta().getDescription()
 
-    # Check if there is help available for this page
-    help = []
-    if page.getMeta().hasHelp():
-        help.append(tag("a", target="_blank",
-                       href=url_for(controller="/annotations",
-                                    action="help", id=page.getMeta().getName())))
-        help.append(tag("img", src=icon("formular/help.png"), alt="help",
-                    width=12,
-                    height=12))
-        help.append('</a>')
-
-    if isinstance(page.getParent(), RepeatInstanceNode):
-        page_label += " %s" % get_child_num(page.getParent())
-    c.formularheader = ("<h1>%s %s</h1>" % (escape(page_label), "".join(help)))
+    url_factory = UrlFactory()
+    renderer_kw_args = dict(read_only=session.get('render_mode', 'ro') == 'ro',
+                            languages=get_lang())
     if isinstance(page, RepeatInstanceContainerNode):
-        renderer = RepeatGroupRenderer(instance_tree, page,
-                                       session.get('render_mode',
-                                                   'ro') == 'ro',
-                                       g.mpuls_config.get("renderer",
-                                                          "rg_digests"))
+        formdata = TreeFormData(instance_tree)
+        renderer = RepeatGroupRenderer(formdata, page.getMeta(), url_factory,
+                                       digests=g.mpuls_config.get("renderer",
+                                                                  "rg_digests"),
+                                       **renderer_kw_args)
         errors = {}
         defaults = {}
     else:
-        renderer = ViewRenderer(instance_tree, page,
-                                session.get('render_mode', 'ro') == 'ro')
         errors, defaults = convert_error_items(page.getErrors())
-    try:
-        mark_folderopen(session['navigation.tree'].getTreeItem(id))
-        if session['navigation.tree'].getTreeItem(id).nextKey():
-            c.formular = renderer.render(showNext=True)
-        else:
-            c.formular = renderer.render(showNext=False)
-    except Exception, e:
-        log.exception(e)
-    error_renderer = ErrorRenderer(errors, renderer.warnings,
-                                   renderer.notices, renderer.widget_order)
-    c.formularerrors = error_renderer.render(page)
+        formdata = TreeFormData(instance_tree, shadow=defaults, errors=errors,
+                                node=page)
+        renderer = ViewRenderer(formdata, page.getMeta(), url_factory,
+                                **renderer_kw_args)
+
+    navigation_item = session['navigation.tree'].getTreeItem(id)
+    mark_folderopen(navigation_item)
+
+    c.formularheader = renderer.render_page_headings()
+    c.formular = renderer.render(show_next=navigation_item.nextKey(),
+                                 hidden_fields=[("page", page.getIdentifier())])
+
+    error_renderer = ErrorRenderer(formdata, g.formedTree, errors,
+                                   renderer.warnings, renderer.notices,
+                                   renderer.widget_order, url_factory,
+                                   languages=get_lang())
+    c.formularerrors = error_renderer.render()
     c.page_id = page.getIdentifier()
     session['case'].setPageId(c.page_id)
     session['navigation.selectedpage'] = id
     session.save()
 
-    # Render form and use htmlfill to fillout the form and mark errorfields.
-    form = render('/formularpage/content.mako')
-    # Currently values of the fields are set within the renderer.
-    # Htmlfill is used to fill in the error values from the error items.
-    defaults = prepare_htmlfill_defaults(defaults)
+    return render('/formularpage/content.mako')
 
-    # Parse the real errors
-    parsedForm = formencode.htmlfill.render(form, defaults=defaults,
-                                            errors=errors,
-                                            auto_insert_errors=False,
-                                            force_defaults=False)
 
-    # Replace "form:warnings" with "form:errors" so that htmlfill understands it
-    parsedForm = parsedForm.replace('form:warning', 'form:error')
-
-    # make a new dictionary because the warnings have the warning text in a (one
-    #element) list
-    warningList = {}
-    for key, value in itertools.chain(renderer.warnings.iteritems(),
-                                      renderer.notices.iteritems()):
-        warningList[key] = value[0]
-
-    # Let htmlfill parse the form again
-    parsedForm = formencode.htmlfill.render(parsedForm, errors=warningList,
-                                            error_class='warning',
-                                            auto_insert_errors=False,
-                                            force_defaults=False)
-    return parsedForm
-
-
 class FormularpageController(BaseController):
 
     def index(self):

Added: base/trunk/mpulsweb/lib/formedrenderer.py
===================================================================
--- base/trunk/mpulsweb/lib/formedrenderer.py	2011-02-09 17:12:40 UTC (rev 4598)
+++ base/trunk/mpulsweb/lib/formedrenderer.py	2011-02-09 18:05:08 UTC (rev 4599)
@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2007, 2008, 2011 Intevation GmbH, Germany, <info at intevation.de>
+#
+# This file is part of mpuls WASKA (CoMPUter-based case fiLeS -
+# Web-Anwendungs-Server fuer Kompetenzagenturen).
+#
+# mpuls WASKA is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the
+# Free Software Foundation, either version 3 of the License, or (at your
+# option) any later version.
+#
+# mpuls WASKA 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 Affero General Public
+# License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with mpuls WASKA. If not, see <http://www.gnu.org/licenses/>.
+#
+# mpuls WASKA has been developed on behalf of the
+# Projekttraeger im Deutschen Zentrum fuer Luft- und Raumfahrt e.V. (PT-DLR)
+# within the programme Kompetenzagenturen (Durchfuehrungsphase) funded by
+# the Bundesministerium fuer Familie, Senioren, Frauen und Jugend and
+# European Social Fund resources.
+#
+# Authors:
+# Bernhard Herzog <bernhard.herzog at intevation.de>
+#
+
+from formed.renderer.html import HtmlRenderer, UrlFactory as BaseUrlFactory
+from formed.renderer.helpers import tag
+
+from mpulsweb.lib.helpers import url_for
+from mpulsweb.lib.renderer import icon
+
+
+class UrlFactory(BaseUrlFactory):
+
+    def url_for(self, controller, action, id=None):
+        return url_for(controller=controller, action=action, id=id)
+
+    def icon(self, name):
+        return icon(name)
+
+
+class ViewRenderer(HtmlRenderer):
+
+    def render_errors(self, context, nc):
+        messages = self.data.get_errors(nc)
+        if not messages:
+            messages = self.get_warnings(nc)
+
+        out = []
+        if messages:
+            out.extend([tag("span", class_=self.error_css_class(context, nc)),
+                        ", ".join(messages),
+                        "</span>",
+                        ])
+        return out


Property changes on: base/trunk/mpulsweb/lib/formedrenderer.py
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native



More information about the Mpuls-commits mailing list