Hi!


I've been talking to Roger about a problem I was having with some
AJAX things to allow editing a complex form I'm creating for an
application.

The thing that was causing me more problems was filling fields that
depend on previous values to get their own values, a kind of cascade of
options.  This happens when I'm editing existing records and I need to
display data to my users.

I tried to populate those with known values before displaying the form,
to avoid unneeded roundtrips from the client's machine to the server to
get values that I know that should be on the form.

What I did was:

================================================================================
In [1]: import model

In [2]: from clientes.controllers.contatos import Contatos

In [3]: from neobh.controllers.tecnico.formularios import 
formulario_planilha_dados

In [4]: from clientes.controllers.formularios import clientes

In [5]: fpd = 
formulario_planilha_dados(template="neobh.templates.planilha_dados_template")

In [6]: fpd.render()
Out[6]: '<div>\n  <form method="post" name="planilhas_dados">\n      <input 
class="hiddenfield" type="hidden" id="id" name="id">\n    <p>\n      <label 
class="fieldlabel" for="tipoServico">\n        Tipo de Servi\xc3\xa7o\n      
</label>\n      <ul class="checkboxlist" id="tipoServico">\n        <li>\n      
      <input type="checkbox" name="tipoServico" value="18" 
id="tipoServico_18">\n            <label for="tipoServico_18">Auditor (Follow 
up) - GMB</label>\n        </li><li>\n            <input type="checkbox" 
name="tipoServico" value="5" id="tipoServico_5">\n

(...)

In [7]: clientes()
Out[7]: 
[[60, u'AAM'],
 [88, u'Abc Group'],
 [55, u'Alcoa Alum\xednios'],
 [11, u'Alusur Fundi\xe7\xe3o'],
 [134, u'A. Pedro Ind. Metal\xfargica '],
 [84, u'Arniflex Artigos de Borracha'],

(...)

In [8]: Contatos.get_contatos(1)
Out[8]: 
{'contatos': [(52, u'Ant\xf4nio Gimenes Jr.'),
              (54, u'Rog\xe9rio Milleo Pitella'),
              (59, u'Eduardo Sousa'),
              (58, u'Sebasti\xe3o E.Martins B. Jr.'),
              (128, u'Amauri')]}

================================================================================

But then, when I change one field's options the form stops rendering:

================================================================================
In [9]: fpd.get_field_by_name('contatoClienteBeneficiado')
Out[9]: SingleSelectFieldAddSubModal(name='contatoClienteBeneficiado', 
convert=False, target=None, width=810, 
text=<turbogears.i18n.tg_gettext.lazystring object at 0xa01616c>, height=510, 
callback="function () {signal('planilhas_dados_clienteBeneficiado', 
'onchange');}", link='/clientes/contatos/add/', attrs={}, css_classes=[], 
field_class='singleselectfieldaddsubmodal', options=[])
================================================================================

What I needed here was some mean to replace the empty list of options
with a real list of options that is valid in this context.

I can replace the options using AJAX, but this would add the undesirable
roundtrip plus some other logic that I'd like to avoid if I can.
(Specially on setting the selected option...)


I've tried this:

================================================================================
In [10]: fpd.get_field_by_name('contatoClienteBeneficiado').options[:]=[]

In [11]: 
fpd.get_field_by_name('contatoClienteBeneficiado').options.append(Contatos.get_contatos(1))
================================================================================

Rendering the form I get the following traceback:

================================================================================
File 
'/home/godoy/empresa/clientes/bhquality/NeoBH/trunk/Neo-BH/neobh/templates/planilhas_dados.html',
 line 25 in <Expression u"ET(formulario_planilha_dados(planilha_dados,          
               action="/tecnico/planilhas_dados/update/",                       
  method="post"))">
  <span py:replace="ET(formulario_planilha_dados(planilha_dados,
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/base.py', 
line 226 in __call__
  return self.display(*args, **params)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/base.py', 
line 352 in display
  return super(CompoundWidget, self).display(value, **params)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 51 in _update_path
  returnval = func(self, *args, **kw)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 236 in display
  return super(InputWidget, self).display(value, **params)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/base.py', 
line 264 in display
  return view.engines.get('kid').transform(params, self.template_c)
File 
'/usr/local/lib/python2.5/site-packages/TurboKid-0.9.9-py2.5.egg/turbokid/kidsupport.py',
 line 191 in transform
  return ElementStream(t.transform()).expand()
File 
'/usr/local/lib/python2.5/site-packages/kid-0.9.5-py2.5.egg/kid/parser.py', 
line 106 in expand
  for ev, item in self._iter:
File 
'/usr/local/lib/python2.5/site-packages/kid-0.9.5-py2.5.egg/kid/parser.py', 
line 177 in _track
  for p in stream:
File 
'/usr/local/lib/python2.5/site-packages/kid-0.9.5-py2.5.egg/kid/parser.py', 
line 177 in _track
  for p in stream:
File 
'/usr/local/lib/python2.5/site-packages/kid-0.9.5-py2.5.egg/kid/filter.py', 
line 24 in apply_matches
  for ev, item in stream:
File 
'/usr/local/lib/python2.5/site-packages/kid-0.9.5-py2.5.egg/kid/parser.py', 
line 177 in _track
  for p in stream:
File 
'/usr/local/lib/python2.5/site-packages/kid-0.9.5-py2.5.egg/kid/parser.py', 
line 219 in _coalesce
  for ev, item in stream:
File 
'/home/godoy/empresa/clientes/bhquality/NeoBH/trunk/Neo-BH/neobh/templates/planilha_dados_template.py',
 line 175 in _pull
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 543 in <lambda>
  **d["params_for"](f))
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 51 in _update_path
  returnval = func(self, *args, **kw)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 470 in _retrieve_field_for
  returnval = func(self, field, *args, **kw)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 523 in display_field_for
  return item.display(value, **params)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 51 in _update_path
  returnval = func(self, *args, **kw)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 236 in display
  return super(InputWidget, self).display(value, **params)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/base.py', 
line 261 in display
  self.update_params(params)
File 
'/home/godoy/desenvolvimento/python/TurboGears/1.0/turbogears/widgets/forms.py',
 line 908 in update_params
  optlist[i] = (option[0], option[1], option_attrs)
UnboundLocalError: ("local variable 'option_attrs' referenced before 
assignment", <bound method PlanilhasDados.edit of 
<neobh.controllers.tecnico.planilhas_dados.PlanilhasDados object at 0x9f3a96c>>)

================================================================================


With the WSGI inspector I can see the following at the traceback point:

================================================================================
d       {'attrs': {}, 'callback': "function () 
{signal('planilhas_dados_clienteBeneficiado', 'onchange...');}", 'convert': 
False, 'css_classes': [], 'error': None, 'field_class': 
'singleselectfieldaddsubmodal requiredfield', 'field_id': 
'planilhas_dados_contatoClienteBeneficiado', 'height': 510, 'help_text': None, 
'label': <turbogears.i18n.tg_gettext.lazystring object at 0x9ea074c>, 'link': 
'/clientes/contatos/add/', 'name': 'contatoClienteBeneficiado', 'options': 
[[(52, u'Ant\xf4nio Gimenes Jr.'), (54, u'Rog\xe9rio Milleo Pitella'), (59, 
u'Eduardo Sousa'), (58, u'Sebasti\xe3o E.Martins B. Jr.'), (128, u'Amauri')]], 
'target': None, 'text': <turbogears.i18n.tg_gettext.lazystring object at 
0x9ea068c>, 'value': 54, 'width': 810}         
group   False
        
grouped_options []
        
i       0
        
optgroup        [(52, u'Ant\xf4nio Gimenes Jr.'), (54, u'Rog\xe9rio Milleo 
Pitella'), (59, u'Eduardo Sousa'), (58..., u'Sebasti\xe3o E.Martins B. Jr.'), 
(128, u'Amauri')]      
option  [(52, u'Ant\xf4nio Gimenes Jr.'), (54, u'Rog\xe9rio Milleo Pitella'), 
(59, u'Eduardo Sousa'), (58..., u'Sebasti\xe3o E.Martins B. Jr.'), (128, 
u'Amauri')]      
options []
        
optlist [[(52, u'Ant\xf4nio Gimenes Jr.'), (54, u'Rog\xe9rio Milleo Pitella'), 
(59, u'Eduardo Sousa'), ... (58, u'Sebasti\xe3o E.Martins B. Jr.'), (128, 
u'Amauri')]]   
self    SingleSelectFieldAddSubModal(name='contatoClienteBeneficiado', 
convert=False, target=None, width=810..., 
text=<turbogears.i18n.tg_gettext.lazystring object at 0x9ea068c>, height=510, 
callback="function () {signal('planilhas_dados_clienteBeneficiado', 
'onchange');}", link='/clientes/contatos/add/', attrs={}, css_classes=[], 
field_class='singleselectfieldaddsubmodal', options=[[(52, u'Ant\xf4nio Gimenes 
Jr.'), (54, u'Rog\xe9rio Milleo Pitella'), (59, u'Eduardo Sousa'), (58, 
u'Sebasti\xe3o E.Martins B. Jr.'), (128, u'Amauri')]])     

self    SingleSelectFieldAddSubModal(name='contatoClienteBeneficiado', 
convert=False, target=None, width=810..., 
text=<turbogears.i18n.tg_gettext.lazystring object at 0x9ea068c>, height=510, 
callback="function () {signal('planilhas_dados_clienteBeneficiado', 
'onchange');}", link='/clientes/contatos/add/', attrs={}, css_classes=[], 
field_class='singleselectfieldaddsubmodal', options=[[(52, u'Ant\xf4nio Gimenes 
Jr.'), (54, u'Rog\xe9rio Milleo Pitella'), (59, u'Eduardo Sousa'), (58, 
u'Sebasti\xe3o E.Martins B. Jr.'), (128, u'Amauri')]])

>>  optlist[i] = (option[0], option[1], option_attrs)

UnboundLocalError: ("local variable 'option_attrs' referenced before 
assignment", <bound method PlanilhasDados.edit of 
<neobh.controllers.tecnico.planilhas_dados.PlanilhasDados object at 0x9f3a96c>>)
================================================================================


This works on IPython and fails on my browser...  If I stop changing
options like this then the form renders perfectly.


Is it really not allowed?  Is it possible to do what I want?  If so, any
hints on how could I achieve that?



-- 
Jorge Godoy      <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears Trunk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to