~openerp-usertesting/openobject-server/trunk-muilti_address-rmu

« back to all changes in this revision

Viewing changes to openerp/addons/base/res/res_partner.py

  • Committer: Randhir Mayatra(OpenERP)
  • Date: 2013-04-05 11:40:31 UTC
  • mfrom: (4815.1.35 trunk)
  • Revision ID: rma@tinyerp.com-20130405114031-a1711m8sz1w5cmzs
[MERGE] merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
import openerp
29
29
from openerp import SUPERUSER_ID
30
 
from openerp import pooler, tools
 
30
from openerp import tools
31
31
from openerp.osv import osv, fields
32
32
from openerp.tools.translate import _
33
33
 
34
34
class format_address(object):
35
35
    def fields_view_get_address(self, cr, uid, arch, context={}):
36
 
        user_obj = self.pool.get('res.users')
 
36
        user_obj = self.pool['res.users']
37
37
        fmt = user_obj.browse(cr, SUPERUSER_ID, uid, context).company_id.country_id
38
38
        fmt = fmt and fmt.address_format
39
39
        layouts = {
154
154
    }
155
155
 
156
156
def _lang_get(self, cr, uid, context=None):
157
 
    lang_pool = self.pool.get('res.lang')
 
157
    lang_pool = self.pool['res.lang']
158
158
    ids = lang_pool.search(cr, uid, [], context=context)
159
159
    res = lang_pool.read(cr, uid, ids, ['code', 'name'], context)
160
160
    return [(r['code'], r['name']) for r in res]
289
289
 
290
290
    def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
291
291
        if (not view_id) and (view_type=='form') and context and context.get('force_email', False):
292
 
            view_id = self.pool.get('ir.model.data').get_object_reference(cr, user, 'base', 'view_partner_simple_form')[1]
 
292
            view_id = self.pool['ir.model.data'].get_object_reference(cr, user, 'base', 'view_partner_simple_form')[1]
293
293
        res = super(res_partner,self).fields_view_get(cr, user, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
294
294
        if view_type == 'form':
295
295
            res['arch'] = self.fields_view_get_address(cr, user, res['arch'], context=context)
301
301
        'tz': lambda self, cr, uid, ctx: ctx.get('tz', False),
302
302
        'customer': True,
303
303
        'category_id': _default_category,
304
 
        'company_id': lambda self, cr, uid, ctx: self.pool.get('res.company')._company_default_get(cr, uid, 'res.partner', context=ctx),
 
304
        'company_id': lambda self, cr, uid, ctx: self.pool['res.company']._company_default_get(cr, uid, 'res.partner', context=ctx),
305
305
        'color': 0,
306
306
        'is_company': False,
307
307
        'type': 'default',
338
338
 
339
339
    def onchange_state(self, cr, uid, ids, state_id, context=None):
340
340
        if state_id:
341
 
            country_id = self.pool.get('res.country.state').browse(cr, uid, state_id, context).country_id.id
 
341
            country_id = self.pool['res.country.state'].browse(cr, uid, state_id, context).country_id.id
342
342
            return {'value':{'country_id':country_id}}
343
343
        return {}
344
344
 
345
345
    def _check_ean_key(self, cr, uid, ids, context=None):
346
 
        for partner_o in pooler.get_pool(cr.dbname).get('res.partner').read(cr, uid, ids, ['ean13',]):
 
346
        for partner_o in self.pool['res.partner'].read(cr, uid, ids, ['ean13',]):
347
347
            thisean=partner_o['ean13']
348
348
            if thisean and thisean!='':
349
349
                if len(thisean)!=13:
364
364
        # Update parent and siblings or children records
365
365
        if isinstance(ids, (int, long)):
366
366
            ids = [ids]
367
 
        if vals.get('is_company')==False:
368
 
            vals.update({'child_ids' : [(5,)]})
369
367
        for partner in self.browse(cr, uid, ids, context=context):
370
368
            update_ids = []
371
369
            if partner.is_company:
372
 
                domain_children = [('parent_id', '=', partner.id), ('use_parent_address', '=', True)]
 
370
                domain_children = [('parent_id', 'child_of', partner.id), ('use_parent_address', '=', True)]
373
371
                update_ids = self.search(cr, uid, domain_children, context=context)
374
 
            elif partner.parent_id:
375
 
                 if vals.get('use_parent_address')==True:
376
 
                     domain_siblings = [('parent_id', '=', partner.parent_id.id), ('use_parent_address', '=', True)]
377
 
                     update_ids = [partner.parent_id.id] + self.search(cr, uid, domain_siblings, context=context)
378
 
                 if 'use_parent_address' not in vals and  partner.use_parent_address:
379
 
                    domain_siblings = [('parent_id', '=', partner.parent_id.id), ('use_parent_address', '=', True)]
380
 
                    update_ids = [partner.parent_id.id] + self.search(cr, uid, domain_siblings, context=context)
 
372
            elif partner.parent_id and vals.get('use_parent_address', partner.use_parent_address):
 
373
                domain_siblings = [('parent_id', '=', partner.parent_id.id), ('use_parent_address', '=', True)]
 
374
                update_ids = [partner.parent_id.id] + self.search(cr, uid, domain_siblings, context=context)
381
375
            self.update_address(cr, uid, update_ids, vals, context)
382
376
        return super(res_partner,self).write(cr, uid, ids, vals, context=context)
383
377
 
384
378
    def create(self, cr, uid, vals, context=None):
385
379
        if context is None:
386
 
            context={}
 
380
            context = {}
387
381
        # Update parent and siblings records
388
 
        if vals.get('parent_id') and vals.get('use_parent_address'):
389
 
            domain_siblings = [('parent_id', '=', vals['parent_id']), ('use_parent_address', '=', True)]
390
 
            update_ids = [vals['parent_id']] + self.search(cr, uid, domain_siblings, context=context)
391
 
            self.update_address(cr, uid, update_ids, vals, context)
392
 
        return super(res_partner,self).create(cr, uid, vals, context=context)
 
382
        if vals.get('parent_id'):
 
383
            if 'use_parent_address' in vals:
 
384
                use_parent_address = vals['use_parent_address']
 
385
            else:
 
386
                use_parent_address = self.default_get(cr, uid, ['use_parent_address'], context=context)['use_parent_address']
 
387
 
 
388
            if use_parent_address:
 
389
                domain_siblings = [('parent_id', '=', vals['parent_id']), ('use_parent_address', '=', True)]
 
390
                update_ids = [vals['parent_id']] + self.search(cr, uid, domain_siblings, context=context)
 
391
                self.update_address(cr, uid, update_ids, vals, context)
 
392
 
 
393
                # add missing address keys
 
394
                onchange_values = self.onchange_address(cr, uid, [], use_parent_address,
 
395
                                                        vals['parent_id'], context=context).get('value') or {}
 
396
                vals.update(dict((key, value)
 
397
                            for key, value in onchange_values.iteritems()
 
398
                            if key in ADDRESS_FIELDS and key not in vals))
 
399
 
 
400
        return super(res_partner, self).create(cr, uid, vals, context=context)
393
401
 
394
402
    def update_address(self, cr, uid, ids, vals, context=None):
395
 
        addr_vals = dict((key, vals[key]) for key in POSTAL_ADDRESS_FIELDS if vals.get(key))
 
403
        addr_vals = dict((key, vals[key]) for key in POSTAL_ADDRESS_FIELDS if key in vals)
396
404
        if addr_vals:
397
405
            return super(res_partner, self).write(cr, uid, ids, addr_vals, context)
398
406
 
419
427
        """ Supported syntax:
420
428
            - 'Raoul <raoul@grosbedon.fr>': will find name and email address
421
429
            - otherwise: default, everything is set as the name """
422
 
        match = re.search(r'([^\s,<@]+@[^>\s,]+)', text)
423
 
        if match:
424
 
            email = match.group(1)
425
 
            name = text[:text.index(email)].replace('"','').replace('<','').strip()
 
430
        emails = tools.email_split(text)
 
431
        if emails:
 
432
            email = emails[0]
 
433
            name = text[:text.index(email)].replace('"', '').replace('<', '').strip()
426
434
        else:
427
435
            name, email = text, ''
428
436
        return name, email
465
473
                             OR partner.name || ' (' || COALESCE(company.name,'') || ')'
466
474
                          ''' + operator + ' %(name)s ' + limit_str, query_args)
467
475
            ids = map(lambda x: x[0], cr.fetchall())
468
 
            if args:
469
 
                ids = self.search(cr, uid, [('id', 'in', ids)] + args, limit=limit, context=context)
 
476
            ids = self.search(cr, uid, [('id', 'in', ids)] + args, limit=limit, context=context)
470
477
            if ids:
471
478
                return self.name_get(cr, uid, ids, context)
472
479
        return super(res_partner,self).name_search(cr, uid, name, args, operator=operator, context=context, limit=limit)
495
502
 
496
503
    def email_send(self, cr, uid, ids, email_from, subject, body, on_error=''):
497
504
        while len(ids):
498
 
            self.pool.get('ir.cron').create(cr, uid, {
 
505
            self.pool['ir.cron'].create(cr, uid, {
499
506
                'name': 'Send Partner Emails',
500
507
                'user_id': uid,
501
508
                'model': 'res.partner',
531
538
        if res: return res
532
539
        if not context.get('category_id', False):
533
540
            return False
534
 
        return _('Partners: ')+self.pool.get('res.partner.category').browse(cr, uid, context['category_id'], context).name
 
541
        return _('Partners: ')+self.pool['res.partner.category'].browse(cr, uid, context['category_id'], context).name
535
542
 
536
543
    def main_partner(self, cr, uid):
537
544
        ''' Return the id of the main partner
538
545
        '''
539
 
        model_data = self.pool.get('ir.model.data')
 
546
        model_data = self.pool['ir.model.data']
540
547
        return model_data.browse(cr, uid,
541
548
                            model_data.search(cr, uid, [('module','=','base'),
542
549
                                                ('name','=','main_partner')])[0],