Browse Source

Create a partner revision on writes

pull/208/head
Guewen Baconnier 9 years ago
parent
commit
4f415f67e6
  1. 2
      partner_revision/__init__.py
  2. 4
      partner_revision/__openerp__.py
  3. 5
      partner_revision/models/__init__.py
  4. 79
      partner_revision/models/res_partner.py
  5. 63
      partner_revision/models/res_partner_revision.py
  6. 60
      partner_revision/models/revision_behavior.py
  7. 67
      partner_revision/views/res_partner_revision_views.xml
  8. 58
      partner_revision/views/revision_behavior_views.xml

2
partner_revision/__init__.py

@ -1 +1,3 @@
# -*- coding: utf-8 -*-
from . import models

4
partner_revision/__openerp__.py

@ -27,7 +27,9 @@
'depends': ['base',
],
'website': 'http://www.camptocamp.com',
'data': [],
'data': ['views/res_partner_revision_views.xml',
'views/revision_behavior_views.xml',
],
'test': [],
'installable': True,
'auto_install': False,

5
partner_revision/models/__init__.py

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
from . import res_partner
from . import res_partner_revision
from . import revision_behavior

79
partner_revision/models/res_partner.py

@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-
#
#
# Authors: Guewen Baconnier
# Copyright 2015 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
from openerp import models, fields, api
class ResPartner(models.Model):
_inherit = 'res.partner'
@api.multi
def write(self, values):
for record in self:
values = record._add_revision(values)
return super(ResPartner, self).write(values)
@api.multi
def _add_revision(self, values):
""" Add a revision on a partner
:param values: the values being written on the partner
:type values: dict
:returns: dict of values that should be wrote on the partner
(fields with a 'Validate' or 'Never' rule are excluded)
"""
self.ensure_one()
write_values = values.copy()
changes = []
rules = self.env['revision.behavior'].get_rules(self._model._name)
for field in values:
rule = rules.get(field)
if not rule:
continue
if field in values:
# TODO: if a change is done manually, values are always
# written but we create 'done' changes
if self[field] == values[field]:
# TODO handle relations, types
continue
change = {
'current_value': self[field],
'new_value': values[field],
'field_id': rule.field_id.id,
}
if rule.default_behavior == 'auto':
change['state'] = 'done'
elif rule.default_behavior == 'validate':
change['state'] = 'draft'
write_values.pop(field) # change to apply manually
elif rule.default_behavior == 'never':
change['state'] = 'cancel'
write_values.pop(field) # change never applied
changes.append(change)
if changes:
self.env['res.partner.revision'].create({
'partner_id': self.id,
'change_ids': [(0, 0, vals) for vals in changes],
'date': fields.Datetime.now(),
})
return write_values

63
partner_revision/models/res_partner_revision.py

@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
#
#
# Authors: Guewen Baconnier
# Copyright 2015 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
from openerp import models, fields
class ResPartnerRevision(models.Model):
_name = 'res.partner.revision'
_description = 'Partner Revision'
_order = 'date desc'
_rec_name = 'date'
partner_id = fields.Many2one(comodel_name='res.partner',
string='Partner',
required=True)
change_ids = fields.One2many(comodel_name='res.partner.revision.change',
inverse_name='revision_id',
string='Changes')
date = fields.Datetime()
note = fields.Text()
class ResPartnerRevisionChange(models.Model):
_name = 'res.partner.revision.change'
_description = 'Partner Revision Change'
_rec_name = 'new_value'
revision_id = fields.Many2one(comodel_name='res.partner.revision',
required=True,
string='Revision',
ondelete='cascade')
field_id = fields.Many2one(comodel_name='ir.model.fields',
string='Field',
required=True)
# TODO: different types of fields
current_value = fields.Char('Current')
new_value = fields.Char('New')
state = fields.Selection(
selection=[('draft', 'Waiting'),
('done', 'Accepted'),
('cancel', 'Refused'),
],
required=True,
default='draft',
)

60
partner_revision/models/revision_behavior.py

@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
#
#
# Authors: Guewen Baconnier
# Copyright 2015 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
from openerp import models, fields, api
class RevisionBehavior(models.Model):
_name = 'revision.behavior'
_description = 'Revision Behavior'
_rec_name = 'field_id'
model_id = fields.Many2one(comodel_name='ir.model',
string='Model',
ondelete='cascade',
default=lambda self: self._default_model_id())
field_id = fields.Many2one(comodel_name='ir.model.fields',
string='Field',
ondelete='cascade')
default_behavior = fields.Selection(
selection='_selection_default_behavior',
string='Default Behavior',
help="Auto: always apply a change.\n"
"Validate: manually applied by an administrator.\n"
"Never: change never applied.",
)
@api.model
def _default_model_id(self):
return self.env.ref('base.model_res_partner').id
@api.model
def _selection_default_behavior(self):
return [('auto', 'Auto'),
('validate', 'Validate'),
('never', 'Never'),
]
# TODO: cache
@api.model
def get_rules(self, model_name):
rules = self.search([('model_id', '=', model_name)])
return {rule.field_id.name: rule for rule in rules}

67
partner_revision/views/res_partner_revision_views.xml

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="0">
<record id="view_res_partner_revision_tree" model="ir.ui.view">
<field name="name">res.partner.revision.tree</field>
<field name="model">res.partner.revision</field>
<field name="arch" type="xml">
<tree string="Partner Revision">
<field name="partner_id"/>
<field name="date"/>
</tree>
</field>
</record>
<record id="view_res_partner_revision_form" model="ir.ui.view">
<field name="name">res.partner.revision.form</field>
<field name="model">res.partner.revision</field>
<field name="arch" type="xml">
<form string="Partner Revision">
<sheet string="Partner Revision">
<group>
<field name="partner_id"/>
<field name="date"/>
</group>
<group>
<field name="note"/>
</group>
<group>
<field name="change_ids" nolabel="1">
<tree>
<field name="field_id" context="{'no_open': true}"/>
<field name="current_value"/>
<field name="new_value"/>
<field name="state"/>
</tree>
</field>
</group>
</sheet>
</form>
</field>
</record>
<record id="view_res_partner_revision_search" model="ir.ui.view">
<field name="name">res.partner.revision.search</field>
<field name="model">res.partner.revision</field>
<field name="arch" type="xml">
<search string="Partner Revision">
<field name="partner_id"/>
</search>
</field>
</record>
<record model="ir.actions.act_window" id="action_res_partner_revision_view">
<field name="name">Partner Revision</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.partner.revision</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="search_view_id" ref="view_res_partner_revision_search"/>
</record>
<menuitem id="menu_res_partner_revision"
parent="base.menu_base_config"
sequence="20"
action="action_res_partner_revision_view"/>
</data>
</openerp>

58
partner_revision/views/revision_behavior_views.xml

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="0">
<record id="view_revision_behavior_tree" model="ir.ui.view">
<field name="name">revision.behavior.tree</field>
<field name="model">revision.behavior</field>
<field name="arch" type="xml">
<tree string="Revision Behavior">
<field name="model_id"/>
<field name="field_id"/>
<field name="default_behavior"/>
</tree>
</field>
</record>
<record id="view_revision_behavior_form" model="ir.ui.view">
<field name="name">revision.behavior.form</field>
<field name="model">revision.behavior</field>
<field name="arch" type="xml">
<form string="Revision Behavior">
<sheet string="Revision Behavior">
<group>
<field name="model_id" invisible="1"/>
<field name="field_id" domain="[('model_id', '=', model_id)]"/>
<field name="default_behavior"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="view_revision_behavior_search" model="ir.ui.view">
<field name="name">revision.behavior.search</field>
<field name="model">revision.behavior</field>
<field name="arch" type="xml">
<search string="Revision Behavior">
<field name="model_id"/>
<field name="field_id"/>
<field name="default_behavior"/>
</search>
</field>
</record>
<record model="ir.actions.act_window" id="action_revision_behavior_view">
<field name="name">Revision Behavior</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">revision.behavior</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="search_view_id" ref="view_revision_behavior_search"/>
</record>
<menuitem id="menu_revision_behavior"
parent="base.menu_base_config"
sequence="20"
action="action_revision_behavior_view"/>
</data>
</openerp>
Loading…
Cancel
Save