From 7175db54eac1fce3fe52bc79ce60b76d9d6166b1 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Mon, 25 Jul 2016 18:00:58 +0200 Subject: [PATCH] Hardcode constraints in the `create` method instead of using normal ones. Depending on the context where the record is created, it gets `resource` or `model_id`. The problem is that Odoo checks constrains before inverses, so constrains would fail always. Test added to ensure future versions contemplate all use cases. --- base_export_manager/models/ir_exports.py | 21 ++++++++------- base_export_manager/models/ir_exports_line.py | 13 +++++++--- base_export_manager/tests/test_ir_exports.py | 26 +++++++++++++++++++ base_export_manager/views/ir_exports_view.xml | 5 +++- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/base_export_manager/models/ir_exports.py b/base_export_manager/models/ir_exports.py index cdc917915..b0ba0c32a 100644 --- a/base_export_manager/models/ir_exports.py +++ b/base_export_manager/models/ir_exports.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- -# © 2015 Antiun Ingeniería S.L. - Jairo Llopis +# Copyright 2015-2016 Jairo Llopis # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openerp import api, fields, models +from openerp import _, api, fields, models +from openerp.exceptions import ValidationError class IrExports(models.Model): @@ -10,13 +11,12 @@ class IrExports(models.Model): name = fields.Char(required=True) resource = fields.Char( - required=True, + required=False, readonly=True, help="Model's technical name.") model_id = fields.Many2one( "ir.model", "Model", - required=True, store=True, domain=[("osv_memory", "=", False)], compute="_compute_model_id", @@ -55,12 +55,13 @@ class IrExports(models.Model): @api.model def create(self, vals): - """Add new required value when missing. + """Check required values when creating the record. - This is required because this model is created from a QWeb wizard view - that does not populate ``model_id``, and it is easier to hack here than - in the view. + Odoo's export dialog populates ``resource``, while this module's new + form populates ``model_id``. At least one of them is required to + trigger the methods that fill up the other, so this should fail if + one is missing. """ - vals.setdefault("model_id", - self._get_model_id(vals.get("resource")).id) + if not any(f in vals for f in {"model_id", "resource"}): + raise ValidationError(_("You must supply a model or resource.")) return super(IrExports, self).create(vals) diff --git a/base_export_manager/models/ir_exports_line.py b/base_export_manager/models/ir_exports_line.py index 8fe29b699..46993439b 100644 --- a/base_export_manager/models/ir_exports_line.py +++ b/base_export_manager/models/ir_exports_line.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # © 2015 Antiun Ingeniería S.L. - Antonio Espinosa -# © 2015 Antiun Ingeniería S.L. - Jairo Llopis +# Copyright 2015-2016 Jairo Llopis # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp import models, fields, api, exceptions @@ -12,7 +12,7 @@ class IrExportsLine(models.Model): _order = 'sequence,id' name = fields.Char( - required=True, + required=False, readonly=True, store=True, compute="_compute_name", @@ -31,8 +31,10 @@ class IrExportsLine(models.Model): "Third field", domain="[('model_id', '=', model3_id)]") model1_id = fields.Many2one( - string="First model", + "ir.model", + "First model", readonly=True, + default=lambda self: self._default_model1_id(), related="export_id.model_id") model2_id = fields.Many2one( "ir.model", @@ -46,6 +48,11 @@ class IrExportsLine(models.Model): label = fields.Char( compute="_compute_label") + @api.model + def _default_model1_id(self): + """Default model depending on context.""" + return self.env.context.get("default_model1_id", False) + @api.multi @api.depends("field1_id", "field2_id", "field3_id") def _compute_name(self): diff --git a/base_export_manager/tests/test_ir_exports.py b/base_export_manager/tests/test_ir_exports.py index aa2a38936..983c995ee 100644 --- a/base_export_manager/tests/test_ir_exports.py +++ b/base_export_manager/tests/test_ir_exports.py @@ -2,6 +2,7 @@ # © 2015 Antiun Ingeniería S.L. - Jairo Llopis # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openerp.exceptions import ValidationError from openerp.tests.common import TransactionCase @@ -26,3 +27,28 @@ class TestIrExportsCase(TransactionCase): record = self.env["ir.exports"].create(data) self.assertEqual(record.model_id.model, data["resource"]) + + def test_create_without_model(self): + """Creating a record without ``model_id`` and ``resource`` fails.""" + IrExports = self.env["ir.exports"] + model = IrExports._get_model_id("res.partner") + + # Creating with resource + record = IrExports.create({ + "name": "some", + "resource": model.model, + }) + self.assertEqual(record.model_id, model) + + # Creating with model_id + record = IrExports.create({ + "name": "some", + "model_id": model.id, + }) + self.assertEqual(record.resource, model.model) + + # Creating without anyone + with self.assertRaises(ValidationError): + IrExports.create({ + "name": "some", + }) diff --git a/base_export_manager/views/ir_exports_view.xml b/base_export_manager/views/ir_exports_view.xml index ea1602c0f..afbc799ef 100644 --- a/base_export_manager/views/ir_exports_view.xml +++ b/base_export_manager/views/ir_exports_view.xml @@ -1,4 +1,6 @@ + @@ -38,7 +40,8 @@ - +