Browse Source

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.
pull/317/head
Jairo Llopis 8 years ago
parent
commit
7175db54ea
  1. 21
      base_export_manager/models/ir_exports.py
  2. 13
      base_export_manager/models/ir_exports_line.py
  3. 26
      base_export_manager/tests/test_ir_exports.py
  4. 5
      base_export_manager/views/ir_exports_view.xml

21
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 <jairo.llopis@tecnativa.com>
# 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)

13
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 <jairo.llopis@tecnativa.com>
# 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):

26
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",
})

5
base_export_manager/views/ir_exports_view.xml

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<openerp>
<data>
@ -38,7 +40,8 @@
<field name="resource"/>
</group>
<group>
<field name="export_fields" nolabel="1">
<field name="export_fields" nolabel="1"
context="{'default_model1_id': model_id}">
<tree editable="bottom">
<field name="sequence" invisible="True"/>
<field name="model1_id" invisible="True"/>

Loading…
Cancel
Save