11# Copyright 2021 Camptocamp SA
22# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33
4- from odoo import api , fields , models
4+ from odoo import fields , models
55
66
77class IrModel (models .Model ):
@@ -11,6 +11,11 @@ class IrModel(models.Model):
1111 "m2x.create.edit.option" ,
1212 "model_id" ,
1313 )
14+ m2x_create_edit_option_as_comodel_ids = fields .One2many (
15+ "m2x.create.edit.option" ,
16+ "comodel_id" ,
17+ )
18+ comodel_field_ids = fields .One2many ("ir.model.fields" , "comodel_id" )
1419
1520 def button_empty (self ):
1621 for ir_model in self :
@@ -21,32 +26,39 @@ def button_fill(self):
2126 ir_model ._fill_m2x_create_edit_option ()
2227
2328 def _empty_m2x_create_edit_option (self ):
24- """Removes every option for model ``self``"""
29+ """Removes every option for model ``self``'s fields """
2530 self .ensure_one ()
2631 self .m2x_create_edit_option_ids .unlink ()
2732
2833 def _fill_m2x_create_edit_option (self ):
2934 """Adds every missing field option for model ``self``"""
3035 self .ensure_one ()
3136 existing = self .m2x_create_edit_option_ids .mapped ("field_id" )
32- valid = self .field_id .filtered (lambda f : f .ttype in ("many2many" , "many2one" ))
33- vals = [(0 , 0 , {"field_id" : f .id }) for f in valid - existing ]
34- self .write ({"m2x_create_edit_option_ids" : vals })
35-
36-
37- class IrModelFields (models .Model ):
38- _inherit = "ir.model.fields"
39-
40- @api .model
41- def name_search (self , name = "" , args = None , operator = "ilike" , limit = 100 ):
42- res = super ().name_search (name , args , operator , limit )
43- if not (name and self .env .context .get ("search_by_technical_name" )):
44- return res
45- domain = list (args or []) + [("name" , operator , name )]
46- new_fids = self .search (domain , limit = limit ).ids
47- for fid in [x [0 ] for x in res ]:
48- if fid not in new_fids :
49- new_fids .append (fid )
50- if limit and limit > 0 :
51- new_fids = new_fids [:limit ]
52- return self .browse (new_fids ).sudo ().name_get ()
37+ valid = self .field_id .filtered ("can_have_options" )
38+ missing = valid - existing
39+ if missing :
40+ vals = [(0 , 0 , {"field_id" : f .id }) for f in missing ]
41+ self .write ({"m2x_create_edit_option_ids" : vals })
42+
43+ def button_empty_comodel (self ):
44+ for ir_model in self :
45+ ir_model ._empty_comodel_m2x_create_edit_option ()
46+
47+ def button_fill_comodel (self ):
48+ for ir_model in self :
49+ ir_model ._fill_comodel_m2x_create_edit_option ()
50+
51+ def _empty_comodel_m2x_create_edit_option (self ):
52+ """Removes every option for fields where ``self`` is comodel"""
53+ self .ensure_one ()
54+ self .m2x_create_edit_option_as_comodel_ids .unlink ()
55+
56+ def _fill_comodel_m2x_create_edit_option (self ):
57+ """Adds every missing field option for fields where ``self`` is comodel"""
58+ self .ensure_one ()
59+ existing = self .m2x_create_edit_option_as_comodel_ids .mapped ("field_id" )
60+ valid = self .comodel_field_ids .filtered ("can_have_options" )
61+ missing = valid - existing
62+ if missing :
63+ vals = [(0 , 0 , {"field_id" : f .id }) for f in missing ]
64+ self .write ({"m2x_create_edit_option_as_comodel_ids" : vals })
0 commit comments