changeset aaecbbb8e595 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=aaecbbb8e595
description:
        Validate domain of view search

        issue11380
        review354871002
diffstat:

 trytond/ir/message.xml                  |   3 ++
 trytond/ir/ui/view.py                   |  42 ++++++++++++++++++++++++++++++++-
 trytond/ir/view/ui_view_search_form.xml |   5 +++-
 3 files changed, 48 insertions(+), 2 deletions(-)

diffs (92 lines):

diff -r d8672f4b9e44 -r aaecbbb8e595 trytond/ir/message.xml
--- a/trytond/ir/message.xml    Tue Apr 12 10:35:40 2022 +0200
+++ b/trytond/ir/message.xml    Tue Apr 12 10:47:17 2022 +0200
@@ -387,5 +387,8 @@
         <record model="ir.message" id="msg_button_name_unique">
             <field name="text">The name of the button must be unique per 
model.</field>
         </record>
+        <record model="ir.message" id="msg_view_search_invalid_domain">
+            <field name="text">Invalid domain or search criteria "%(domain)s" 
for search "%(search)s".</field>
+        </record>
     </data>
 </tryton>
diff -r d8672f4b9e44 -r aaecbbb8e595 trytond/ir/ui/view.py
--- a/trytond/ir/ui/view.py     Tue Apr 12 10:35:40 2022 +0200
+++ b/trytond/ir/ui/view.py     Tue Apr 12 10:47:17 2022 +0200
@@ -10,12 +10,14 @@
 from trytond.model import ModelSQL, ModelView, fields
 from trytond.model.exceptions import ValidationError
 from trytond.pool import Pool
-from trytond.pyson import Bool, Eval, If, PYSONDecoder
+from trytond.pyson import PYSON, Bool, Eval, If, PYSONDecoder
 from trytond.rpc import RPC
 from trytond.tools import file_open
 from trytond.transaction import Transaction
 from trytond.wizard import Button, StateView, Wizard
 
+from ..action import DomainError
+
 
 class XMLError(ValidationError):
     pass
@@ -573,6 +575,44 @@
         return Transaction().user
 
     @classmethod
+    def validate_fields(cls, searches, field_names):
+        super().validate_fields(searches, field_names)
+        cls.check_domain(searches, field_names)
+
+    @classmethod
+    def check_domain(cls, searches, field_names):
+        decoder = PYSONDecoder()
+        if field_names and 'domain' not in field_names:
+            return
+        for search in searches:
+            try:
+                value = decoder.decode(search.domain)
+            except Exception as exception:
+                raise DomainError(
+                    gettext('ir.msg_view_search_invalid_domain',
+                        domain=search.domain,
+                        search=search.rec_name)) from exception
+            if isinstance(value, PYSON):
+                if not value.types() == set([list]):
+                    raise DomainError(
+                        gettext('ir.msg_view_search_invalid_domain',
+                            domain=search.domain,
+                            search=search.rec_name))
+            elif not isinstance(value, list):
+                raise DomainError(
+                    gettext('ir.msg_view_search_invalid_domain',
+                        domain=search.domain,
+                        search=search.rec_name))
+            else:
+                try:
+                    fields.domain_validate(value)
+                except Exception as exception:
+                    raise DomainError(
+                        gettext('ir.msg_view_search_invalid_domain',
+                            domain=search.domain,
+                            search=search.rec_name)) from exception
+
+    @classmethod
     def get_search(cls):
         decoder = PYSONDecoder()
         searches = cls.search_read(
diff -r d8672f4b9e44 -r aaecbbb8e595 trytond/ir/view/ui_view_search_form.xml
--- a/trytond/ir/view/ui_view_search_form.xml   Tue Apr 12 10:35:40 2022 +0200
+++ b/trytond/ir/view/ui_view_search_form.xml   Tue Apr 12 10:47:17 2022 +0200
@@ -6,8 +6,11 @@
     <field name="name"/>
     <label name="user"/>
     <field name="user"/>
+
     <label name="model"/>
     <field name="model"/>
+    <newline/>
+
     <label name="domain"/>
-    <field name="domain"/>
+    <field name="domain" colspan="3" widget="pyson"/>
 </form>

Reply via email to