changeset 61687ca83ba3 in modules/party:default
details: https://hg.tryton.org/modules/party?cmd=changeset&node=61687ca83ba3
description:
        Add optional address to contact mechanism

        issue11705
        review421691006
diffstat:

 CHANGELOG                                |   2 +
 address.py                               |  17 ++++++++++++++-
 contact_mechanism.py                     |  35 ++++++++++++++++++++++++++++++++
 party.py                                 |  27 +++---------------------
 view/address_form.xml                    |   3 ++
 view/contact_mechanism_tree.xml          |   1 +
 view/contact_mechanism_tree_sequence.xml |   1 +
 7 files changed, 62 insertions(+), 24 deletions(-)

diffs (193 lines):

diff -r 288942e9f597 -r 61687ca83ba3 CHANGELOG
--- a/CHANGELOG Mon Sep 19 21:25:55 2022 +0200
+++ b/CHANGELOG Sat Oct 01 14:52:06 2022 +0200
@@ -1,3 +1,5 @@
+* Add optional address to contact mechanism
+
 Version 6.4.0 - 2022-05-02
 * Bug fixes (see mercurial logs for details)
 * Always show country name in English language
diff -r 288942e9f597 -r 61687ca83ba3 address.py
--- a/address.py        Mon Sep 19 21:25:55 2022 +0200
+++ b/address.py        Sat Oct 01 14:52:06 2022 +0200
@@ -19,10 +19,13 @@
 from trytond.rpc import RPC
 from trytond.transaction import Transaction
 
+from .contact_mechanism import _ContactMechanismMixin
 from .exceptions import InvalidFormat
 
 
-class Address(DeactivableMixin, sequence_ordered(), ModelSQL, ModelView):
+class Address(
+        DeactivableMixin, sequence_ordered(), _ContactMechanismMixin,
+        ModelSQL, ModelView):
     "Address"
     __name__ = 'party.address'
     party = fields.Many2One('party.party', 'Party', required=True,
@@ -62,6 +65,11 @@
             ('party', '=', Eval('party')),
             ('type', 'in', ['fr_siret']),
             ])
+    contact_mechanisms = fields.One2Many(
+        'party.contact_mechanism', 'address', "Contact Mechanisms",
+        domain=[
+            ('party', '=', Eval('party', -1)),
+            ])
 
     @classmethod
     def __setup__(cls):
@@ -264,6 +272,13 @@
         Types = pool.get('party.address.subdivision_type')
         return Types.get_types(self.country)
 
+    def contact_mechanism_get(self, types=None, usage=None):
+        mechanism = super().contact_mechanism_get(types=types, usage=usage)
+        if mechanism is None:
+            mechanism = self.party.contact_mechanism_get(
+                types=types, usage=usage)
+        return mechanism
+
 
 class AddressFormat(DeactivableMixin, MatchMixin, ModelSQL, ModelView):
     "Address Format"
diff -r 288942e9f597 -r 61687ca83ba3 contact_mechanism.py
--- a/contact_mechanism.py      Mon Sep 19 21:25:55 2022 +0200
+++ b/contact_mechanism.py      Sat Oct 01 14:52:06 2022 +0200
@@ -38,6 +38,31 @@
     }
 
 
+class _ContactMechanismMixin:
+    __slots__ = ()
+
+    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, str):
+                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 ContactMechanism(
         DeactivableMixin, sequence_ordered(), ModelSQL, ModelView,
         MultiValueMixin):
@@ -56,6 +81,11 @@
     comment = fields.Text("Comment")
     party = fields.Many2One(
         'party.party', "Party", required=True, ondelete='CASCADE', select=True)
+    address = fields.Many2One(
+        'party.address', "Address", ondelete='CASCADE', select=True,
+        domain=[
+            ('party', '=', Eval('party', -1)),
+            ])
     language = fields.MultiValue(
         fields.Many2One('ir.lang', "Language",
             help="Used to translate communication made "
@@ -107,6 +137,11 @@
     def default_party(cls):
         return Transaction().context.get('related_party')
 
+    @fields.depends('address', '_parent_address.party')
+    def on_change_address(self):
+        if self.address:
+            self.party = self.address.party
+
     @classmethod
     def get_value(cls, mechanisms, names):
         return dict((name, dict((m.id, m.value) for m in mechanisms))
diff -r 288942e9f597 -r 61687ca83ba3 party.py
--- a/party.py  Mon Sep 19 21:25:55 2022 +0200
+++ b/party.py  Sat Oct 01 14:52:06 2022 +0200
@@ -19,12 +19,14 @@
 from trytond.transaction import Transaction
 from trytond.wizard import Button, StateTransition, StateView, Wizard
 
-from .contact_mechanism import _PHONE_TYPES
+from .contact_mechanism import _PHONE_TYPES, _ContactMechanismMixin
 from .exceptions import (
     EraseError, InvalidIdentifierCode, SimilarityWarning, VIESUnavailable)
 
 
-class Party(DeactivableMixin, ModelSQL, ModelView, MultiValueMixin):
+class Party(
+        DeactivableMixin, _ContactMechanismMixin, ModelSQL, ModelView,
+        MultiValueMixin):
     "Party"
     __name__ = 'party.party'
 
@@ -324,27 +326,6 @@
                         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, str):
-                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 -r 288942e9f597 -r 61687ca83ba3 view/address_form.xml
--- a/view/address_form.xml     Mon Sep 19 21:25:55 2022 +0200
+++ b/view/address_form.xml     Sat Oct 01 14:52:06 2022 +0200
@@ -35,5 +35,8 @@
         <page name="identifiers">
             <field name="identifiers" colspan="4" pre_validate="1" 
view_ids="party.identifier_list_sequence"/>
         </page>
+        <page name="contact_mechanisms">
+            <field name="contact_mechanisms" colspan="4" pre_validate="1" 
view_ids="party.contact_mechanism_view_tree_sequence"/>
+        </page>
     </notebook>
 </form>
diff -r 288942e9f597 -r 61687ca83ba3 view/contact_mechanism_tree.xml
--- a/view/contact_mechanism_tree.xml   Mon Sep 19 21:25:55 2022 +0200
+++ b/view/contact_mechanism_tree.xml   Sat Oct 01 14:52:06 2022 +0200
@@ -6,5 +6,6 @@
     <field name="value" expand="1"/>
     <field name="name" expand="1" optional="0"/>
     <field name="party" expand="2" optional="0"/>
+    <field name="address" expand="1" optional="0"/>
     <field name="language" widget="selection" optional="1"/>
 </tree>
diff -r 288942e9f597 -r 61687ca83ba3 view/contact_mechanism_tree_sequence.xml
--- a/view/contact_mechanism_tree_sequence.xml  Mon Sep 19 21:25:55 2022 +0200
+++ b/view/contact_mechanism_tree_sequence.xml  Sat Oct 01 14:52:06 2022 +0200
@@ -6,6 +6,7 @@
     <field name="value" expand="1"/>
     <field name="name" expand="1" optional="0"/>
     <field name="party" expand="2" optional="0"/>
+    <field name="address" expand="1" optional="0"/>
     <field name="language" widget="selection" optional="1"/>
     <field name="url" widget="url"/>
 </tree>

Reply via email to