fields.on_changes no requiere un nombre especifico para su ejecucion
FossilOrigin-Name: 55d11605df9d1228737da18bf04b242fff3b08939021488f169ad2b042330d6f
This commit is contained in:
		| @@ -23,17 +23,17 @@ class ModelBase(object, metaclass=ModelMeta): | |||||||
|         obj._namespace_prefix = None |         obj._namespace_prefix = None | ||||||
|         obj._on_change_fields = defaultdict(list) |         obj._on_change_fields = defaultdict(list) | ||||||
|          |          | ||||||
|         def on_change_fields_for_function(function_name): |         def on_change_fields_for_function(): | ||||||
|             # se recorre arbol buscando el primero |             # se recorre arbol buscando el primero | ||||||
|             for parent_cls in type(obj).__mro__: |             for parent_cls in type(obj).__mro__: | ||||||
|                 parent_meth = getattr(parent_cls, function_name, None) |                 for parent_attr in dir(parent_cls): | ||||||
|                 if not parent_meth: |                     parent_meth = getattr(parent_cls, parent_attr, None) | ||||||
|  |                     if not callable(parent_meth): | ||||||
|                         continue |                         continue | ||||||
|                  |  | ||||||
|                     on_changes = getattr(parent_meth, 'on_changes', None) |                     on_changes = getattr(parent_meth, 'on_changes', None) | ||||||
|                     if on_changes: |                     if on_changes: | ||||||
|                     return on_changes |                         return (parent_meth, on_changes) | ||||||
|             return [] |             return (None, []) | ||||||
|  |  | ||||||
|         # forzamos registros de campos al modelo |         # forzamos registros de campos al modelo | ||||||
|         # al instanciar |         # al instanciar | ||||||
| @@ -43,10 +43,9 @@ class ModelBase(object, metaclass=ModelMeta): | |||||||
|                     setattr(obj, key, v.default) |                     setattr(obj, key, v.default) | ||||||
|  |  | ||||||
|                 # register callbacks for changes |                 # register callbacks for changes | ||||||
|                 function_name = 'on_change_%s' % (key) |                 (fun, on_change_fields) = on_change_fields_for_function() | ||||||
|                 on_change_fields = on_change_fields_for_function(function_name) |  | ||||||
|                 for field in on_change_fields: |                 for field in on_change_fields: | ||||||
|                     obj._on_change_fields[field].append(function_name) |                     obj._on_change_fields[field].append(fun) | ||||||
|          |          | ||||||
|         return obj |         return obj | ||||||
|  |  | ||||||
|   | |||||||
| @@ -47,5 +47,5 @@ class Field: | |||||||
|  |  | ||||||
|     def _changed_field(self, inst, name, value): |     def _changed_field(self, inst, name, value): | ||||||
|         for fun in inst._on_change_fields[name]: |         for fun in inst._on_change_fields[name]: | ||||||
|             getattr(inst, fun)(name, value) |             fun(inst, name, value) | ||||||
|              |              | ||||||
|   | |||||||
| @@ -391,7 +391,7 @@ def test_model_on_change_field_attribute(): | |||||||
|         hash = fields.Attribute('Hash') |         hash = fields.Attribute('Hash') | ||||||
|  |  | ||||||
|         @fields.on_change(['hash']) |         @fields.on_change(['hash']) | ||||||
|         def on_change_react(self, name, value): |         def on_react(self, name, value): | ||||||
|             assert name == 'hash' |             assert name == 'hash' | ||||||
|             self.react = "%s+4" % (value) |             self.react = "%s+4" % (value) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user