Il 14/03/2012 18:09, Alan Etkin ha scritto:
You can create a widget function that creates the original widget and
returns it modified:
def mycustomwidget():
new_widget = SQLFORM.widgets.autocomplete(request,
db.category.name, limitby=(0,10), min_length=2)
... # modify the helper
return new_widget
db.mytable.myfield.widget = mycustomwidget
Other (better I think) way is to inherit the widget classes in a
custom class
class mycustomwidget(SQFFORM.widgets.widgetname):
# method overriding code
....
I tryed this second way but without success maybe because of different
situations in which request is called in the same code line of sqlhtml.py
in my modules file (for example test.py):
###############################################################
from sqlhtml import AutocompleteWidget
class smartAutocompleteWidget(AutocompleteWidget):
def __init__(self, request, field, help_fields=[],
help_string='%s', **kwargs):
self.help_string = help_string
hList = ['%s' for i in help_fields]
if hList:
self.help_string += ' (%s)' % '; '.join(hList)
self.help_fields = help_fields
AutocompleteWidget.__init__(self, request, field, **kwargs)
def callback(self):
if self.keyword in self.request.vars:
field = self.fields[0]
rows =
self.db(field.like(self.request.vars[self.keyword]+'%'))\
.select(orderby=self.orderby,limitby=self.limitby,*self.fields)
if rows:
if self.is_reference:
id_field = self.fields[1]
# import pdb; pdb.set_trace()
raise
HTTP(200,SELECT(_id=self.keyword,_class='autocomplete',
_size=len(rows),_multiple=(len(rows)==1),
*[OPTION(self.help_string %
tuple([s[h.name] for h in self.fields[:1]+self.help_fields]),
_value=s[id_field.name],
_selected=(k==0)) \
for k,s in
enumerate(rows)]).xml())
else:
raise
HTTP(200,SELECT(_id=self.keyword,_class='autocomplete',
_size=len(rows),_multiple=(len(rows)==1),
*[OPTION(s[field.name],
_selected=(k==0)) \
for k,s in
enumerate(rows)]).xml())
else:
raise HTTP(200,'')
###############################################################
in my controller:
###############################################################
def foo():
from test import smartAutocompleteWidget
autocomplete_widget = smartAutocompleteWidget(
request, db.meta_fields.field_name, id_field=db.meta_fields.id,
help_fields=[db.meta_fields.id])
form = SQLFORM.factory(
Field('field_name', widget=autocomplete_widget))
return dict(form=form)
###############################################################
and this is the ticket error I get:
Traceback (most recent call last):
File
"/home/manuele/Dropbox/sviluppo/web2py-1.99.4/gluon/restricted.py", line
204, in restricted
exec ccode in environment
File
"/home/manuele/Dropbox/sviluppo/web2py-1.99.4/applications/dev_meta_db/controllers/plugin_metadb.py",
line 296, in <module>
File "/home/manuele/Dropbox/sviluppo/web2py-1.99.4/gluon/globals.py",
line 172, in <lambda>
self._caller = lambda f: f()
File "/home/manuele/Dropbox/sviluppo/web2py-1.99.4/gluon/tools.py",
line 2533, in f
return action(*a, **b)
File
"/home/manuele/Dropbox/sviluppo/web2py-1.99.4/applications/dev_meta_db/controllers/plugin_metadb.py",
line 145, in add_field_to_table
request, db.meta_fields.field_name, id_field=db.meta_fields.id,
help_fields=[db.meta_fields.id])
File "applications/dev_meta_db/modules/plugin_metadb.py", line 156,
in __init__
AutocompleteWidget.__init__(self, request, field, **kwargs)
File "/home/manuele/Dropbox/sviluppo/web2py-1.99.4/gluon/sqlhtml.py",
line 552, in __init__
self.url = URL(args=request.args)
File "/home/manuele/Dropbox/sviluppo/web2py-1.99.4/gluon/html.py",
line 242, in URL
raise SyntaxError, 'not enough information to build the url'
SyntaxError: not enough information to build the url
Function argument list
(a=None, c=None, f=None, r=None, args=['1'], vars={}, anchor='',
extension=None, env=None, hmac_key=None, hash_vars=True, salt=None,
user_signature=None, scheme=None, host=None, port=None,
encode_embedded_slash=False)
the four values a=None, c=None, f=None and r=None tell me why I got this
error but I cannot understand why the request is differently defined
then the case when I use the original autosugestWidget...
Thank you for any help
Manuele