From 792410f2774b8e316b89df223294fed9f3760458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=C3=89vrard?= Date: Wed, 30 Mar 2011 17:20:31 +0200 Subject: [PATCH] Added autocompletion on fields.Char --- CHANGELOG | 1 + doc/ref/models/fields.rst | 11 +++++++++++ trytond/ir/ui/form.rnc | 2 -- trytond/ir/ui/form.rng | 10 ---------- trytond/model/fields/char.py | 18 +++++++++++++++++- trytond/model/model.py | 15 +++++++-------- 6 files changed, 36 insertions(+), 21 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d7ac057beb..a8cbf12825 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Added autocompletion on fields.Char * Remove ir.default * Add type, last user and last modification on ir.attachment * Rename datas into data on ir.attachment diff --git a/doc/ref/models/fields.rst b/doc/ref/models/fields.rst index b0fb770e28..e99a788212 100644 --- a/doc/ref/models/fields.rst +++ b/doc/ref/models/fields.rst @@ -198,6 +198,17 @@ A single line string field. If true, the value of the field is translatable. The value readed and stored will depend on the ``language`` defined in the context. +.. attribute:: Char.autocomplete + + A list of field names. If this attribute is set, the client will call the + method ``autocomplete_`` of the model when the user changes one + of those field value. The method signature is:: + + autocomplete_(values) + + This method must return a list of string that will populate the + ComboboxEntry in the client. + .. warning:: Note that you may need to set :attribute:`Fields.loading` to ``lazy`` when :attribute:`Char.translate` is ``True``. diff --git a/trytond/ir/ui/form.rnc b/trytond/ir/ui/form.rnc index 0096a8d990..a3c97b8098 100644 --- a/trytond/ir/ui/form.rnc +++ b/trytond/ir/ui/form.rnc @@ -104,8 +104,6 @@ attlist.field &= attribute readonly { "0" | "1" }? attlist.field &= [ a:defaultValue = "0" ] attribute tree_invisible { "0" | "1" }? attlist.field &= attribute mode { text }? -attlist.field &= - [ a:defaultValue = "0" ] attribute completion { "0" | "1" }? attlist.field &= [ a:defaultValue = "0" ] attribute invisible { "0" | "1" }? attlist.field &= attribute sum { text }? diff --git a/trytond/ir/ui/form.rng b/trytond/ir/ui/form.rng index a2aa8e6f11..8540f19f96 100644 --- a/trytond/ir/ui/form.rng +++ b/trytond/ir/ui/form.rng @@ -369,16 +369,6 @@ - - - - - 0 - 1 - - - - diff --git a/trytond/model/fields/char.py b/trytond/model/fields/char.py index 2df49a5b9b..09bba2b2ba 100644 --- a/trytond/model/fields/char.py +++ b/trytond/model/fields/char.py @@ -3,6 +3,10 @@ from trytond.model.fields.field import Field +def autocomplete_validate(value): + if value: + assert isinstance(value, list), 'autocomplete must be a list' + class Char(Field): ''' @@ -13,7 +17,8 @@ class Char(Field): def __init__(self, string='', size=None, help='', required=False, readonly=False, domain=None, states=None, change_default=False, translate=False, select=0, on_change=None, on_change_with=None, - depends=None, order_field=None, context=None, loading='eager'): + depends=None, order_field=None, context=None, loading='eager', + autocomplete=None): ''' :param translate: A boolean. If ``True`` the field is translatable. :param size: A integer. If set defines the maximum size of the values. @@ -24,6 +29,17 @@ class Char(Field): on_change=on_change, on_change_with=on_change_with, depends=depends, order_field=order_field, context=context, loading=loading) + self.__autocomplete = None + self.autocomplete = autocomplete if autocomplete else None self.translate = translate self.size = size __init__.__doc__ += Field.__init__.__doc__ + + def _get_autocomplete(self): + return self.__autocomplete + + def _set_autocomplete(self, value): + autocomplete_validate(value) + self.__autocomplete = value + + autocomplete = property(_get_autocomplete, _set_autocomplete) diff --git a/trytond/model/model.py b/trytond/model/model.py index 2d6b7c3dc0..edf50bc63f 100644 --- a/trytond/model/model.py +++ b/trytond/model/model.py @@ -94,14 +94,12 @@ class Model(WarningErrorMixin): and not isinstance(field.selection, (list, tuple)) \ and field.selection not in self._rpc: self._rpc[field.selection] = False - if field.on_change: - on_change = 'on_change_' + field_name - if on_change not in self._rpc: - self._rpc[on_change] = False - if field.on_change_with: - on_change_with = 'on_change_with_' + field_name - if on_change_with not in self._rpc: - self._rpc[on_change_with] = False + + for attribute in ('on_change', 'on_change_with', 'autocomplete'): + function_name = '%s_%s' % (attribute, field_name) + if (getattr(field, attribute, False) + and callable(getattr(self, function_name, False))): + self._rpc.setdefault(function_name, False) def __getattr__(self, name): # Search if a function exists in inherits parents @@ -503,6 +501,7 @@ class Model(WarningErrorMixin): 'on_change', 'add_remove', 'on_change_with', + 'autocomplete', 'sort', 'datetime_field', 'loading',