web/views/formrenderers.py
changeset 2006 78d5b57d4964
parent 2005 e8032965f37a
child 2093 e3aaf61f8c66
child 2103 41840b1a2a1a
equal deleted inserted replaced
2005:e8032965f37a 2006:78d5b57d4964
    67         form.add_media()
    67         form.add_media()
    68         data = []
    68         data = []
    69         w = data.append
    69         w = data.append
    70         w(self.open_form(form, values))
    70         w(self.open_form(form, values))
    71         if self.display_progress_div:
    71         if self.display_progress_div:
    72             w(u'<div id="progress">%s</div>' % form.req._('validating...'))
    72             w(u'<div id="progress">%s</div>' % self.req._('validating...'))
    73         w(u'<fieldset>')
    73         w(u'<fieldset>')
    74         w(tags.input(type=u'hidden', name=u'__form_id',
    74         w(tags.input(type=u'hidden', name=u'__form_id',
    75                      value=values.get('formvid', form.id)))
    75                      value=values.get('formvid', form.id)))
    76         if form.redirect_path:
    76         if form.redirect_path:
    77             w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path))
    77             w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path))
    83         if errormsg:
    83         if errormsg:
    84             data.insert(0, errormsg)
    84             data.insert(0, errormsg)
    85         return '\n'.join(data)
    85         return '\n'.join(data)
    86 
    86 
    87     def render_label(self, form, field):
    87     def render_label(self, form, field):
    88         label = form.req._(field.label)
    88         label = self.req._(field.label)
    89         attrs = {'for': form.context[field]['id']}
    89         attrs = {'for': form.context[field]['id']}
    90         if field.required:
    90         if field.required:
    91             attrs['class'] = 'required'
    91             attrs['class'] = 'required'
    92         return tags.label(label, **attrs)
    92         return tags.label(label, **attrs)
    93 
    93 
    94     def render_help(self, form, field):
    94     def render_help(self, form, field):
    95         help = []
    95         help = []
    96         descr = field.help
    96         descr = field.help
    97         if descr:
    97         if descr:
    98             help.append('<div class="helper">%s</div>' % form.req._(descr))
    98             help.append('<div class="helper">%s</div>' % self.req._(descr))
    99         example = field.example_format(form.req)
    99         example = field.example_format(self.req)
   100         if example:
   100         if example:
   101             help.append('<div class="helper">(%s: %s)</div>'
   101             help.append('<div class="helper">(%s: %s)</div>'
   102                         % (form.req._('sample format'), example))
   102                         % (self.req._('sample format'), example))
   103         return u'&nbsp;'.join(help)
   103         return u'&nbsp;'.join(help)
   104 
   104 
   105     # specific methods (mostly to ease overriding) #############################
   105     # specific methods (mostly to ease overriding) #############################
   106 
   106 
   107     def error_message(self, form):
   107     def error_message(self, form):
   108         """return formatted error message
   108         """return formatted error message
   109 
   109 
   110         This method should be called once inlined field errors has been consumed
   110         This method should be called once inlined field errors has been consumed
   111         """
   111         """
   112         req = form.req
   112         req = self.req
   113         errex = form.form_valerror
   113         errex = form.form_valerror
   114         # get extra errors
   114         # get extra errors
   115         if errex is not None:
   115         if errex is not None:
   116             errormsg = req._('please correct the following errors:')
   116             errormsg = req._('please correct the following errors:')
   117             displayed = form.form_displayed_errors
   117             displayed = form.form_displayed_errors
   136         if form.form_needs_multipart:
   136         if form.form_needs_multipart:
   137             enctype = 'multipart/form-data'
   137             enctype = 'multipart/form-data'
   138         else:
   138         else:
   139             enctype = 'application/x-www-form-urlencoded'
   139             enctype = 'application/x-www-form-urlencoded'
   140         if form.action is None:
   140         if form.action is None:
   141             action = form.req.build_url('edit')
   141             action = self.req.build_url('edit')
   142         else:
   142         else:
   143             action = form.action
   143             action = form.action
   144         tag = ('<form action="%s" method="post" enctype="%s"' % (
   144         tag = ('<form action="%s" method="post" enctype="%s"' % (
   145             html_escape(action or '#'), enctype))
   145             html_escape(action or '#'), enctype))
   146         if form.domid:
   146         if form.domid:
   296                 w(u'</td>')
   296                 w(u'</td>')
   297         else:
   297         else:
   298             # main form, display table headers
   298             # main form, display table headers
   299             w(u'<tr class="header">')
   299             w(u'<tr class="header">')
   300             w(u'<th align="left">%s</th>'
   300             w(u'<th align="left">%s</th>'
   301               % tags.input(type='checkbox', title=form.req._('toggle check boxes'),
   301               % tags.input(type='checkbox', title=self.req._('toggle check boxes'),
   302                            onclick="setCheckboxesState('eid', this.checked)"))
   302                            onclick="setCheckboxesState('eid', this.checked)"))
   303             for field in self.forms[0].fields:
   303             for field in self.forms[0].fields:
   304                 if self.display_field(form, field) and field.is_visible():
   304                 if self.display_field(form, field) and field.is_visible():
   305                     w(u'<th>%s</th>' % form.req._(field.label))
   305                     w(u'<th>%s</th>' % self.req._(field.label))
   306         w(u'</tr>')
   306         w(u'</tr>')
   307 
   307 
   308 
   308 
   309 class EntityFormRenderer(FormRenderer):
   309 class EntityFormRenderer(FormRenderer):
   310     """specific renderer for entity edition form (edition)"""
   310     """specific renderer for entity edition form (edition)"""
   317         rendered = super(EntityFormRenderer, self).render(form, values)
   317         rendered = super(EntityFormRenderer, self).render(form, values)
   318         return rendered + u'</div>' # close extra div introducted by open_form
   318         return rendered + u'</div>' # close extra div introducted by open_form
   319 
   319 
   320     def open_form(self, form, values):
   320     def open_form(self, form, values):
   321         attrs_fs_label = ('<div class="iformTitle"><span>%s</span></div>'
   321         attrs_fs_label = ('<div class="iformTitle"><span>%s</span></div>'
   322                           % form.req._('main informations'))
   322                           % self.req._('main informations'))
   323         attrs_fs_label += '<div class="formBody">'
   323         attrs_fs_label += '<div class="formBody">'
   324         return attrs_fs_label + super(EntityFormRenderer, self).open_form(form, values)
   324         return attrs_fs_label + super(EntityFormRenderer, self).open_form(form, values)
   325 
   325 
   326     def render_fields(self, w, form, values):
   326     def render_fields(self, w, form, values):
   327         super(EntityFormRenderer, self).render_fields(w, form, values)
   327         super(EntityFormRenderer, self).render_fields(w, form, values)
   350 
   350 
   351     def relations_form(self, w, form):
   351     def relations_form(self, w, form):
   352         srels_by_cat = form.srelations_by_category('generic', 'add')
   352         srels_by_cat = form.srelations_by_category('generic', 'add')
   353         if not srels_by_cat:
   353         if not srels_by_cat:
   354             return u''
   354             return u''
   355         req = form.req
   355         req = self.req
   356         _ = req._
   356         _ = req._
   357         label = u'%s :' % _('This %s' % form.edited_entity.e_schema).capitalize()
   357         label = u'%s :' % _('This %s' % form.edited_entity.e_schema).capitalize()
   358         eid = form.edited_entity.eid
   358         eid = form.edited_entity.eid
   359         w(u'<fieldset class="subentity">')
   359         w(u'<fieldset class="subentity">')
   360         w(u'<legend class="iformTitle">%s</legend>' % label)
   360         w(u'<legend class="iformTitle">%s</legend>' % label)
   369                     w(u'<li class="invisible">%s<div id="span%s" class="%s">%s</div></li>'
   369                     w(u'<li class="invisible">%s<div id="span%s" class="%s">%s</div></li>'
   370                       % (viewparams[1], viewparams[0], viewparams[2], viewparams[3]))
   370                       % (viewparams[1], viewparams[0], viewparams[2], viewparams[3]))
   371                 if not form.force_display and form.maxrelitems < len(related):
   371                 if not form.force_display and form.maxrelitems < len(related):
   372                     link = (u'<span class="invisible">'
   372                     link = (u'<span class="invisible">'
   373                             '[<a href="javascript: window.location.href+=\'&amp;__force_display=1\'">%s</a>]'
   373                             '[<a href="javascript: window.location.href+=\'&amp;__force_display=1\'">%s</a>]'
   374                             '</span>' % form.req._('view all'))
   374                             '</span>' % self.req._('view all'))
   375                     w(u'<li class="invisible">%s</li>' % link)
   375                     w(u'<li class="invisible">%s</li>' % link)
   376                 w(u'</ul>')
   376                 w(u'</ul>')
   377                 w(u'</td>')
   377                 w(u'</td>')
   378                 w(u'</tr>')
   378                 w(u'</tr>')
   379         pendings = list(form.restore_pending_inserts())
   379         pendings = list(form.restore_pending_inserts())
   411     def inline_entities_form(self, w, form):
   411     def inline_entities_form(self, w, form):
   412         """create a form to edit entity's inlined relations"""
   412         """create a form to edit entity's inlined relations"""
   413         if not hasattr(form, 'inlined_relations'):
   413         if not hasattr(form, 'inlined_relations'):
   414             return
   414             return
   415         entity = form.edited_entity
   415         entity = form.edited_entity
   416         __ = form.req.__
   416         __ = self.req.__
   417         for rschema, targettypes, role in form.inlined_relations():
   417         for rschema, targettypes, role in form.inlined_relations():
   418             # show inline forms only if there's one possible target type
   418             # show inline forms only if there's one possible target type
   419             # for rschema
   419             # for rschema
   420             if len(targettypes) != 1:
   420             if len(targettypes) != 1:
   421                 self.warning('entity related by the %s relation should have '
   421                 self.warning('entity related by the %s relation should have '
   471             w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values)
   471             w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values)
   472         except KeyError:
   472         except KeyError:
   473             w(u'<div id="div-%(divid)s">' % values)
   473             w(u'<div id="div-%(divid)s">' % values)
   474         else:
   474         else:
   475             w(u'<div id="notice-%s" class="notice">%s</div>' % (
   475             w(u'<div id="notice-%s" class="notice">%s</div>' % (
   476                 values['divid'], form.req._('click on the box to cancel the deletion')))
   476                 values['divid'], self.req._('click on the box to cancel the deletion')))
   477         w(u'<div class="iformBody">')
   477         w(u'<div class="iformBody">')
   478         values['removemsg'] = form.req.__('remove this %s' % form.edited_entity.e_schema)
   478         values['removemsg'] = self.req.__('remove this %s' % form.edited_entity.e_schema)
   479         w(u'<div class="iformTitle"><span>%(title)s</span> '
   479         w(u'<div class="iformTitle"><span>%(title)s</span> '
   480           '#<span class="icounter">1</span> '
   480           '#<span class="icounter">1</span> '
   481           '[<a href="javascript: %(removejs)s;noop();">%(removemsg)s</a>]</div>'
   481           '[<a href="javascript: %(removejs)s;noop();">%(removemsg)s</a>]</div>'
   482           % values)
   482           % values)
   483         # cleanup values
   483         # cleanup values