Browse Source

Store the source of a changeset

pull/208/head
Guewen Baconnier 9 years ago
parent
commit
45ee8400f7
  1. 10
      partner_changeset/README.rst
  2. 32
      partner_changeset/models/res_partner_changeset.py
  3. 3
      partner_changeset/tests/common.py
  4. 8
      partner_changeset/tests/test_changeset_field_type.py
  5. 25
      partner_changeset/tests/test_changeset_flow.py
  6. 1
      partner_changeset/views/res_partner_changeset_views.xml
  7. 1
      partner_changeset/views/res_partner_views.xml

10
partner_changeset/README.rst

@ -50,6 +50,16 @@ there is nothing to do. Addons wanting to create changeset which need a
validation should pass the key ``_changeset_rules`` in the context when
they write on the partner.
The keys a caller should pass in the context are:
* ``__changeset_rules``: activate the rules for the changesets
* ``__changeset_rules_source_model``: name of the model which asks for
the change
* ``__changeset_rules_source_id``: id of the record which asks for the
change
The source is used for the application of the rules, it is also stored
on the changeset for information.
Finding changesets
------------------

32
partner_changeset/models/res_partner_changeset.py

@ -57,6 +57,16 @@ class ResPartnerChangeset(models.Model):
store=True,
)
note = fields.Text()
source = fields.Reference(
string='Source of the change',
selection='_reference_models',
readonly=True,
)
@api.model
def _reference_models(self):
models = self.env['ir.model'].search([])
return [(model.model, model.name) for model in models]
@api.one
@api.depends('change_ids', 'change_ids.state')
@ -85,6 +95,17 @@ class ResPartnerChangeset(models.Model):
fields to the changeset must explicitly ask for it by providing a
key ``__changeset_rules`` in the environment's context.
A caller should pass the following keys in the context:
* ``__changeset_rules``: activate the rules for the changesets
* ``__changeset_rules_source_model``: name of the model which
asks for the change
* ``__changeset_rules_source_id``: id of the record which asks
for the change
When the source model and id are not defined, the current user
is considered as the origin of the change.
Should be called before the execution of ``write`` on the record
so we can keep track of the existing value and also because the
returned values should be used for ``write`` as some of the
@ -98,6 +119,16 @@ class ResPartnerChangeset(models.Model):
"""
record.ensure_one()
source_model = self.env.context.get('__changeset_rules_source_model')
source_id = self.env.context.get('__changeset_rules_source_id')
if not (source_model and source_id):
# if the changes source is not defined, log the user who
# made the change
source_model = 'res.users'
source_id = self.env.uid
source = '%s,%s' % (source_model, source_id)
change_model = self.env['res.partner.changeset.change']
write_values = values.copy()
changes = []
@ -121,6 +152,7 @@ class ResPartnerChangeset(models.Model):
'partner_id': record.id,
'change_ids': [(0, 0, vals) for vals in changes],
'date': fields.Datetime.now(),
'source': source,
})
return write_values

3
partner_changeset/tests/common.py

@ -22,7 +22,7 @@
class ChangesetMixin(object):
def assert_changeset(self, partner, expected_changes):
def assert_changeset(self, partner, expected_source, expected_changes):
""" Check if a changeset has been created according to expected values
The partner should have no prior changeset than the one created in the
@ -40,6 +40,7 @@ class ChangesetMixin(object):
)
self.assertEqual(len(changeset), 1,
"1 changeset expected, got %s" % (changeset,))
self.assertEqual(changeset.source, expected_source)
changes = changeset.change_ids
missing = []
for expected_change in expected_changes:

8
partner_changeset/tests/test_changeset_field_type.py

@ -72,6 +72,7 @@ class TestChangesetFieldType(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_char, self.partner[self.field_char.name],
'New value', 'draft'),
]
@ -84,6 +85,7 @@ class TestChangesetFieldType(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_text, self.partner[self.field_text.name],
'New comment\non 2 lines', 'draft'),
]
@ -101,6 +103,7 @@ class TestChangesetFieldType(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_boolean, self.partner[self.field_boolean.name],
True, 'draft'),
]
@ -113,6 +116,7 @@ class TestChangesetFieldType(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_date, self.partner[self.field_date.name],
'2015-09-15', 'draft'),
]
@ -125,6 +129,7 @@ class TestChangesetFieldType(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_integer, self.partner[self.field_integer.name],
42, 'draft'),
]
@ -137,6 +142,7 @@ class TestChangesetFieldType(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_float, self.partner[self.field_float.name],
3.1415, 'draft'),
]
@ -149,6 +155,7 @@ class TestChangesetFieldType(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_selection, self.partner[self.field_selection.name],
'delivery', 'draft'),
]
@ -165,6 +172,7 @@ class TestChangesetFieldType(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_many2one, self.partner[self.field_many2one.name],
self.env.ref('base.ch'), 'draft'),
]

25
partner_changeset/tests/test_changeset_flow.py

@ -88,6 +88,7 @@ class TestChangesetFlow(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_name, 'X', 'Y', 'done'),
(self.field_street, 'street X', 'street Y', 'draft'),
(self.field_street2, 'street2 X', 'street2 Y', 'cancel'),
@ -104,6 +105,7 @@ class TestChangesetFlow(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_street, 'street X', False, 'draft')]
)
@ -119,6 +121,7 @@ class TestChangesetFlow(ChangesetMixin, common.TransactionCase):
})
self.assert_changeset(
self.partner,
self.env.user,
[(self.field_name, 'X', 'Y', 'done'),
(self.field_street, 'street X', 'street Y', 'done'),
(self.field_street2, 'street2 X', 'street2 Y', 'done'),
@ -252,3 +255,25 @@ class TestChangesetFlow(ChangesetMixin, common.TransactionCase):
self.assertEqual(partner2.street2, 'street2 Y')
self.assertEqual(changeset.state, 'done')
self.assertEqual(changeset2.state, 'done')
def test_new_changeset_source(self):
""" Source is the user who made the change """
self.partner.with_context(__changeset_rules=True).write({
'street': False,
})
changeset = self.partner.changeset_ids
self.assertEqual(changeset.source, self.env.user)
def test_new_changeset_source_other_model(self):
""" Define source from another model """
company = self.env.ref('base.main_company')
keys = {
'__changeset_rules': True,
'__changeset_rules_source_model': 'res.company',
'__changeset_rules_source_id': company.id,
}
self.partner.with_context(**keys).write({
'street': False,
})
changeset = self.partner.changeset_ids
self.assertEqual(changeset.source, company)

1
partner_changeset/views/res_partner_changeset_views.xml

@ -33,6 +33,7 @@
<sheet string="Partner Changeset">
<group>
<field name="partner_id"/>
<field name="source"/>
<field name="date"/>
</group>
<group string="Changes">

1
partner_changeset/views/res_partner_views.xml

@ -13,7 +13,6 @@
<button class="oe_inline oe_stat_button"
type="action"
name="%(partner_changeset.action_res_partner_changeset_view)d"
attrs="{'invisible': [('count_pending_changesets', '=', 0)]}"
context="{'search_default_draft': 1, 'search_default_partner_id': active_id}"
icon="fa-code-fork">
<field string="Pending Changesets"

Loading…
Cancel
Save