Skip to content

Commit

Permalink
[IMP] mrp_bom_version: pre-commit stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Kay K. Cross committed Oct 18, 2024
1 parent fa8e3e0 commit c15be7d
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 221 deletions.
7 changes: 4 additions & 3 deletions mrp_bom_version/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# (c) 2015 Oihane Crucelaegui - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

Expand All @@ -7,6 +6,8 @@

def set_active_bom_active_state(cr, registry):
"""Set those active BoMs to state 'active'"""
cr.execute("""UPDATE mrp_bom
cr.execute(
"""UPDATE mrp_bom
SET state = 'active'
WHERE active = True""")
WHERE active = True"""
)
9 changes: 4 additions & 5 deletions mrp_bom_version/__openerp__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# (c) 2015 Alfredo de la Fuente - AvanzOSC
# (c) 2015 Oihane Crucelaegui - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
Expand All @@ -9,10 +8,10 @@
"version": "8.0.1.0.0",
"license": "AGPL-3",
"author": "OdooMRP team,"
"AvanzOSC,"
"Serv. Tecnol. Avanzados - Pedro M. Baeza, "
"Odoo Community Association (OCA)",
"website": "http://www.odoomrp.com",
"AvanzOSC,"
"Serv. Tecnol. Avanzados - Pedro M. Baeza, "
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/manufacture",
"contributors": [
"Pedro M. Baeza <[email protected]>",
"Ana Juaristi <[email protected]>",
Expand Down
4 changes: 2 additions & 2 deletions mrp_bom_version/data/mrp_bom_data.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<openerp>
<data>
<record id="mt_active" model="mail.message.subtype">
<field name="name">MRP BoM Active</field>
<field name="res_model">mrp.bom</field>
<field name="default" eval="False"/>
<field name="default" eval="False" />
<field name="description">MRP BoM Active</field>
</record>
</data>
Expand Down
1 change: 0 additions & 1 deletion mrp_bom_version/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# (c) 2015 Oihane Crucelaegui - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

Expand Down
200 changes: 102 additions & 98 deletions mrp_bom_version/models/mrp_bom.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# -*- coding: utf-8 -*-
# (c) 2015 Oihane Crucelaegui - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from openerp import models, fields, api
from openerp import api, fields, models
from openerp.tools import config


class MrpBom(models.Model):
_inherit = 'mrp.bom'
_inherit = "mrp.bom"

_track = {
'state': {
'mrp_bom_version.mt_active': lambda self, cr, uid, obj,
ctx=None: obj.state == 'active',
"state": {
"mrp_bom_version.mt_active": lambda self, cr, uid, obj, ctx=None: obj.state
== "active",
},
}

@api.one
def _get_old_versions(self):
parent = self.parent_bom
old_version = self.env['mrp.bom']
old_version = self.env["mrp.bom"]
while parent:
old_version += parent
parent = parent.parent_bom
Expand All @@ -28,142 +27,147 @@ def _get_old_versions(self):
def _default_active(self):
"""Needed for preserving normal flow when testing other modules."""
res = False
if config['test_enable']:
res = not bool(self.env.context.get('test_mrp_bom_version'))
if config["test_enable"]:
res = not bool(self.env.context.get("test_mrp_bom_version"))
return res

def _default_state(self):
"""Needed for preserving normal flow when testing other modules."""
res = 'draft'
if (config['test_enable'] and
not self.env.context.get('test_mrp_bom_version')):
res = 'active'
res = "draft"
if config["test_enable"] and not self.env.context.get("test_mrp_bom_version"):
res = "active"
return res

active = fields.Boolean(
default=_default_active,
readonly=True, states={'draft': [('readonly', False)]})
historical_date = fields.Date(string='Historical Date', readonly=True)
default=_default_active, readonly=True, states={"draft": [("readonly", False)]}
)
historical_date = fields.Date(string="Historical Date", readonly=True)
state = fields.Selection(
selection=[('draft', 'Draft'), ('active', 'Active'),
('historical', 'Historical')], string='State',
index=True, readonly=True, default=_default_state, copy=False)
selection=[
("draft", "Draft"),
("active", "Active"),
("historical", "Historical"),
],
string="State",
index=True,
readonly=True,
default=_default_state,
copy=False,
)
product_tmpl_id = fields.Many2one(
readonly=True, states={'draft': [('readonly', False)]})
product_id = fields.Many2one(
readonly=True, states={'draft': [('readonly', False)]})
product_qty = fields.Float(
readonly=True, states={'draft': [('readonly', False)]})
name = fields.Char(
states={'historical': [('readonly', True)]})
code = fields.Char(
states={'historical': [('readonly', True)]})
type = fields.Selection(
states={'historical': [('readonly', True)]})
company_id = fields.Many2one(
states={'historical': [('readonly', True)]})
product_uom = fields.Many2one(
states={'historical': [('readonly', True)]})
routing_id = fields.Many2one(
readonly=True, states={'draft': [('readonly', False)]})
readonly=True, states={"draft": [("readonly", False)]}
)
product_id = fields.Many2one(readonly=True, states={"draft": [("readonly", False)]})
product_qty = fields.Float(readonly=True, states={"draft": [("readonly", False)]})
name = fields.Char(states={"historical": [("readonly", True)]})
code = fields.Char(states={"historical": [("readonly", True)]})
type = fields.Selection(states={"historical": [("readonly", True)]})
company_id = fields.Many2one(states={"historical": [("readonly", True)]})
product_uom = fields.Many2one(states={"historical": [("readonly", True)]})
routing_id = fields.Many2one(readonly=True, states={"draft": [("readonly", False)]})
bom_line_ids = fields.One2many(
readonly=True, states={'draft': [('readonly', False)]})
position = fields.Char(
states={'historical': [('readonly', True)]})
date_start = fields.Date(
states={'historical': [('readonly', True)]})
date_stop = fields.Date(
states={'historical': [('readonly', True)]})
property_ids = fields.Many2many(
states={'historical': [('readonly', True)]})
product_rounding = fields.Float(
states={'historical': [('readonly', True)]})
product_efficiency = fields.Float(
states={'historical': [('readonly', True)]})
message_follower_ids = fields.Many2many(
states={'historical': [('readonly', True)]})
message_ids = fields.One2many(
states={'historical': [('readonly', True)]})
version = fields.Integer(states={'historical': [('readonly', True)]},
copy=False, default=1)
readonly=True, states={"draft": [("readonly", False)]}
)
position = fields.Char(states={"historical": [("readonly", True)]})
date_start = fields.Date(states={"historical": [("readonly", True)]})
date_stop = fields.Date(states={"historical": [("readonly", True)]})
property_ids = fields.Many2many(states={"historical": [("readonly", True)]})
product_rounding = fields.Float(states={"historical": [("readonly", True)]})
product_efficiency = fields.Float(states={"historical": [("readonly", True)]})
message_follower_ids = fields.Many2many(states={"historical": [("readonly", True)]})
message_ids = fields.One2many(states={"historical": [("readonly", True)]})
version = fields.Integer(
states={"historical": [("readonly", True)]}, copy=False, default=1
)
parent_bom = fields.Many2one(
comodel_name='mrp.bom', string='Parent BoM', copy=False)
comodel_name="mrp.bom", string="Parent BoM", copy=False
)
old_versions = fields.Many2many(
comodel_name='mrp.bom', string='Old Versions',
compute='_get_old_versions')
comodel_name="mrp.bom", string="Old Versions", compute="_get_old_versions"
)

@api.multi
def button_draft(self):
active_draft = self.env['mrp.config.settings']._get_parameter(
'active.draft')
self.write({
'active': active_draft and active_draft.value or False,
'state': 'draft',
})
active_draft = self.env["mrp.config.settings"]._get_parameter("active.draft")
self.write(
{
"active": active_draft and active_draft.value or False,
"state": "draft",
}
)

@api.multi
def button_new_version(self):
self.ensure_one()
new_bom = self._copy_bom()
self.button_historical()
return {
'type': 'ir.actions.act_window',
'view_type': 'form, tree',
'view_mode': 'form',
'res_model': 'mrp.bom',
'res_id': new_bom.id,
'target': 'current',
"type": "ir.actions.act_window",
"view_type": "form, tree",
"view_mode": "form",
"res_model": "mrp.bom",
"res_id": new_bom.id,
"target": "current",
}

def _copy_bom(self):
active_draft = self.env['mrp.config.settings']._get_parameter(
'active.draft')
new_bom = self.copy({
'version': self.version + 1,
'active': active_draft and active_draft.value or False,
'parent_bom': self.id,
})
active_draft = self.env["mrp.config.settings"]._get_parameter("active.draft")
new_bom = self.copy(
{
"version": self.version + 1,
"active": active_draft and active_draft.value or False,
"parent_bom": self.id,
}
)
return new_bom

@api.multi
def button_activate(self):
self.write({
'active': True,
'state': 'active'
})
self.write({"active": True, "state": "active"})

@api.multi
def button_historical(self):
self.write({
'active': False,
'state': 'historical',
'historical_date': fields.Date.today()
})
self.write(
{
"active": False,
"state": "historical",
"historical_date": fields.Date.today(),
}
)

def search(self, cr, uid, args, offset=0, limit=None, order=None,
context=None, count=False):
def search(
self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False
):
"""Add search argument for field type if the context says so. This
should be in old API because context argument is not the last one.
"""
if context is None:
context = {}
search_state = context.get('state', False)
search_state = context.get("state", False)
if search_state:
args += [('state', '=', search_state)]
args += [("state", "=", search_state)]
return super(MrpBom, self).search(
cr, uid, args, offset=offset, limit=limit, order=order,
context=context, count=count)
cr,
uid,
args,
offset=offset,
limit=limit,
order=order,
context=context,
count=count,
)

@api.model
def _bom_find(
self, product_tmpl_id=None, product_id=None, properties=None):
""" Finds BoM for particular product and product uom.
def _bom_find(self, product_tmpl_id=None, product_id=None, properties=None):
"""Finds BoM for particular product and product uom.
@param product_tmpl_id: Selected product.
@param product_uom: Unit of measure of a product.
@param properties: List of related properties.
@return: False or BoM id.
"""
bom_id = super(MrpBom, self.with_context(state='active'))._bom_find(
product_tmpl_id=product_tmpl_id, product_id=product_id,
properties=properties)
bom_id = super(MrpBom, self.with_context(state="active"))._bom_find(
product_tmpl_id=product_tmpl_id,
product_id=product_id,
properties=properties,
)
return bom_id
32 changes: 17 additions & 15 deletions mrp_bom_version/models/res_config.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
# -*- coding: utf-8 -*-
# (c) 2015 Oihane Crucelaegui - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from openerp import api, fields, models


class MrpConfigSettings(models.TransientModel):
_inherit = 'mrp.config.settings'
_inherit = "mrp.config.settings"

group_mrp_bom_version = fields.Boolean(
string='Allow to re-edit BoMs',
implied_group='mrp_bom_version.group_mrp_bom_version',
help='The active state may be passed back to state draft')
string="Allow to re-edit BoMs",
implied_group="mrp_bom_version.group_mrp_bom_version",
help="The active state may be passed back to state draft",
)
active_draft = fields.Boolean(
string='Keep re-editing BoM active',
help='This will allow you to define if those BoM passed back to draft'
' are still activated or not')
string="Keep re-editing BoM active",
help="This will allow you to define if those BoM passed back to draft"
" are still activated or not",
)

def _get_parameter(self, key, default=False):
param_obj = self.env['ir.config_parameter']
rec = param_obj.search([('key', '=', key)])
param_obj = self.env["ir.config_parameter"]
rec = param_obj.search([("key", "=", key)])
return rec or default

def _write_or_create_param(self, key, value):
param_obj = self.env['ir.config_parameter']
param_obj = self.env["ir.config_parameter"]
rec = self._get_parameter(key)
if rec:
if not value:
rec.unlink()
else:
rec.value = value
elif value:
param_obj.create({'key': key, 'value': value})
param_obj.create({"key": key, "value": value})

@api.multi
def get_default_parameters(self):
def get_value(key, default=''):
def get_value(key, default=""):
rec = self._get_parameter(key)
return rec and rec.value or default

return {
'active_draft': get_value('active.draft', False),
"active_draft": get_value("active.draft", False),
}

@api.multi
def set_parameters(self):
self._write_or_create_param('active.draft', self.active_draft)
self._write_or_create_param("active.draft", self.active_draft)
2 changes: 1 addition & 1 deletion mrp_bom_version/security/mrp_bom_version_security.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<openerp>
<data noupdate="1">
<record id="group_mrp_bom_version" model="res.groups">
Expand Down
Loading

0 comments on commit c15be7d

Please sign in to comment.