Add contact_mechanism_get on Party

Similar to address_get but for contact mechanisms with types and usage
parameters.
It also adds the method 'usages' to allow other models to retrieve a selection
list ready to use. Each module must inherit it and append the field name to
the _fields parameter.

issue7093
review35901002
This commit is contained in:
Cédric Krier
2018-02-19 19:14:42 +01:00
parent fa3d7d8be3
commit 895a0d4344
5 changed files with 173 additions and 0 deletions
+1
View File
@@ -1,3 +1,4 @@
* Add contact_mechanism_get
* Add name to contact mechanism
* Improve distinction of address record name
+9
View File
@@ -269,3 +269,12 @@ class ContactMechanism(sequence_ordered(), ModelSQL, ModelView):
'phone': self.value,
'party': self.party.rec_name
})
@classmethod
def usages(cls, _fields=None):
"Returns the selection list of usage"
usages = [(None, "")]
if _fields:
for name, desc in cls.fields_get(_fields).iteritems():
usages.append((name, desc['string']))
return usages
+21
View File
@@ -236,6 +236,27 @@ class Party(ModelSQL, ModelView, MultiValueMixin):
return address
return default_address
def contact_mechanism_get(self, types=None, usage=None):
"""
Try to find a contact mechanism for the given types and usage, if no
usage matches the first mechanism of the given types is returned.
"""
default_mechanism = None
if types:
if isinstance(types, basestring):
types = {types}
mechanisms = [m for m in self.contact_mechanisms
if m.type in types]
else:
mechanisms = self.contact_mechanisms
if mechanisms:
default_mechanism = mechanisms[0]
if usage:
for mechanism in mechanisms:
if getattr(mechanism, usage):
return mechanism
return default_mechanism
class PartyLang(ModelSQL, ValueMixin):
"Party Lang"
+141
View File
@@ -273,6 +273,147 @@ class PartyTestCase(ModuleTestCase):
# Test acceptance of a non-phone value when type is non-phone
mechanism = create('email', 'name@example.com')
@with_transaction()
def test_contact_mechanism_get_no_usage(self):
"Test contact_mechanism_get with no usage"
pool = Pool()
Party = pool.get('party.party')
ContactMechanism = pool.get('party.contact_mechanism')
party, = Party.create([{}])
contact1, contact2 = ContactMechanism.create([{
'party': party.id,
'sequence': 1,
'type': 'email',
'value': 'test1@example.com',
}, {
'party': party.id,
'sequence': 2,
'type': 'email',
'value': 'test2@example.com',
}])
contact = party.contact_mechanism_get('email')
self.assertEqual(contact, contact1)
@with_transaction()
def test_contact_mechanism_get_many_types(self):
"Test contact_mechanism_get with many types"
pool = Pool()
Party = pool.get('party.party')
ContactMechanism = pool.get('party.contact_mechanism')
party, = Party.create([{}])
contact1, contact2 = ContactMechanism.create([{
'party': party.id,
'sequence': 1,
'type': 'other',
'value': 'test',
}, {
'party': party.id,
'sequence': 2,
'type': 'email',
'value': 'test2@example.com',
}])
contact = party.contact_mechanism_get({'email', 'phone'})
self.assertEqual(contact, contact2)
@with_transaction()
def test_contact_mechanism_get_no_contact_mechanism(self):
"Test contact_mechanism_get with no contact mechanism"
pool = Pool()
Party = pool.get('party.party')
party, = Party.create([{}])
contact = party.contact_mechanism_get()
self.assertEqual(contact, None)
@with_transaction()
def test_contact_mechanism_get_no_type(self):
"Test contact_mechanism_get with no type"
pool = Pool()
Party = pool.get('party.party')
ContactMechanism = pool.get('party.contact_mechanism')
party, = Party.create([{}])
ContactMechanism.create([{
'party': party.id,
'type': 'email',
'value': 'test1@example.com',
}])
contact = party.contact_mechanism_get('phone')
self.assertEqual(contact, None)
@with_transaction()
def test_contact_mechanism_get_any_type(self):
"Test contact_mechanism_get with any type"
pool = Pool()
Party = pool.get('party.party')
ContactMechanism = pool.get('party.contact_mechanism')
party, = Party.create([{}])
email1, = ContactMechanism.create([{
'party': party.id,
'type': 'email',
'value': 'test1@example.com',
}])
contact = party.contact_mechanism_get()
self.assertEqual(contact, email1)
@with_transaction()
def test_contact_mechanism_get_inactive(self):
"Test contact_mechanism_get with inactive"
pool = Pool()
Party = pool.get('party.party')
ContactMechanism = pool.get('party.contact_mechanism')
party, = Party.create([{}])
contact1, contact2 = ContactMechanism.create([{
'party': party.id,
'sequence': 1,
'type': 'email',
'value': 'test1@example.com',
'active': False,
}, {
'party': party.id,
'sequence': 2,
'type': 'email',
'value': 'test2@example.com',
'active': True,
}])
contact = party.contact_mechanism_get()
self.assertEqual(contact, contact2)
@with_transaction()
def test_contact_mechanism_get_usage(self):
"Test contact_mechanism_get with usage"
pool = Pool()
Party = pool.get('party.party')
ContactMechanism = pool.get('party.contact_mechanism')
party, = Party.create([{}])
contact1, contact2 = ContactMechanism.create([{
'party': party.id,
'sequence': 1,
'type': 'email',
'value': 'test1@example.com',
'name': None,
}, {
'party': party.id,
'sequence': 2,
'type': 'email',
'value': 'test2@example.com',
'name': 'email',
}])
contact = party.contact_mechanism_get(usage='name')
self.assertEqual(contact, contact2)
def suite():
suite = trytond.tests.test_tryton.suite()
+1
View File
@@ -6,4 +6,5 @@ this repository contains the full copyright notices and license terms. -->
<field name="value"/>
<field name="name"/>
<field name="party"/>
<field name="active"/>
</tree>