From 7dd23c8ad973f1f73c49912de1212e7fc6c70798 Mon Sep 17 00:00:00 2001
From: Juliana <juliana@le-filament.com>
Date: Thu, 16 Mar 2023 15:03:57 +0100
Subject: [PATCH] [UPD]Update copyclipboard function

---
 models/res_partner.py | 15 ++++++++-------
 views/res_partner.xml | 16 +++++-----------
 2 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/models/res_partner.py b/models/res_partner.py
index 8a6d79a..23f1131 100644
--- a/models/res_partner.py
+++ b/models/res_partner.py
@@ -4,13 +4,10 @@
 import logging
 from collections import defaultdict
 
-import pyperclip
-
 from odoo import api, fields, models
 
 _logger = logging.getLogger(__name__)
 
-# ADDRESS_FIELDS = ('street', 'street2', 'zip', 'city')
 
 class ResPartner(models.Model):
     _inherit = "res.partner"
@@ -109,6 +106,7 @@ class ResPartner(models.Model):
 
     contact_address_simple = fields.Char(
         compute="_compute_contact_address_simple", string="Adresse simplifiée")
+    fields_to_copy_to_clipboard = fields.Char("Champs à copier", compute="_compute_copy_to_clipboard", )
     # ------------------------------------------------------
     # SQL Constraints
     # ------------------------------------------------------
@@ -120,6 +118,13 @@ class ResPartner(models.Model):
     # ------------------------------------------------------
     # Computed fields / Search Fields
     # ------------------------------------------------------
+    def _compute_copy_to_clipboard(self):
+        for record in self:
+            res = (record.lastname or "") + " " + (record.firstname or "") + "\n" + (record.street or "") + "\n" + (record.street2 or "") + "\n" + (record.street3 or "") + "\n" + (record.zip or "") + " " + (record.city or "") + "\n" + (record.parent_id.name or "") + "\n" + (record.mobile or "") + "\n" + (record.email or "")
+            while "\n\n" in res:
+                res = res.replace("\n\n", "\n")
+            record.fields_to_copy_to_clipboard = res.lstrip()
+
     @api.model
     def _get_default_address_format_simple(self):
         return "%(street)s\n%(street2)s\n%(street3)s\n%(city)s %(zip)s"
@@ -262,10 +267,6 @@ class ResPartner(models.Model):
     # ------------------------------------------------------
     # Actions
     # ------------------------------------------------------
-    def copy_to_clipboard(self):
-        for record in self:
-            text_to_copy = (record.lastname or "") + " " + (record.firstname or "") + "\n" + (record.street or "") + "\n" + (record.street2 or "") + "\n" + (record.zip or "") + " " + (record.city or "") + "\n" + (record.parent_id.name or "") + "\n" + (record.mobile or "") + "\n" + (record.email or "")
-            pyperclip.copy(text_to_copy)
 
     # ------------------------------------------------------
     # Business methods
diff --git a/views/res_partner.xml b/views/res_partner.xml
index 3c71ed3..b39cfdb 100644
--- a/views/res_partner.xml
+++ b/views/res_partner.xml
@@ -262,17 +262,6 @@
             <field name="inherit_id" ref="base.view_partner_form" />
             <field name="priority">100</field>
             <field name="arch" type="xml">
-                <xpath expr="//sheet" position="before">
-                    <header>
-                        <button
-                            string="Copier les infos du contact"
-                            name="copy_to_clipboard"
-                            type="object"
-                            class="oe_highlight"
-                            attrs="{'invisible': [('parent_id', '=', False)]}"
-                        />
-                    </header>
-                </xpath>
                 <xpath expr="//div[@name='button_box']" position="after">
                     <div
                         class="alert alert-warning"
@@ -350,6 +339,11 @@
                     <group name="alert" class="oe_edit_only">
                         <field name="alert" />
                     </group>
+                    <group>
+                        <div>
+                            <field name="fields_to_copy_to_clipboard" widget="CopyClipboardChar" options="{'string': 'Copier les données'}"/>
+                        </div>
+                    </group>
                 </xpath>
 
                 <!-- Person -->
-- 
GitLab