diff --git a/CHANGELOG b/CHANGELOG
index b82cff7a83..8506705b43 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,4 @@
+* Add contact_mechanism_get
* Add name to contact mechanism
* Improve distinction of address record name
diff --git a/contact_mechanism.py b/contact_mechanism.py
index 95cbfbca9c..3d62bb4f5b 100644
--- a/contact_mechanism.py
+++ b/contact_mechanism.py
@@ -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
diff --git a/party.py b/party.py
index 5efe081db0..409eb4046c 100644
--- a/party.py
+++ b/party.py
@@ -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"
diff --git a/tests/test_party.py b/tests/test_party.py
index 7e5d958613..5de253b920 100644
--- a/tests/test_party.py
+++ b/tests/test_party.py
@@ -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()
diff --git a/view/contact_mechanism_tree.xml b/view/contact_mechanism_tree.xml
index 4b0c74a765..3c624ee08d 100644
--- a/view/contact_mechanism_tree.xml
+++ b/view/contact_mechanism_tree.xml
@@ -6,4 +6,5 @@ this repository contains the full copyright notices and license terms. -->
+