Browse Source

Add more controls to avoid to update the name or the lastname fields only with whitespace

pull/2/head
Jonathan Nemry 11 years ago
parent
commit
d6772ec4a6
  1. 14
      partner_firstname/__openerp__.py
  2. 40
      partner_firstname/partner.py
  3. 8
      partner_firstname/res_user.py
  4. 12
      partner_firstname/tests/test_partner_firstname.py

14
partner_firstname/__openerp__.py

@ -19,14 +19,18 @@
##############################################################################
{'name': 'Partner first name, last name',
'description': """Split first name and last name on res.partner.
'description': """
This module splits first name and last name for non company partners
====================================================================
The field 'name' becomes a stored function field concatenating lastname, firstname
The field 'name' becomes a stored function field concatenating lastname and firstname
Note: in version 7.0, installing this module invalidates a yaml test in the 'edi' module
""",
'version': '1.0.1',
'version': '1.0.2',
'author': 'Camptocamp',
'category': 'MISC',
'website': 'http://www.camptocamp.com',
'maintainer': 'Camptocamp, Acsone',
'category': 'Extra Tools',
'website': 'http://www.camptocamp.com, http://www.acsone.eu',
'depends': ['base'],
'data': [
'partner_view.xml',

40
partner_firstname/partner.py

@ -47,37 +47,47 @@ class ResPartner(Model):
def _write_name(self, cursor, uid, partner_id, field_name, field_value, arg, context=None):
"""
# Try to reverse the effect of _compute_name_custom:
# * if is_company is True then lastname = name and firstname False
# * if firstname change in the new name: lastname is set to new name, firstname is reset
# * if only lastname change in the new name: lastname is updated accordingly, firstname remains untouched
Try to reverse the effect of _compute_name_custom:
* if the partner is not a company and the firstname does not change in the new name
then firstname remains untouched and lastname is updated accordingly
* otherwise lastname=new name and firstname=False
In addition an heuristic avoids to keep a firstname without a non-blank lastname
"""
field_value = not field_value.isspace() and field_value or False
vals = {'lastname': field_value, 'firstname': False}
fields = self.read(cursor, uid, [partner_id], ['firstname', 'is_company'], context=context)[0]
if not fields['is_company']:
to_check = ' %s' % fields['firstname']
if field_value:
flds = self.read(cursor, uid, [partner_id], ['firstname', 'is_company'], context=context)[0]
if not flds['is_company']:
to_check = ' %s' % flds['firstname']
if field_value.endswith(to_check):
vals['lastname'] = field_value[:-len(to_check)]
ln = field_value[:-len(to_check)].strip()
if ln:
vals['lastname'] = ln
del(vals['firstname'])
else:
# If the lastname is deleted from the new name
# then the firstname becomes the lastname
vals['lastname'] = flds['firstname']
return self.write(cursor, uid, partner_id, vals, context=context)
def copy_data(self, cr, uid, id, default=None, context=None):
def copy_data(self, cr, uid, _id, default=None, context=None):
"""
# Avoid to replicate the firstname into the name when duplicating a partner
Avoid to replicate the firstname into the name when duplicating a partner
"""
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']
default['lastname'] = _('%s (copy)') % self.read(cr, uid, [_id], ['lastname'], context=context)[0]['lastname']
if default.get('name'):
del(default['name'])
return super(ResPartner, self).copy_data(cr, uid, id, default, context=context)
return super(ResPartner, self).copy_data(cr, uid, _id, default, context=context)
def create(self, cursor, uid, vals, context=None):
"""
# To support data backward compatibility we have to keep this overwrite even if we
# use fnct_inv: otherwise we can't create entry because lastname is mandatory and module
# will not install if there is demo data
To support data backward compatibility we have to keep this overwrite even if we
use fnct_inv: otherwise we can't create entry because lastname is mandatory and module
will not install if there is demo data
"""
to_use = vals
if vals.get('name'):

8
partner_firstname/res_user.py

@ -25,14 +25,14 @@ class ResUsers(orm.Model):
_inherit = 'res.users'
def copy_data(self, cr, uid, id, default=None, context=None):
def copy_data(self, cr, uid, _id, default=None, context=None):
"""
# Avoid to replicate the firstname into the name when duplicating a user
Avoid to replicate the firstname into the name when duplicating a user
"""
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']
default['lastname'] = _('%s (copy)') % self.read(cr, uid, [_id], ['lastname'], context=context)[0]['lastname']
if default.get('name'):
del(default['name'])
return super(ResUsers, self).copy_data(cr, uid, id, default, context=context)
return super(ResUsers, self).copy_data(cr, uid, _id, default, context=context)

12
partner_firstname/tests/test_partner_firstname.py

@ -56,8 +56,8 @@ class test_partner_firstname(common.TransactionCase):
# create a user
res_id = self.user_model.create(cr, uid, self.fields_user, context={})
# get the related partner id and add it a firstname
fields = self.user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
self.partner_model.write(cr, uid, fields['partner_id'][0], {'firstname':'firstname'}, context={})
flds = self.user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
self.partner_model.write(cr, uid, flds['partner_id'][0], {'firstname':'firstname'}, context={})
# copy the user and compare result
res_id = self.user_model.copy(cr, uid, res_id, default={}, context={})
vals = self.user_model.read(cr, uid, [res_id], ['name', 'lastname', 'firstname'], context={})[0]
@ -70,8 +70,8 @@ class test_partner_firstname(common.TransactionCase):
# create a user
res_id = self.user_model.create(cr, uid, self.fields_user, context={})
# get the related partner id and add it a firstname
fields = self.user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
self.partner_model.write(cr, uid, fields['partner_id'][0], {'firstname':'firstname'}, context={})
flds = self.user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
self.partner_model.write(cr, uid, flds['partner_id'][0], {'firstname':'firstname'}, context={})
self.user_model.write(cr, uid, res_id, {'name': 'change firstname'}, context={})
vals = self.user_model.read(cr, uid, [res_id], ['name', 'lastname', 'firstname'], context={})[0]
self.assertEqual(vals['name'] == "change firstname" and
@ -83,8 +83,8 @@ class test_partner_firstname(common.TransactionCase):
# create a user
res_id = self.user_model.create(cr, uid, self.fields_user, context={})
# get the related partner id and add it a firstname
fields = self.user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
self.partner_model.write(cr, uid, fields['partner_id'][0], {'firstname':'firstname'}, context={})
flds = self.user_model.read(cr, uid, [res_id], ['partner_id'], context={})[0]
self.partner_model.write(cr, uid, flds['partner_id'][0], {'firstname':'firstname'}, context={})
self.user_model.write(cr, uid, res_id, {'name': 'lastname other'}, context={})
vals = self.user_model.read(cr, uid, [res_id], ['name', 'lastname', 'firstname'], context={})[0]
self.assertEqual(vals['name'] == "lastname other" and

Loading…
Cancel
Save