web/formfields.py
branchstable
changeset 4871 a34b76593ab2
parent 4661 87672c718c3c
child 4874 344857257672
equal deleted inserted replaced
4870:101858d845f7 4871:a34b76593ab2
    12 from warnings import warn
    12 from warnings import warn
    13 from datetime import datetime
    13 from datetime import datetime
    14 
    14 
    15 from logilab.mtconverter import xml_escape
    15 from logilab.mtconverter import xml_escape
    16 from logilab.common.date import ustrftime
    16 from logilab.common.date import ustrftime
    17 from logilab.common.decorators import cached
       
    18 
    17 
    19 from yams.schema import KNOWN_METAATTRIBUTES
    18 from yams.schema import KNOWN_METAATTRIBUTES
    20 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint,
    19 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint,
    21                               FormatConstraint)
    20                               FormatConstraint)
    22 
    21 
   187 
   186 
   188     def get_widget(self, form):
   187     def get_widget(self, form):
   189         """return the widget instance associated to this field"""
   188         """return the widget instance associated to this field"""
   190         return self.widget
   189         return self.widget
   191 
   190 
   192     # cached is necessary else we get some pb on entity creation : entity.eid is
       
   193     # modified from creation mark (eg 'X') to its actual eid (eg 123), and then
       
   194     # `field.input_name()` won't return the right key anymore if not cached
       
   195     # (first call to input_name done *before* eventual eid affectation).
       
   196     @cached
       
   197     def input_name(self, form, suffix=None):
   191     def input_name(self, form, suffix=None):
   198         """return 'qualified name' for this field"""
   192         """return 'qualified name' for this field"""
   199         name = self.role_name()
   193         # caching is necessary else we get some pb on entity creation :
   200         if suffix is not None:
   194         # entity.eid is modified from creation mark (eg 'X') to its actual eid
   201             name += suffix
   195         # (eg 123), and then `field.input_name()` won't return the right key
   202         if self.eidparam:
   196         # anymore if not cached (first call to input_name done *before* eventual
   203             return eid_param(name, form.edited_entity.eid)
   197         # eid affectation).
   204         return name
   198         #
       
   199         # note that you should NOT use @cached else it will create a memory leak
       
   200         # on persistent fields (eg created once for all on a form class) because
       
   201         # of the 'form' appobject argument: the cache will keep growing as new
       
   202         # form are created...
       
   203         try:
       
   204             return form.formvalues[(self, 'input_name')]
       
   205         except KeyError:
       
   206             name = self.role_name()
       
   207             if suffix is not None:
       
   208                 name += suffix
       
   209             if self.eidparam:
       
   210                 name = eid_param(name, form.edited_entity.eid)
       
   211             form.formvalues[(self, 'input_name')] = name
       
   212             return name
   205 
   213 
   206     def role_name(self):
   214     def role_name(self):
   207         """return <field.name>-<field.role> if role is specified, else field.name"""
   215         """return <field.name>-<field.role> if role is specified, else field.name"""
   208         if self.role is not None:
   216         if self.role is not None:
   209             return '%s-%s' % (self.name, self.role)
   217             return '%s-%s' % (self.name, self.role)