|
1 | 1 | # Copyright 2021 Camptocamp SA |
2 | 2 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
3 | 3 |
|
4 | | -from odoo import api, fields, models |
| 4 | +from odoo import fields, models |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class IrModel(models.Model): |
8 | 8 | _inherit = "ir.model" |
9 | 9 |
|
10 | | - m2x_create_edit_option_ids = fields.One2many( |
| 10 | + m2x_option_ids = fields.One2many( |
11 | 11 | "m2x.create.edit.option", |
12 | 12 | "model_id", |
13 | 13 | ) |
| 14 | + m2x_comodels_option_ids = fields.One2many( |
| 15 | + "m2x.create.edit.option", |
| 16 | + "comodel_id", |
| 17 | + ) |
| 18 | + comodel_field_ids = fields.One2many("ir.model.fields", "comodel_id") |
| 19 | + |
| 20 | + def button_empty_m2x_options(self): |
| 21 | + for ir_model in self: |
| 22 | + ir_model._empty_m2x_options(own=True) |
14 | 23 |
|
15 | | - def button_empty(self): |
| 24 | + def button_fill_m2x_options(self): |
16 | 25 | for ir_model in self: |
17 | | - ir_model._empty_m2x_create_edit_option() |
| 26 | + ir_model._fill_m2x_options(own=True) |
18 | 27 |
|
19 | | - def button_fill(self): |
| 28 | + def button_empty_m2x_comodels_options(self): |
20 | 29 | for ir_model in self: |
21 | | - ir_model._fill_m2x_create_edit_option() |
| 30 | + ir_model._empty_m2x_options(comodels=True) |
22 | 31 |
|
23 | | - def _empty_m2x_create_edit_option(self): |
24 | | - """Removes every option for model ``self``""" |
| 32 | + def button_fill_m2x_comodels_options(self): |
| 33 | + for ir_model in self: |
| 34 | + ir_model._fill_m2x_options(comodels=True) |
| 35 | + |
| 36 | + def _empty_m2x_options(self, own=False, comodels=False): |
| 37 | + """Removes every option for model ``self``'s fields |
| 38 | +
|
| 39 | + :param bool own: if True, deletes options for model's fields |
| 40 | + :param bool comodels: if True, deletes options for fields where ``self`` is comodel |
| 41 | + """ |
25 | 42 | self.ensure_one() |
26 | | - self.m2x_create_edit_option_ids.unlink() |
| 43 | + to_delete = self.env["m2x.create.edit.option"] |
| 44 | + if own: |
| 45 | + to_delete += self.m2x_option_ids |
| 46 | + if comodels: |
| 47 | + to_delete += self.m2x_comodels_option_ids |
| 48 | + if to_delete: |
| 49 | + to_delete.unlink() |
| 50 | + |
| 51 | + def _fill_m2x_options(self, own=False, comodels=False): |
| 52 | + """Adds every missing field option for model ``self`` (with default values) |
27 | 53 |
|
28 | | - def _fill_m2x_create_edit_option(self): |
29 | | - """Adds every missing field option for model ``self``""" |
| 54 | + :param bool own: if True, creates options for model's fields |
| 55 | + :param bool comodels: if True, creates options for fields where ``self`` is comodel |
| 56 | + """ |
30 | 57 | self.ensure_one() |
31 | | - 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() |
| 58 | + todo = set() |
| 59 | + if own: |
| 60 | + exist = self.m2x_option_ids.mapped("field_id") |
| 61 | + valid = self.field_id.filtered("can_have_options") |
| 62 | + todo.update((valid - exist).ids) |
| 63 | + if comodels: |
| 64 | + exist = self.m2x_comodels_option_ids.mapped("field_id") |
| 65 | + valid = self.comodel_field_ids.filtered("can_have_options") |
| 66 | + todo.update((valid - exist).ids) |
| 67 | + if todo: |
| 68 | + self.env["m2x.create.edit.option"].create([{"field_id": i} for i in todo]) |
0 commit comments