diff --git a/CHANGELOG b/CHANGELOG index 68b4a43847..cec650bd5c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add MachMixin * Remove context, current_date and time from record rule evaluation Version 3.2.0 - 2014-04-21 diff --git a/doc/ref/models/models.rst b/doc/ref/models/models.rst index 5ac7918d70..a52233f97e 100644 --- a/doc/ref/models/models.rst +++ b/doc/ref/models/models.rst @@ -543,5 +543,22 @@ Instance methods: Getter for the :attr:`selection_json`. +========== +MatchMixin +========== + +.. class:: MatchMixin + +A mixin_ to add to a :class:`Model` a match method on pattern. +The pattern is a dictionary with field name as key and the value to compare. +The record matches the pattern if for all dictionary entries, the value of the +record is equal or not defined. + +Instance methods: + +.. method:: MatchMixin.match(pattern) + + Return if the instance match the pattern + .. _mixin: http://en.wikipedia.org/wiki/Mixin .. _JSON: http://en.wikipedia.org/wiki/Json diff --git a/trytond/model/__init__.py b/trytond/model/__init__.py index b63e22c2dc..68be6a8c3e 100644 --- a/trytond/model/__init__.py +++ b/trytond/model/__init__.py @@ -7,6 +7,7 @@ from .modelsingleton import ModelSingleton from .modelsql import ModelSQL from .workflow import Workflow from .dictschema import DictSchemaMixin +from .match import MatchMixin __all__ = ['Model', 'ModelView', 'ModelStorage', 'ModelSingleton', 'ModelSQL', - 'Workflow', 'DictSchemaMixin'] + 'Workflow', 'DictSchemaMixin', 'MatchMixin'] diff --git a/trytond/model/match.py b/trytond/model/match.py new file mode 100644 index 0000000000..cb82f1984e --- /dev/null +++ b/trytond/model/match.py @@ -0,0 +1,19 @@ +# This file is part of Tryton. The COPYRIGHT file at the toplevel of this +# repository contains the full copyright notices and license terms. + + +class MatchMixin(object): + + def match(self, pattern): + '''Match on pattern + pattern is a dictionary with model field as key + and matching value as value''' + for field, pattern_value in pattern.iteritems(): + value = getattr(self, field) + if value is None: + continue + if self._fields[field]._type == 'many2one': + value = value.id + if value != pattern_value: + return False + return True