web/form.py
branchtls-sprint
changeset 867 14a79faa3a56
parent 865 90a1aea85637
child 869 168ad6d424d1
equal deleted inserted replaced
866:6fdb029663ca 867:14a79faa3a56
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 from warnings import warn
     9 from warnings import warn
    10 from simplejson import dumps
    10 from simplejson import dumps
       
    11 from mx.DateTime import today
    11 
    12 
    12 from logilab.common.compat import any
    13 from logilab.common.compat import any
    13 from logilab.mtconverter import html_escape
    14 from logilab.mtconverter import html_escape
    14 
    15 
    15 from cubicweb import typed_eid
    16 from cubicweb import typed_eid
       
    17 from cubicweb.utils import ustrftime
    16 from cubicweb.selectors import match_form_params
    18 from cubicweb.selectors import match_form_params
    17 from cubicweb.view import NOINDEX, NOFOLLOW, View, EntityView, AnyRsetView
    19 from cubicweb.view import NOINDEX, NOFOLLOW, View, EntityView, AnyRsetView
    18 from cubicweb.common.registerers import accepts_registerer
    20 from cubicweb.common.registerers import accepts_registerer
    19 from cubicweb.web import stdmsgs
    21 from cubicweb.web import stdmsgs
    20 from cubicweb.web.httpcache import NoHTTPCacheManager
    22 from cubicweb.web.httpcache import NoHTTPCacheManager
    21 from cubicweb.web.controller import redirect_params
    23 from cubicweb.web.controller import redirect_params
    22 from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param
    24 from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param
    23 
       
    24 
    25 
    25 
    26 
    26 def relation_id(eid, rtype, target, reid):
    27 def relation_id(eid, rtype, target, reid):
    27     if target == 'subject':
    28     if target == 'subject':
    28         return u'%s:%s:%s' % (eid, rtype, reid)
    29         return u'%s:%s:%s' % (eid, rtype, reid)
   345         req.html_headers.define_var('MONTHNAMES', monthnames)
   346         req.html_headers.define_var('MONTHNAMES', monthnames)
   346         req.html_headers.define_var('DAYNAMES', daynames)
   347         req.html_headers.define_var('DAYNAMES', daynames)
   347     
   348     
   348     def render(self, form, field):
   349     def render(self, form, field):
   349         txtwidget = super(DateTimePicker, self).render(form, field)
   350         txtwidget = super(DateTimePicker, self).render(form, field)
   350         cal_button - self._render_calendar_popup(form, field)
   351         cal_button = self._render_calendar_popup(form, field)
   351         return txtwidget + cal_button
   352         return txtwidget + cal_button
   352     
   353     
   353     def _render_calendar_popup(self, form, field):
   354     def _render_calendar_popup(self, form, field):
   354         req = form.req
   355         req = form.req
   355         name, value, attrs = self._render_attrs(form, field)
   356         value = form.context[field]['rawvalue']
   356         helperid = '%shelper' % name
   357         inputid = form.context[field]['id']
       
   358         helperid = '%shelper' % inputid
   357         if not value:
   359         if not value:
   358             value = _today()
   360             value = today()
   359         year, month = value.year, value.month
   361         year, month = value.year, value.month
   360         onclick = "toggleCalendar('%s', '%s', %s, %s);" % (
   362         onclick = "toggleCalendar('%s', '%s', %s, %s);" % (
   361             helperid, name, year, month)
   363             helperid, inputid, year, month)
   362         return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
   364         return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
   363 <img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
   365 <img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
   364                 % (helperid, inputid, year, month,
   366                 % (helperid, inputid, year, month,
   365                    self.iconurl, req._('calendar'), helperid) )
   367                    req.external_resource('CALENDAR_ICON'),
       
   368                    req._('calendar'), helperid) )
   366 
   369 
   367 
   370 
   368 # fields ############
   371 # fields ############
   369 
   372 
   370 class Field(object):
   373 class Field(object):
   503         # on validation error, we get a dictionnary of previously submitted values
   506         # on validation error, we get a dictionnary of previously submitted values
   504         previous_values = self.req.data.get('formvalues')
   507         previous_values = self.req.data.get('formvalues')
   505         if previous_values:
   508         if previous_values:
   506             values.update(previous_values)
   509             values.update(previous_values)
   507         for field in self.fields:
   510         for field in self.fields:
   508             context[field] = {'value': self.form_field_value(field, values),
   511             value = self.form_field_value(field, values)
       
   512             context[field] = {'value': field.format_value(self.req, value),
       
   513                               'rawvalue': value,
   509                               'name': self.form_field_name(field),
   514                               'name': self.form_field_name(field),
   510                               'id': self.form_field_id(field),
   515                               'id': self.form_field_id(field),
   511                               }
   516                               }
   512 
   517 
   513     def form_field_value(self, field, values):
   518     def form_field_value(self, field, values):
   521             value = values[field.name]
   526             value = values[field.name]
   522         elif field.name in self.req.form:
   527         elif field.name in self.req.form:
   523             value = self.req.form[field.name]
   528             value = self.req.form[field.name]
   524         else:
   529         else:
   525             value = field.initial
   530             value = field.initial
   526         return field.format_value(self.req, value) 
   531         return value # field.format_value(self.req, value) 
   527 
   532 
   528     def form_field_name(self, field):
   533     def form_field_name(self, field):
   529         return field.name
   534         return field.name
   530 
   535 
   531     def form_field_id(self, field):
   536     def form_field_id(self, field):
   638                 else:
   643                 else:
   639                     # use field's initial value
   644                     # use field's initial value
   640                     value = field.initial
   645                     value = field.initial
   641             if callable(value):
   646             if callable(value):
   642                 values = value()
   647                 values = value()
   643         return field.format_value(self.req, value) 
   648         return value # field.format_value(self.req, value) 
   644 
   649 
   645     def form_field_name(self, field):
   650     def form_field_name(self, field):
   646         if field.eidparam:
   651         if field.eidparam:
   647             return eid_param(field.name, self.entity.eid)
   652             return eid_param(field.name, self.entity.eid)
   648         return field.name
   653         return field.name