[IMP] product variant: removed override of fields_view_get and use a "one group or...
authorThibault Delavallée <tde@openerp.com>
Tue, 21 Jan 2014 10:50:46 +0000 (11:50 +0100)
committerThibault Delavallée <tde@openerp.com>
Tue, 21 Jan 2014 10:50:46 +0000 (11:50 +0100)
behavior for product variant. Users are either in group_product_mono or
group_product_variant, allowing to tune the form view according to the
group the user belongs to.
product: added group_product_mono group
sale: added set_group_product_variant method that adds or remove the
group_product_mono according to the group_product_variant being unchecked
or checked.
product: updated form view accordingly

bzr revid: tde@openerp.com-20140121105046-zkbs778upjg0lpyr

addons/product/product.py
addons/product/product_view.xml
addons/product/security/product_security.xml
addons/sale/res_config.py

index 2e7eb3c..a206e39 100644 (file)
@@ -695,29 +695,6 @@ class product_product(osv.osv):
         'seller_id': fields.function(_calc_seller, type='many2one', relation="res.partner", string='Main Supplier', help="Main Supplier who has highest priority in Supplier List.", multi="seller_info"),
     }
 
-
-    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
-        #override of fields_view_get in order to replace the name field to product template
-        if context is None:
-            context = {}
-        res = super(product_product, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
-        #check the current user in group_product_variant
-        if view_type == 'form':
-            doc = etree.XML(res['arch'])
-            if self.pool['res.users'].has_group(cr, uid, 'product.group_product_variant'):
-                for node in doc.xpath("//field[@name='name']"):
-                    node.set('invisible', '1')
-                    node.set('required', '0')
-                    setup_modifiers(node, res['fields']['name'])
-                for node in doc.xpath("//label[@name='label_name']"):
-                    node.set('string','Product Template')
-            else:
-                for node in doc.xpath("//field[@name='product_tmpl_id']"):
-                    node.set('required', '0')
-                    setup_modifiers(node, res['fields']['name'])
-            res['arch'] = etree.tostring(doc)
-        return res
-
     def onchange_uom(self, cursor, user, ids, uom_id, uom_po_id):
         if uom_id and uom_po_id:
             uom_obj=self.pool.get('product.uom')
index 4c315df..c14c773 100644 (file)
                         <field name="image_medium" widget="image" class="oe_avatar oe_left"/>
                         <div class="oe_title">
                             <div class="oe_edit_only">
-                                <label for="name" name='label_name' string="Product Name"/>
+                                <label for="name" name='label_name' string="Product Name"
+                                    groups="product.group_product_mono"/>
+                                <label for="name" name='label_name' string="Product Template"
+                                    groups="product.gorup_product_variant"/>
                             </div>
                             <h1>
-                                <field name="name" class="oe_inline"/>
-                                <field name="product_tmpl_id" groups="product.group_product_variant" on_change="onchange_product_tmpl_id(product_tmpl_id)" class="oe_inline"/>
+                                <field name="name" class="oe_inline"
+                                    required="0"
+                                    groups="product.group_product_mono"/>
+                                <field name="product_tmpl_id" class="oe_inline"
+                                    groups="product.group_product_variant"
+                                    on_change="onchange_product_tmpl_id(product_tmpl_id)"/>
                                 <span attrs="{'invisible':[('variants','=',False)]}" groups="product.group_product_variant"> - </span>
-                                <field name="variants" placeholder="Variant Name" groups="product.group_product_variant" class="oe_inline" readonly="0"/>
+                                <field name="variants" placeholder="Variant Name"
+                                    readonly="0"
+                                    groups="product.group_product_variant" class="oe_inline"/>
                             </h1>
                             <label for="categ_id" class="oe_edit_only"/>
                             <h2><field name="categ_id"/></h2>
index 81a3296..bb1b7d6 100644 (file)
@@ -8,6 +8,10 @@
         <field name="name">Manage Product Variants</field>
         <field name="category_id" ref="base.module_category_hidden"/>
     </record>
+    <record id="group_product_mono" model="res.groups">
+        <field name="name">Do Not Manage Product Variants</field>
+        <field name="category_id" ref="base.module_category_hidden"/>
+    </record>
 
     <record id="group_sale_pricelist" model="res.groups">
         <field name="name">Sales Pricelists</field>
index 4295f77..d9e1091 100644 (file)
@@ -26,9 +26,37 @@ from openerp.tools.translate import _
 
 _logger = logging.getLogger(__name__)
 
-class sale_configuration(osv.osv_memory):
+
+class sale_configuration(osv.TransientModel):
     _inherit = 'sale.config.settings'
 
+    def set_group_product_variant(self, cr, uid, ids, context=None):
+        """ This method is automatically called by res_config as it begins
+            with set. It is used to implement the 'one group or another'
+            behavior. We have to perform some group manipulation by hand
+            because in res_config.execute(), set_* methods are called
+            after group_*; therefore writing on an hidden res_config file
+            could not work.
+            If group_product_variant is checked: remove group_product_mono
+            from group_user, remove the users. Otherwise, just add
+            group_product_mono in group_user.
+            The inverse logic about group_product_variant is managed by the
+            normal behavior of 'group_product_variant' field.
+        """
+        def ref(xml_id):
+            mod, xml = xml_id.split('.', 1)
+            return self.pool['ir.model.data'].get_object(cr, uid, mod, xml, context)
+
+        for obj in self.browse(cr, uid, ids, context=context):
+            config_group = ref('product.group_product_mono')
+            base_group = ref('base.group_user')
+            if obj.group_product_variant:
+                base_group.write({'implied_ids': [(3, config_group.id)]})
+                config_group.write({'users': [(3, u.id) for u in base_group.users]})
+            else:
+                base_group.write({'implied_ids': [(4, config_group.id)]})
+        return True
+
     _columns = {
         'group_invoice_so_lines': fields.boolean('Generate invoices based on the sales order lines',
             implied_group='sale.group_invoice_so_lines',