feat: download Contact as vCard

This commit is contained in:
barredterra 2024-06-16 17:49:16 +02:00
parent 639e3409e8
commit b53203e14c
3 changed files with 62 additions and 0 deletions

View file

@ -88,6 +88,17 @@ frappe.ui.form.on("Contact", {
);
}
}
if (!frm.is_dirty()) {
frm.page.add_menu_item(__("Download vCard"), function () {
window.open(
`/api/method/frappe.contacts.doctype.contact.contact.download_vcard?contact=${encodeURIComponent(
frm.doc.name
)}`,
"_blank"
);
});
}
},
validate: function (frm) {
// clear linked customer / supplier / sales partner on saving...

View file

@ -167,6 +167,56 @@ class Contact(Document):
def _get_full_name(self) -> str:
return get_full_name(self.first_name, self.middle_name, self.last_name, self.company_name)
def get_vcard(self):
from vobject import vCard
from vobject.vcard import Name
vcard = vCard()
vcard.add("fn").value = self.full_name
name = Name()
if self.first_name:
name.given = self.first_name
if self.last_name:
name.family = self.last_name
if self.middle_name:
name.additional = self.middle_name
vcard.add("n").value = name
if self.designation:
vcard.add("title").value = self.designation
for row in self.email_ids:
email = vcard.add("email")
email.value = row.email_id
if row.is_primary:
email.type_param = "pref"
for row in self.phone_nos:
tel = vcard.add("tel")
tel.value = row.phone
if row.is_primary_phone:
tel.type_param = "home"
if row.is_primary_mobile_no:
tel.type_param = "cell"
return vcard
@frappe.whitelist()
def download_vcard(contact: str):
"""Download vCard for the contact"""
contact = frappe.get_doc("Contact", contact)
vcard = contact.get_vcard()
frappe.response["filename"] = f"{contact.name}.vcf"
frappe.response["filecontent"] = vcard.serialize().encode("utf-8")
frappe.response["type"] = "binary"
def get_default_contact(doctype, name):
"""Return default contact for the given doctype, name."""

View file

@ -86,6 +86,7 @@ dependencies = [
"google-auth-oauthlib~=0.4.4",
"google-auth~=1.29.0",
"posthog~=3.0.1",
"vobject~=0.9.7",
]
[project.urls]