|
| 1 | +# Copyright 2024-2025 Quartile (https://www.quartile.com) |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +import ast |
| 5 | +import logging |
| 6 | + |
| 7 | +from odoo import _, api, fields, models |
| 8 | +from odoo.exceptions import ValidationError |
| 9 | + |
| 10 | +_logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class QwebFieldOptions(models.Model): |
| 14 | + _name = "qweb.field.options" |
| 15 | + _description = "Qweb Field Options" |
| 16 | + _order = "res_model_id, field_id" |
| 17 | + |
| 18 | + res_model_id = fields.Many2one( |
| 19 | + "ir.model", string="Model", ondelete="cascade", required=True |
| 20 | + ) |
| 21 | + res_model_name = fields.Char("Model Name", related="res_model_id.model", store=True) |
| 22 | + field_id = fields.Many2one( |
| 23 | + "ir.model.fields", |
| 24 | + domain="[('model_id', '=', res_model_id)]", |
| 25 | + string="Field", |
| 26 | + ondelete="cascade", |
| 27 | + required=True, |
| 28 | + ) |
| 29 | + field_type = fields.Selection(related="field_id.ttype") |
| 30 | + field_name = fields.Char("Field Name", related="field_id.name", store=True) |
| 31 | + uom_id = fields.Many2one("uom.uom", string="UoM", ondelete="cascade") |
| 32 | + uom_field_id = fields.Many2one( |
| 33 | + "ir.model.fields", |
| 34 | + domain="[('model_id', '=', res_model_id), ('relation', '=', 'uom.uom')]", |
| 35 | + string="UoM Field", |
| 36 | + ondelete="cascade", |
| 37 | + ) |
| 38 | + currency_id = fields.Many2one("res.currency", string="Currency", ondelete="cascade") |
| 39 | + currency_field_id = fields.Many2one( |
| 40 | + "ir.model.fields", |
| 41 | + domain="[('model_id', '=', res_model_id), ('relation', '=', 'res.currency')]", |
| 42 | + string="Currency Field", |
| 43 | + ondelete="cascade", |
| 44 | + ) |
| 45 | + field_options = fields.Char( |
| 46 | + "Options", |
| 47 | + help="A string representation of a dictionary to specify field formatting " |
| 48 | + "options. Examples:\n" |
| 49 | + "{'widget': 'date'}\n" |
| 50 | + "{'widget': 'monetary'}\n" |
| 51 | + "{'widget': 'contact', 'fields': ['name', 'phone']}.", |
| 52 | + ) |
| 53 | + digits = fields.Integer() |
| 54 | + company_id = fields.Many2one("res.company", string="Company") |
| 55 | + |
| 56 | + @api.constrains("field_options") |
| 57 | + def _check_field_options_format(self): |
| 58 | + for rec in self: |
| 59 | + if not rec.field_options: |
| 60 | + continue |
| 61 | + field_options = False |
| 62 | + try: |
| 63 | + field_options = ast.literal_eval(rec.field_options) |
| 64 | + except Exception as e: |
| 65 | + raise ValidationError( |
| 66 | + _( |
| 67 | + "Invalid string for the Options field: %(field_options)s.\n" |
| 68 | + "Error: %(error)s" |
| 69 | + ) |
| 70 | + % {"field_options": rec.field_options, "error": e} |
| 71 | + ) from e |
| 72 | + if not isinstance(field_options, dict): |
| 73 | + raise ValidationError( |
| 74 | + _("Options must be a dictionary, but got %s") % type(field_options) |
| 75 | + ) |
| 76 | + |
| 77 | + def _get_score(self, record): |
| 78 | + self.ensure_one() |
| 79 | + score = 1 |
| 80 | + if self.company_id: |
| 81 | + if record.company_id == self.company_id: |
| 82 | + score += 1 |
| 83 | + else: |
| 84 | + return -1 |
| 85 | + if self.uom_id: |
| 86 | + if record[self.uom_field_id.sudo().name] == self.uom_id: |
| 87 | + score += 1 |
| 88 | + else: |
| 89 | + return -1 |
| 90 | + if self.currency_id: |
| 91 | + if record[self.currency_field_id.sudo().name] == self.currency_id: |
| 92 | + score += 1 |
| 93 | + else: |
| 94 | + return -1 |
| 95 | + return score |
| 96 | + |
| 97 | + def _update_field_options(self, record, field_options): |
| 98 | + self.ensure_one() |
| 99 | + if self.field_options: |
| 100 | + try: |
| 101 | + extra_options = ast.literal_eval(self.field_options) |
| 102 | + if extra_options.get("widget") == "monetary": |
| 103 | + extra_options["display_currency"] = ( |
| 104 | + self.currency_id |
| 105 | + or hasattr(record, "company_id") |
| 106 | + and record.company_id.currency_id |
| 107 | + or self.env.company.currency_id |
| 108 | + ) |
| 109 | + field_options.update(extra_options) |
| 110 | + except Exception as e: |
| 111 | + _logger.error( |
| 112 | + "Failed to parse field options as a dictionary: " |
| 113 | + f"{self.field_options}. Error: {e}" |
| 114 | + ) |
| 115 | + elif self.field_type == "float": |
| 116 | + field_options["precision"] = self.digits |
| 117 | + return field_options |
0 commit comments