mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[Refactor] creator addons
(lp:c2c-addons/6.1 rev 28.1.15)
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
##############################################################################
|
||||
|
||||
""" Base template for product config """
|
||||
|
||||
from openerp.osv.orm import browse_record, browse_record_list
|
||||
|
||||
class BaseProductConfigTemplate():
|
||||
""" Abstract class for product config """
|
||||
@@ -42,30 +42,40 @@ class BaseProductConfigTemplate():
|
||||
return must return a list of id"""
|
||||
return []
|
||||
|
||||
def _disable_old_instances(self, cursor, uid, template_id,
|
||||
def _disable_old_instances(self, cursor, uid, template_br_list,
|
||||
product_ids, context=None):
|
||||
""" Clean old instance by setting those inactives """
|
||||
model_obj = self._get_model()
|
||||
ids2clean = self._get_ids_2_clean(cursor, uid, template_id,
|
||||
product_ids, context=context)
|
||||
model_obj.write(cursor, uid, ids2clean,
|
||||
{'active': False}, context=context)
|
||||
for template in template_br_list:
|
||||
ids2clean = self._get_ids_2_clean(cursor, uid, template,
|
||||
product_ids, context=context)
|
||||
if self._clean_mode == 'deactivate':
|
||||
model_obj.write(cursor, uid, ids2clean,
|
||||
{'active': False}, context=context)
|
||||
elif self._clean_mode == 'unlink':
|
||||
model_obj.unlink(cursor, uid, ids2clean, context=context)
|
||||
|
||||
|
||||
def create_instances(self, cursor, uid, template_br,
|
||||
product_ids, context=None):
|
||||
""" Create instances of model using template inherited model """
|
||||
|
||||
if not isinstance(product_ids, list):
|
||||
product_ids = [product_ids]
|
||||
|
||||
self._disable_old_instances(cursor, uid, template_br,
|
||||
product_ids, context=context)
|
||||
|
||||
data = self.copy_data(cursor, uid, template_br.id, context=context)
|
||||
# data = self.copy_data(cursor, uid, template_br.id, context=context)
|
||||
# copy data will not work in any case and may write erronus value
|
||||
|
||||
model_obj = self._get_model()
|
||||
|
||||
data = {}
|
||||
for key in model_obj._columns.keys():
|
||||
tmp = template_br[key]
|
||||
if isinstance(tmp, browse_record):
|
||||
tmp = tmp.id
|
||||
if isinstance(tmp, browse_record_list):
|
||||
tmp = [0,6,tmp]
|
||||
data[key] = tmp
|
||||
|
||||
for product_id in product_ids:
|
||||
data['product_id'] = product_id
|
||||
model_obj.create(cursor, uid, data, context=context)
|
||||
|
||||
@@ -21,15 +21,17 @@
|
||||
|
||||
""" Template of order point object """
|
||||
|
||||
from openerp.osv.orm import TransientModel, fields
|
||||
from openerp.osv.orm import Model, fields
|
||||
from base_product_config_template import BaseProductConfigTemplate
|
||||
|
||||
class OrderpointTemplate(BaseProductConfigTemplate, TransientModel):
|
||||
class OrderpointTemplate(BaseProductConfigTemplate, Model):
|
||||
""" Template for orderpoints """
|
||||
_name = 'stock.warehouse.orderpoint.template'
|
||||
|
||||
_inherit = 'stock.warehouse.orderpoint'
|
||||
_table = 'stock_warehouse_orderpoint_template'
|
||||
_clean_mode = 'deactivate'
|
||||
|
||||
|
||||
_columns = {
|
||||
'product_id': fields.many2one('product.product',
|
||||
@@ -40,13 +42,11 @@ class OrderpointTemplate(BaseProductConfigTemplate, TransientModel):
|
||||
}
|
||||
|
||||
def _get_ids_2_clean(self, cursor, uid, template_br, product_ids, context=None):
|
||||
""" hook to select model specific objects to clean before recteating new one
|
||||
return must return a list of id. """
|
||||
""" hook to select model specific objects to clean
|
||||
return must return a list of id"""
|
||||
model_obj = self._get_model()
|
||||
search_args = [('warehouse_id', '=', template_br.warehouse_id.id),
|
||||
('product_id', 'in', product_ids)]
|
||||
|
||||
ids2clean = model_obj.search(cursor, uid, search_args, context=context)
|
||||
return ids2clean
|
||||
ids_to_del = model_obj.search(cursor, uid,
|
||||
[('product_id', 'in', product_ids)])
|
||||
return ids_to_del
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
||||
3
stock_orderpoint_creator/security/ir.model.access.csv
Normal file
3
stock_orderpoint_creator/security/ir.model.access.csv
Normal file
@@ -0,0 +1,3 @@
|
||||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||
access_stock_warehouse_orderpoint_creator,stock.warehouse.manage,model_stock_warehouse_orderpoint_creator,stock.group_stock_manager,1,1,1,1
|
||||
access_stock_warehouse_orderpoint_template,stock.warehouse.manage,model_stock_warehouse_orderpoint_template,stock.group_stock_manager,1,1,1,1
|
||||
|
@@ -22,18 +22,18 @@
|
||||
""" Wizard defining stock.warehouse.orderpoint configurations for selected
|
||||
products. Those configs are generated using templates """
|
||||
|
||||
from osv import osv, fields
|
||||
from openerp.osv.orm import browse_record, TransientModel, fields
|
||||
|
||||
_template_register = ['orderpoint_template_id']
|
||||
|
||||
class OrderpointCreator(osv.osv_memory):
|
||||
class OrderpointCreator(TransientModel):
|
||||
_name = 'stock.warehouse.orderpoint.creator'
|
||||
_description = 'Orderpoint Creator'
|
||||
|
||||
_columns = {
|
||||
'orderpoint_template_id': fields.many2one(
|
||||
'stock.warehouse.orderpoint.template',
|
||||
"Stock rule template")
|
||||
_columns = {'orderpoint_template_id': fields.many2many(
|
||||
'stock.warehouse.orderpoint.template',
|
||||
rel='order_point_creator_rel',
|
||||
string='Stock rule template')
|
||||
}
|
||||
|
||||
|
||||
@@ -50,13 +50,17 @@ class OrderpointCreator(osv.osv_memory):
|
||||
if isinstance(wiz_id, list):
|
||||
wiz_id = wiz_id[0]
|
||||
current = self.browse(cursor, uid, wiz_id, context=context)
|
||||
|
||||
for template_field in self._get_template_register():
|
||||
template_br = current[template_field]
|
||||
if template_br:
|
||||
template_model = template_br._model._name
|
||||
template_br_list = current[template_field]
|
||||
if template_br_list:
|
||||
if isinstance(template_br_list, browse_record):
|
||||
template_br_list = [template_br_list]
|
||||
template_model = template_br_list[0]._model._name
|
||||
template_obj = self.pool.get(template_model)
|
||||
template_obj.create_instances(cursor, uid, template_br,
|
||||
template_obj._disable_old_instances(cursor, uid, template_br_list,
|
||||
product_ids, context=context)
|
||||
for template_br in template_br_list:
|
||||
template_obj.create_instances(cursor, uid, template_br,
|
||||
product_ids, context=context)
|
||||
|
||||
return {}
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
<field name="type">form</field>
|
||||
<field name="arch" type="xml">
|
||||
<form>
|
||||
<group string="templates">
|
||||
<field name="orderpoint_template_id"/>
|
||||
<group string="templates" colspan="4">
|
||||
<field name="orderpoint_template_id" colspan="4"/>
|
||||
</group>
|
||||
<group colspan="2" col="4">
|
||||
<button special="cancel" string="Cancel" icon="gtk-cancel"/>
|
||||
|
||||
Reference in New Issue
Block a user