← Back to team overview

openerp-community-reviewer team mailing list archive

[Merge] lp:~nemry/partner-contact-management/7.0-partner-firstname into lp:partner-contact-management

 

Jonathan Nemry (Acsone) has proposed merging lp:~nemry/partner-contact-management/7.0-partner-firstname into lp:partner-contact-management.

Requested reviews:
  Acsone OpenErp Team (acsone-openerp)

For more details, see:
https://code.launchpad.net/~nemry/partner-contact-management/7.0-partner-firstname/+merge/202494

For Partner:

1) Create always the SQL constraint on 'lastname'
2) Better implementation of "_write_name" allowing more intuitive update of the partner name when processing from another model (ex: res_user), i.e., try to keep the firstname if unchanged
3) When duplicating a partner, avoid to repeat the firstname in the name

For user:

1) Reintegrate the name as "required"
2) When duplicating a user, avoid to repeat the firstname in the related partner name
-- 
https://code.launchpad.net/~nemry/partner-contact-management/7.0-partner-firstname/+merge/202494
Your team Partner and Contact Core Editors is subscribed to branch lp:partner-contact-management.
=== modified file 'partner_firstname/partner.py'
--- partner_firstname/partner.py	2013-08-12 06:18:39 +0000
+++ partner_firstname/partner.py	2014-01-21 16:18:27 +0000
@@ -29,6 +29,10 @@
         cursor.execute('SELECT id FROM res_partner WHERE lastname IS NOT NULL Limit 1')
         if not cursor.fetchone():
             cursor.execute('UPDATE res_partner set lastname = name WHERE name IS NOT NULL')
+            # Create Sql constraint if table is not empty
+            cursor.execute('SELECT id FROM res_partner Limit 1')
+            if cursor.fetchone():
+                cursor.execute('ALTER TABLE res_partner ALTER COLUMN lastname SET NOT NULL')
 
     def _compute_name_custom(self, cursor, uid, ids, fname, arg, context=None):
         res = {}
@@ -41,8 +45,36 @@
         return res
 
     def _write_name(self, cursor, uid, partner_id, field_name, field_value, arg, context=None):
-        return self.write(cursor, uid, partner_id,
-                          {'lastname': field_value}, context=context)
+        """
+        # This method will act like this:
+        # - If firstname changed or no firstname-> name equals lastname
+        # - If only lastname changed -> only lastname will be updated
+        # pre: field_value contains the new value of name
+        # res: pass vals on the super write
+        """
+        new_name_value = field_value.split(" ", 1)
+        vals = {'lastname': field_value, 'firstname': False}
+        if len(new_name_value) != 1:
+            dict = self.read(cursor, uid, [partner_id], ['firstname'], context=context)[0]
+            if dict['firstname'] == new_name_value[1]:
+                vals['lastname'] = new_name_value[0]
+                del(vals['firstname'])
+
+        return self.write(cursor, uid, partner_id, vals, context=context)
+
+    def copy_data(self, cr, uid, id, default=None, context=None):
+        """
+        # Avoid the replication of the firstname into the name case of
+        # partner duplication
+        """
+        default = default or {}
+        if not default.get('lastname'):
+            default = default.copy()
+            default['lastname'] = '%s (copy)' % self.read(cr, uid, [id], ['lastname'], context=context)[0]['lastname']
+            if default.get('name'):
+                del(default['name'])
+        res = super(ResPartner, self).copy_data(cr, uid, id, default, context=context)
+        return res
 
     def create(self, cursor, uid, vals, context=None):
         """To support data backward compatibility we have to keep this overwrite even if we

=== modified file 'partner_firstname/res_user.py'
--- partner_firstname/res_user.py	2013-02-20 14:00:17 +0000
+++ partner_firstname/res_user.py	2014-01-21 16:18:27 +0000
@@ -17,7 +17,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
-from openerp.osv import orm
+from openerp.osv import orm,fields
 
 
 class ResUsers(orm.Model):
@@ -26,7 +26,16 @@
 
     _inherit = 'res.users'
 
-    def create(self, cursor, uid, vals, context=None):
-        if not vals.get('name'):
-            vals['name'] = vals['login']
-        return super(ResUsers, self).create(cursor, uid, vals, context=context)
+    def copy_data(self, cr, uid, id, default=None, context=None):
+        """
+        # Avoid the replication of the firstname into the name case of
+        # user duplication
+        """
+        default = default or {}
+        if not default.get('lastname'):
+            default = default.copy()
+            default['lastname'] = '%s (copy)' % self.read(cr, uid, [id], ['lastname'], context=context)[0]['lastname']
+            if default.get('name'):
+                del(default['name'])
+        res = super(ResUsers, self).copy_data(cr, uid, id, default, context=context)
+        return res

=== modified file 'partner_firstname/res_user_view.xml'
--- partner_firstname/res_user_view.xml	2013-02-20 12:43:45 +0000
+++ partner_firstname/res_user_view.xml	2014-01-21 16:18:27 +0000
@@ -8,6 +8,7 @@
       <field name="arch" type="xml">
         <field name="name" position="attributes">
           <attribute name="readonly">False</attribute>
+          <attribute name="required">True</attribute>
         </field>
       </field>
     </record>

=== added directory 'partner_firstname/tests'
=== added file 'partner_firstname/tests/__init__.py'
--- partner_firstname/tests/__init__.py	1970-01-01 00:00:00 +0000
+++ partner_firstname/tests/__init__.py	2014-01-21 16:18:27 +0000
@@ -0,0 +1,5 @@
+import test_partner_firstname
+
+checks = [
+    test_partner_firstname
+    ]

=== added file 'partner_firstname/tests/test_partner_firstname.py'
--- partner_firstname/tests/test_partner_firstname.py	1970-01-01 00:00:00 +0000
+++ partner_firstname/tests/test_partner_firstname.py	2014-01-21 16:18:27 +0000
@@ -0,0 +1,68 @@
+import openerp.tests.common as common
+
+ADMIN_USER_ID = common.ADMIN_USER_ID
+
+
+class test_partner_firstname(common.TransactionCase):
+
+    def setUp(self):
+        super(test_partner_firstname, self).setUp()
+
+        self.registry('ir.model').clear_caches()
+        self.registry('ir.model.data').clear_caches()
+
+    def test_copy_partner(self):
+        cr, uid = self.cr, ADMIN_USER_ID
+        partner_model = self.registry("res.partner")
+        res_id = partner_model.create(cr, uid, {'lastname':'lastname', 'firstname':'firstname'}, context={})
+        res_id = partner_model.copy(cr, uid, res_id, default={}, context={})
+        vals = partner_model.read(cr, uid, [res_id], ['name', 'lastname', 'firstname'], context={})[0]
+        self.assertEqual(vals['name'] == "lastname (copy) firstname" and
+                         vals['lastname'] == 'lastname (copy)' and
+                         vals['firstname'] == 'firstname', True, 'Copy of the partner failed with wrong values')
+
+    def test_copy_user(self):
+        cr, uid = self.cr, ADMIN_USER_ID
+        user_model = self.registry("res.users")
+        partner_model = self.registry("res.partner")
+        # create a user
+        res_id = user_model.create(cr, uid, {'name':'lastname', 'login':'v5Ue4Tql0Pm67KX05g25A'}, context={})
+        # get the related partner id and add it a firstname
+        dict = user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
+        partner_model.write(cr, uid, dict['partner_id'][0], {'firstname':'firstname'}, context={})
+        # copy the user and compare result
+        res_id = user_model.copy(cr, uid, res_id, default={}, context={})
+        vals = user_model.read(cr, uid, [res_id], ['name', 'lastname', 'firstname'], context={})[0]
+        self.assertEqual(vals['name'] == "lastname (copy) firstname" and
+                         vals['lastname'] == 'lastname (copy)' and
+                         vals['firstname'] == 'firstname', True, 'Copy of the user failed with wrong values')
+
+    def test_update_user_lastname(self):
+        cr, uid = self.cr, ADMIN_USER_ID
+        user_model = self.registry("res.users")
+        partner_model = self.registry("res.partner")
+        # create a user
+        res_id = user_model.create(cr, uid, {'name': 'lastname', 'login': 'v5Ue4Tql0Pm67KX05g25A'}, context={})
+        # get the related partner id and add it a firstname
+        dict = user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
+        partner_model.write(cr, uid, dict['partner_id'][0], {'firstname':'firstname'}, context={})
+        user_model.write(cr, uid, res_id, {'name': 'change firstname'}, context={})
+        vals = user_model.read(cr, uid, [res_id], ['name', 'lastname', 'firstname'], context={})[0]
+        self.assertEqual(vals['name'] == "change firstname" and
+                         vals['lastname'] == 'change' and
+                         vals['firstname'] == 'firstname', True, 'Update of the user lastname failed with wrong values')
+
+    def test_update_user_firstname(self):
+        cr, uid = self.cr, ADMIN_USER_ID
+        user_model = self.registry("res.users")
+        partner_model = self.registry("res.partner")
+        # create a user
+        res_id = user_model.create(cr, uid, {'name': 'lastname', 'login': 'v5Ue4Tql0Pm67KX05g25A'}, context={})
+        # get the related partner id and add it a firstname
+        dict = user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
+        partner_model.write(cr, uid, dict['partner_id'][0], {'firstname':'firstname'}, context={})
+        user_model.write(cr, uid, res_id, {'name': 'lastname other'}, context={})
+        vals = user_model.read(cr, uid, [res_id], ['name', 'lastname', 'firstname'], context={})[0]
+        self.assertEqual(vals['name'] == "lastname other" and
+                         vals['lastname'] == 'lastname other' and
+                         vals['firstname'] == False, True, 'Update of the user firstname failed with wrong values')


Follow ups