if not parent.is_company:
parent.write({'is_company': True})
+ def unlink(self, cr, uid, ids, context=None):
+ orphan_contact_ids = self.search(cr, uid,
+ [('parent_id', 'in', ids), ('id', 'not in', ids), ('use_parent_address', '=', True)], context=context)
+ if orphan_contact_ids:
+ # no longer have a parent address
+ self.write(cr, uid, orphan_contact_ids, {'use_parent_address': False}, context=context)
+ return super(res_partner, self).unlink(cr, uid, ids, context=context)
+
- def write(self, cr, uid, ids, vals, context=None):
- if isinstance(ids, (int, long)):
- ids = [ids]
- #res.partner must only allow to set the company_id of a partner if it
- #is the same as the company of all users that inherit from this partner
- #(this is to allow the code from res_users to write to the partner!) or
- #if setting the company_id to False (this is compatible with any user company)
+ def _clean_website(self, website):
+ (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(website)
+ if not scheme:
+ if not netloc:
+ netloc, path = path, ''
+ website = urlparse.urlunparse(('http', netloc, path, params, query, fragment))
+ return website
+
+ @api.multi
+ def write(self, vals):
+ # res.partner must only allow to set the company_id of a partner if it
+ # is the same as the company of all users that inherit from this partner
+ # (this is to allow the code from res_users to write to the partner!) or
+ # if setting the company_id to False (this is compatible with any user
+ # company)
+ if vals.get('website'):
+ vals['website'] = self._clean_website(vals['website'])
if vals.get('company_id'):
- for partner in self.browse(cr, uid, ids, context=context):
+ company = self.env['res.company'].browse(vals['company_id'])
+ for partner in self:
if partner.user_ids:
- user_companies = set([user.company_id.id for user in partner.user_ids])
- if len(user_companies) > 1 or vals['company_id'] not in user_companies:
+ companies = set(user.company_id for user in partner.user_ids)
+ if len(companies) > 1 or company not in companies:
raise osv.except_osv(_("Warning"),_("You can not change the company as the partner/user has multiple user linked with different companies."))
- result = super(res_partner,self).write(cr, uid, ids, vals, context=context)
- for partner in self.browse(cr, uid, ids, context=context):
- self._fields_sync(cr, uid, partner, vals, context)
+
+ result = super(res_partner, self).write(vals)
+ for partner in self:
+ self._fields_sync(partner, vals)
return result
- def create(self, cr, uid, vals, context=None):
- new_id = super(res_partner, self).create(cr, uid, vals, context=context)
- partner = self.browse(cr, uid, new_id, context=context)
- self._fields_sync(cr, uid, partner, vals, context)
- self._handle_first_contact_creation(cr, uid, partner, context)
- return new_id
+ @api.model
+ def create(self, vals):
+ if vals.get('website'):
+ vals['website'] = self._clean_website(vals['website'])
+ partner = super(res_partner, self).create(vals)
+ self._fields_sync(partner, vals)
+ self._handle_first_contact_creation(partner)
+ return partner
def open_commercial_entity(self, cr, uid, ids, context=None):
""" Utility method used to add an "Open Company" button in partner views """