Browse Source

More Yaml tests

Use the new phone attributes on objects inside the code
pull/31/head
Alexis de Lattre 10 years ago
parent
commit
b4522e78c8
  1. 84
      base_phone/base_phone.py
  2. 8
      base_phone/test/phonenum.yml
  3. 10
      base_phone/wizard/reformat_all_phonenumbers.py
  4. 12
      crm_claim_phone/crm_claim_phone.py
  5. 1
      crm_phone/__openerp__.py
  6. 19
      crm_phone/crm_phone.py
  7. 41
      crm_phone/test/phonenum.yml
  8. 13
      event_phone/event_phone.py
  9. 13
      hr_phone/hr_phone.py
  10. 13
      hr_recruitment_phone/hr_recruitment_phone.py

84
base_phone/base_phone.py

@ -114,26 +114,21 @@ class PhoneCommon(models.AbstractModel):
% (vals.get(field), e))
return vals
def get_name_from_phone_number(
self, cr, uid, presented_number, context=None):
@api.model
def get_name_from_phone_number(self, presented_number):
'''Function to get name from phone number. Usefull for use from IPBX
to add CallerID name to incoming calls.'''
res = self.get_record_from_phone_number(
cr, uid, presented_number, context=context)
res = self.get_record_from_phone_number(presented_number)
if res:
return res[2]
else:
return False
def get_record_from_phone_number(
self, cr, uid, presented_number, context=None):
@api.model
def get_record_from_phone_number(self, presented_number):
'''If it finds something, it returns (object name, ID, record name)
For example : ('res.partner', 42, u'Alexis de Lattre (Akretion)')
'''
if context is None:
context = {}
ctx_phone = context.copy()
ctx_phone['callerid'] = True
_logger.debug(
u"Call get_name_from_phone_number with number = %s"
% presented_number)
@ -146,48 +141,47 @@ class PhoneCommon(models.AbstractModel):
_logger.warning(
u"Number '%s' should only contain digits." % presented_number)
user = self.pool['res.users'].browse(cr, uid, uid, context=context)
nr_digits_to_match_from_end = \
user.company_id.number_of_digits_to_match_from_end
self.env.user.company_id.number_of_digits_to_match_from_end
if len(presented_number) >= nr_digits_to_match_from_end:
end_number_to_match = presented_number[
-nr_digits_to_match_from_end:len(presented_number)]
else:
end_number_to_match = presented_number
phonefieldsdict = self._get_phone_fields(cr, uid, context=context)
phonefieldslist = []
for objname, prop in phonefieldsdict.iteritems():
if prop.get('get_name_sequence'):
phonefieldslist.append({objname: prop})
phoneobjects = self._get_phone_fields()
phonefieldslist = [] # [('res.parter', 10), ('crm.lead', 20)]
for objname in phoneobjects:
if (
'_phone_name_sequence' in dir(self.env[objname]) and
self.env[objname]._phone_name_sequence):
phonefieldslist.append(
(objname, self.env[objname]._phone_name_sequence))
phonefieldslist_sorted = sorted(
phonefieldslist,
key=lambda element: element.values()[0]['get_name_sequence'])
for phonedict in phonefieldslist_sorted:
objname = phonedict.keys()[0]
prop = phonedict.values()[0]
phonefields = prop['phonefields']
obj = self.pool[objname]
key=lambda element: element[1])
_logger.debug('phonefieldslist_sorted=%s' % phonefieldslist_sorted)
for (objname, prio) in phonefieldslist_sorted:
obj = self.with_context(callerid=True).env[objname]
pg_search_number = str('%' + end_number_to_match)
_logger.debug(
"Will search phone and mobile numbers in %s ending with '%s'"
% (objname, end_number_to_match))
domain = []
for phonefield in phonefields:
for phonefield in obj._phone_fields:
domain.append((phonefield, '=like', pg_search_number))
if len(phonefields) > 1:
domain = ['|'] * (len(phonefields) - 1) + domain
res_ids = obj.search(cr, uid, domain, context=context)
if len(res_ids) > 1:
if len(obj._phone_fields) > 1:
domain = ['|'] * (len(obj._phone_fields) - 1) + domain
res_obj = obj.search(domain)
if len(res_obj) > 1:
_logger.warning(
u"There are several %s (IDS = %s) with a phone number "
"ending with '%s'. Taking the first one."
% (objname, res_ids, end_number_to_match))
if res_ids:
name = obj.name_get(
cr, uid, res_ids[0], context=ctx_phone)[0][1]
res = (objname, res_ids[0], name)
% (objname, res_obj.ids, end_number_to_match))
res_obj = res_obj[0]
if res_obj:
name = res_obj.name_get()[0][1]
res = (objname, res_obj.id, name)
_logger.debug(
u"Answer get_record_from_phone_number: (%s, %d, %s)"
% (res[0], res[1], res[2]))
@ -198,16 +192,22 @@ class PhoneCommon(models.AbstractModel):
% (objname, end_number_to_match))
return False
def _get_phone_fields(self, cr, uid, context=None):
@api.model
def _get_phone_fields(self):
'''Returns a dict with key = object name
and value = list of phone fields'''
res = {
'res.partner': {
'phonefields': ['phone', 'mobile'],
'faxfields': ['fax'],
'get_name_sequence': 10,
},
}
models = self.env['ir.model'].search([('osv_memory', '=', False)])
res = []
for model in models:
senv = False
try:
senv = self.env[model.model]
except:
continue
if (
'_phone_fields' in dir(senv) and
isinstance(senv._phone_fields, list)):
res.append(model.model)
return res
def click2dial(self, cr, uid, erp_number, context=None):

8
base_phone/test/phonenum.yml

@ -35,3 +35,11 @@
!python {model: res.partner}: |
partner3 = self.browse(cr, uid, ref('partner3'), context=context)
assert partner3.phone == '42', 'Invalid phone numbers should not be changed'
-
Get name from phone number
-
!python {model: phone.common}: |
name = self.get_name_from_phone_number(cr, uid, '0642774266')
assert name == 'Pierre Paillet', 'Wrong result for get_name_from_phone_number'
name2 = self.get_name_from_phone_number(cr, uid, '0041216191010')
assert name2 == u'Joël Grand-Guillaume (Camptocamp)', 'Wrong result for get_name_from_phone_number (partner2)'

10
base_phone/wizard/reformat_all_phonenumbers.py

@ -40,16 +40,12 @@ class reformat_all_phonenumbers(models.TransientModel):
def run_reformat_all_phonenumbers(self, cr, uid, ids, context=None):
logger.info('Starting to reformat all the phone numbers')
phonenumbers_not_reformatted = ''
toreformat_dict = self.pool['phone.common']._get_phone_fields(
phoneobjects = self.pool['phone.common']._get_phone_fields(
cr, uid, context=context)
ctx_raise = dict(context, raise_if_phone_parse_fails=True)
for objname, prop in toreformat_dict.iteritems():
fields = []
for objname in phoneobjects:
fields = self.pool[objname]._phone_fields
obj = self.pool[objname]
if prop.get('phonefields'):
fields += prop['phonefields']
if prop.get('faxfields'):
fields += prop['faxfields']
logger.info(
'Starting to reformat phone numbers on object %s '
'(fields = %s)' % (objname, fields))

12
crm_claim_phone/crm_claim_phone.py

@ -41,15 +41,3 @@ class crm_claim(orm.Model):
cr, uid, ids, vals, context=context)
return super(crm_claim, self).write(
cr, uid, ids, vals_reformated, context=context)
class phone_common(orm.AbstractModel):
_inherit = 'phone.common'
def _get_phone_fields(self, cr, uid, context=None):
res = super(phone_common, self)._get_phone_fields(
cr, uid, context=context)
res['crm.claim'] = {
'phonefields': ['partner_phone'],
}
return res

1
crm_phone/__openerp__.py

@ -49,6 +49,7 @@ for any help or question about this module.
'wizard/number_not_found_view.xml',
'wizard/create_crm_phonecall_view.xml',
],
'test': ['test/phonenum.yml'],
'images': [],
'installable': True,
'auto_install': True,

19
crm_phone/crm_phone.py

@ -87,25 +87,6 @@ class CrmPhonecall(models.Model):
cr, uid, ids, vals_reformated, context=context)
class PhoneCommon(models.AbstractModel):
_inherit = 'phone.common'
def _get_phone_fields(self, cr, uid, context=None):
res = super(PhoneCommon, self)._get_phone_fields(
cr, uid, context=context)
res.update({
'crm.lead': {
'phonefields': ['phone', 'mobile'],
'faxfields': ['fax'],
'get_name_sequence': 20,
},
'crm.phonecall': {
'phonefields': ['partner_phone', 'partner_mobile'],
},
})
return res
class ResUsers(models.Model):
_inherit = "res.users"

41
crm_phone/test/phonenum.yml

@ -0,0 +1,41 @@
-
Write french phone numbers in national format
-
!record {model: crm.lead, id: lead1}:
name: Jacques Toufaux
mobile: 06 42 77 42 77
fax: (0) 1 45 44 42 43
country_id: base.fr
-
Write swiss phone numbers in national format
-
!record {model: crm.lead, id: lead2}:
name: Michel Content
country_id: base.ch
phone: 04 31 23 45 67
-
Create a german lead
-
!record {model: crm.lead, id: lead3}:
name: Angela Strasse
country_id: base.de
-
Check that valid phone numbers have been converted to E.164
-
!python {model: crm.lead}: |
lead1 = self.browse(cr, uid, ref('lead1'), context=context)
assert lead1.mobile == '+33642774277', 'Mobile number not written in E.164 format (lead1)'
assert lead1.fax == '+33145444243', 'Fax number not written in E.164 format (lead1)'
lead2 = self.browse(cr, uid, ref('lead2'), context=context)
assert lead2.phone == '+41431234567', 'Phone number not written in E.164 format (lead2)'
self.write(cr, uid, ref('lead3'), {'phone': '0891234567'})
lead3 = self.browse(cr, uid, ref('lead3'), context=context)
assert lead3.phone == '+49891234567', 'Phone number not written in E.164 format (lead3)'
-
Get name from phone number
-
!python {model: phone.common}: |
name = self.get_name_from_phone_number(cr, uid, '0642774277')
assert name == 'Jacques Toufaux', 'Wrong result for get_name_from_phone_number (lead1)'
name2 = self.get_name_from_phone_number(cr, uid, '0041431234567')
assert name2 == 'Michel Content', 'Wrong result for get_name_from_phone_number (lead2)'

13
event_phone/event_phone.py

@ -42,16 +42,3 @@ class event_registration(orm.Model):
cr, uid, ids, vals, context=context)
return super(event_registration, self).write(
cr, uid, ids, vals_reformated, context=context)
class phone_common(orm.AbstractModel):
_inherit = 'phone.common'
def _get_phone_fields(self, cr, uid, context=None):
res = super(phone_common, self)._get_phone_fields(
cr, uid, context=context)
res['event.registration'] = {
'phonefields': ['phone'],
'get_name_sequence': 100,
}
return res

13
hr_phone/hr_phone.py

@ -42,16 +42,3 @@ class hr_employee(orm.Model):
cr, uid, ids, vals, context=context)
return super(hr_employee, self).write(
cr, uid, ids, vals_reformated, context=context)
class phone_common(orm.AbstractModel):
_inherit = 'phone.common'
def _get_phone_fields(self, cr, uid, context=None):
res = super(phone_common, self)._get_phone_fields(
cr, uid, context=context)
res['hr.employee'] = {
'phonefields': ['work_phone', 'mobile_phone'],
'get_name_sequence': 30,
}
return res

13
hr_recruitment_phone/hr_recruitment_phone.py

@ -42,16 +42,3 @@ class hr_applicant(orm.Model):
cr, uid, ids, vals, context=context)
return super(hr_applicant, self).write(
cr, uid, ids, vals_reformated, context=context)
class phone_common(orm.AbstractModel):
_inherit = 'phone.common'
def _get_phone_fields(self, cr, uid, context=None):
res = super(phone_common, self)._get_phone_fields(
cr, uid, context=context)
res['hr.applicant'] = {
'phonefields': ['partner_phone', 'partner_mobile'],
'get_name_sequence': 50,
}
return res
Loading…
Cancel
Save