forked from itpp-labs/misc-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29899ef
commit 1e9a07a
Showing
5 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Replace x2y references | ||
====================== | ||
|
||
The module allows to create rules to execute replacement for relation field. | ||
|
||
Tested on Odoo 8.0 bf9544d7d430704efd006cca182a7120a55a0c8a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
'name': "Replace x2y references", | ||
'version': '1.0.0', | ||
'author': 'Ivan Yelizariev', | ||
'category': 'Custom', | ||
'website': 'https://yelizariev.github.io', | ||
'depends': [], | ||
'data': [ | ||
'views.xml', | ||
], | ||
'installable': True | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from openerp import api, models, fields, SUPERUSER_ID, exceptions | ||
|
||
class replace_rule(models.Model): | ||
_name = 'base_replace_ref.rule' | ||
|
||
name = fields.Char('Name', required=True) | ||
draft = fields.Boolean('Draft', default=True) | ||
model_id = fields.Many2one('ir.model', 'Model', required=True) | ||
|
||
value_line_ids = fields.One2many('base_replace_ref.rule.value_line', 'rule_id', string='Value lines') | ||
|
||
field_line_ids = fields.One2many('base_replace_ref.rule.field_line', 'rule_id', string='Field lines') | ||
|
||
@api.one | ||
def find_fields(self): | ||
if not self.model_id: | ||
raise exceptions.Warning('Define Model first') | ||
|
||
self.draft = True | ||
res = [] | ||
cur_fields = [line.field_id.id for line in self.field_line_ids] | ||
for field in self.env['ir.model.fields'].search([('relation', '=', self.model_id.model)]): | ||
if field.id in cur_fields: | ||
continue | ||
self.env['base_replace_ref.rule.field_line'].create({'rule_id': self.id, 'model_id': field.model_id.id, 'field_id': field.id}) | ||
|
||
@api.one | ||
def clear_fields(self): | ||
self.field_line_ids.unlink() | ||
|
||
@api.model | ||
def parse_value(self, model, value): | ||
if not value: | ||
return None | ||
try: | ||
return int(value) | ||
except ValueError: | ||
pass | ||
res = self.env.ref(value) | ||
assert res, 'Value not found for ref %s' % value | ||
return res.id | ||
|
||
@api.one | ||
def apply(self): | ||
if self.draft: | ||
raise exceptions.Warning('You cannot apply draft rule') | ||
|
||
for vline in self.value_line_ids: | ||
src = self.parse_value(self.model_id.model, vline.src) | ||
dst = self.parse_value(self.model_id.model, vline.dst) | ||
|
||
for fline in self.field_line_ids: | ||
self.replace(fline.field_id, src, dst) | ||
|
||
@api.model | ||
def replace(self, field_id, src, dst): | ||
model = self.env[field_id.model_id.model] | ||
|
||
if field_id.ttype == 'one2many': | ||
r = self.env[field_id.relation].browse(src) | ||
parent_id = getattr(r, field_id.relation_field).id | ||
r.write({field_id.relation_field: None}) | ||
self.env[field_id.relation].browse(dst).write({field_id.relation_field: parent_id}) | ||
return True | ||
res = model.search([ (field_id.name, '=', src)]) | ||
if field_id.ttype == 'many2one': | ||
res.write({field_id.name: dst}) | ||
if field_id.ttype == 'many2many': | ||
res.write({field_id.name: [(3, src, False)]}) | ||
res.write({field_id.name: [(4, dst, False)]}) | ||
|
||
|
||
class value_line(models.Model): | ||
_name = 'base_replace_ref.rule.value_line' | ||
|
||
_src_dst_help = 'ID or Reference' | ||
|
||
rule_id = fields.Many2one('base_replace_ref.rule') | ||
src = fields.Char('Source', help=_src_dst_help, required=True) | ||
dst = fields.Char('Destination', help=_src_dst_help) | ||
|
||
class field_line(models.Model): | ||
_name = 'base_replace_ref.rule.field_line' | ||
|
||
rule_id = fields.Many2one('base_replace_ref.rule') | ||
model_id = fields.Many2one('ir.model', string='Model') | ||
field_id = fields.Many2one('ir.model.fields', string='Field', required=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<openerp><data> | ||
|
||
<record id="replace_rule-view" model="ir.ui.view"> | ||
<field name="name">base_replace_ref.rule.form</field> | ||
<field name="model">base_replace_ref.rule</field> | ||
<field name="arch" type="xml"> | ||
<form string="Replace Rule"> | ||
<header> | ||
<button name="apply" string="Apply (DANGEROUS)" type="object" class="oe_highlight"/> | ||
</header> | ||
<sheet> | ||
<div class="oe_title"> | ||
<label for="name" class="oe_edit_only"/> | ||
<h1> | ||
<field name="name"/> | ||
</h1> | ||
</div> | ||
|
||
<group> | ||
<field name="draft"/> | ||
<field name="model_id"/> | ||
</group> | ||
|
||
<separator string="Value lines (what to replace)"/> | ||
<field name="value_line_ids"> | ||
<tree editable="bottom"> | ||
<field name="src"/> | ||
<field name="dst"/> | ||
</tree> | ||
</field> | ||
|
||
<separator string="Field lines (where to replace)"/> | ||
<button name="find_fields" string="Find all fields" type="object"/> | ||
<button name="clear_fields" string="Clear all fields" type="object"/> | ||
<field name="field_line_ids"> | ||
<tree editable="bottom"> | ||
<field name="model_id"/> | ||
<field name="field_id" domain="[('model_id', '=', model_id)]"/> | ||
</tree> | ||
</field> | ||
|
||
</sheet> | ||
</form> | ||
</field> | ||
</record> | ||
|
||
<record id="replace_rule_tree-view" model="ir.ui.view"> | ||
<field name="name">base_replace_ref.rule.form</field> | ||
<field name="model">base_replace_ref.rule</field> | ||
<field name="arch" type="xml"> | ||
<tree string="Request Link"> | ||
<field name="name"/> | ||
<field name="model_id"/> | ||
</tree> | ||
</field> | ||
</record> | ||
|
||
<record id="replace_rule-act" model="ir.actions.act_window"> | ||
<field name="name">Replace rules</field> | ||
<field name="type">ir.actions.act_window</field> | ||
<field name="res_model">base_replace_ref.rule</field> | ||
<field name="view_type">form</field> | ||
<field name="view_id" eval="False"/> | ||
</record> | ||
|
||
<menuitem action="replace_rule-act" id="menu_replace_rule_act" parent="base.next_id_9"/> | ||
|
||
</data></openerp> |