stop using autocomplete_initfuncs dict on entity classes
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 27 May 2009 15:19:25 +0200
changeset 1966 87ce7d336393
parent 1962 45af1e015683
child 1970 c519a37193e1
stop using autocomplete_initfuncs dict on entity classes
web/formwidgets.py
--- a/web/formwidgets.py	Wed May 27 13:01:32 2009 +0200
+++ b/web/formwidgets.py	Wed May 27 15:19:25 2009 +0200
@@ -7,6 +7,7 @@
 __docformat__ = "restructuredtext en"
 
 from datetime import date
+from warnings import warn
 
 from cubicweb.common import tags
 from cubicweb.web import stdmsgs, INTERNAL_FIELD_VALUE
@@ -304,17 +305,30 @@
     wdgtype = 'SuggestField'
     loadtype = 'auto'
 
+    def __init__(self, *args, **kwargs):
+        try:
+            self.autocomplete_initfunc = kwargs.pop('autocomplete_initfunc')
+        except KeyError:
+            warn('use autocomplete_initfunc argument of %s constructor '
+                 'instead of relying on autocomplete_initfuncs dictionary on '
+                 'the entity class' % self.__class__.__name__,
+                 DeprecationWarning)
+            self.autocomplete_initfunc = None
+        super(AutoCompletionWidget, self).__init__(*args, **kwargs)
+
     def _render_attrs(self, form, field):
         name, values, attrs = super(AutoCompletionWidget, self)._render_attrs(form, field)
-        if not values[0]:
-            values = (INTERNAL_FIELD_VALUE,)
         init_ajax_attributes(attrs, self.wdgtype, self.loadtype)
         # XXX entity form specific
         attrs['cubicweb:dataurl'] = self._get_url(form.edited_entity, field)
         return name, values, attrs
 
     def _get_url(self, entity, field):
-        fname = entity.autocomplete_initfuncs[field.name]
+        if self.autocomplete_initfunc is None:
+            # XXX for bw compat
+            fname = entity.autocomplete_initfuncs[field.name]
+        else:
+            fname = self.autocomplete_initfunc
         return entity.req.build_url('json', fname=fname, mode='remote',
                                     pageid=entity.req.pageid)
 
@@ -324,7 +338,12 @@
     wdgtype = 'StaticFileSuggestField'
 
     def _get_url(self, entity, field):
-        return entity.req.datadir_url + entity.autocomplete_initfuncs[field.name]
+        if self.autocomplete_initfunc is None:
+            # XXX for bw compat
+            fname = entity.autocomplete_initfuncs[field.name]
+        else:
+            fname = self.autocomplete_initfunc
+        return entity.req.datadir_url + fname
 
 
 class RestrictedAutoCompletionWidget(AutoCompletionWidget):