From ee678f97c81dab9c71fb208448e940dd7bb34940 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Tue, 30 Apr 2024 14:39:43 +0530 Subject: [PATCH] feat: add attachments in comment --- crm/api/activities.py | 10 +++-- crm/api/comment.py | 41 ++++++++++++++++++- frontend/src/components/Activities.vue | 13 +++++- frontend/src/components/CommunicationArea.vue | 8 +++- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/crm/api/activities.py b/crm/api/activities.py index d04678d2..3cd9e9f3 100644 --- a/crm/api/activities.py +++ b/crm/api/activities.py @@ -105,6 +105,7 @@ def get_deal_activities(name): "creation": comment.creation, "owner": comment.owner, "content": comment.content, + "attachments": get_attachments('Comment', comment.name), "is_lead": False, } activities.append(activity) @@ -121,7 +122,7 @@ def get_deal_activities(name): "recipients": communication.recipients, "cc": communication.cc, "bcc": communication.bcc, - "attachments": get_attachments(communication.name), + "attachments": get_attachments('Communication', communication.name), "read_by_recipient": communication.read_by_recipient, }, "is_lead": False, @@ -216,6 +217,7 @@ def get_lead_activities(name): "creation": comment.creation, "owner": comment.owner, "content": comment.content, + "attachments": get_attachments('Comment', comment.name), "is_lead": True, } activities.append(activity) @@ -232,7 +234,7 @@ def get_lead_activities(name): "recipients": communication.recipients, "cc": communication.cc, "bcc": communication.bcc, - "attachments": get_attachments(communication.name), + "attachments": get_attachments('Communication', communication.name), "read_by_recipient": communication.read_by_recipient, }, "is_lead": True, @@ -249,10 +251,10 @@ def get_lead_activities(name): return activities, calls, notes, tasks @redis_cache() -def get_attachments(name): +def get_attachments(doctype, name): return frappe.db.get_all( "File", - filters={"attached_to_doctype": "Communication", "attached_to_name": name}, + filters={"attached_to_doctype": doctype, "attached_to_name": name}, fields=["name", "file_name", "file_url", "file_size", "is_private"], ) diff --git a/crm/api/comment.py b/crm/api/comment.py index 378a01b8..0333d174 100644 --- a/crm/api/comment.py +++ b/crm/api/comment.py @@ -1,3 +1,5 @@ +from collections.abc import Iterable + import frappe from frappe import _ from bs4 import BeautifulSoup @@ -54,4 +56,41 @@ def extract_mentions(html): mentions.append( frappe._dict(full_name=d.get("data-label"), email=d.get("data-id")) ) - return mentions \ No newline at end of file + return mentions + +@frappe.whitelist() +def add_attachments(name: str, attachments: Iterable[str | dict]) -> None: + """Add attachments to the given Comment + + :param name: Comment name + :param attachments: File names or dicts with keys "fname" and "fcontent" + """ + # loop through attachments + for a in attachments: + if isinstance(a, str): + attach = frappe.db.get_value("File", {"name": a}, ["file_url", "is_private"], as_dict=1) + file_args = { + "file_url": attach.file_url, + "is_private": attach.is_private, + } + elif isinstance(a, dict) and "fcontent" in a and "fname" in a: + # dict returned by frappe.attach_print() + file_args = { + "file_name": a["fname"], + "content": a["fcontent"], + "is_private": 1, + } + else: + continue + + file_args.update( + { + "attached_to_doctype": "Comment", + "attached_to_name": name, + "folder": "Home/Attachments", + } + ) + + _file = frappe.new_doc("File") + _file.update(file_args) + _file.save(ignore_permissions=True) \ No newline at end of file diff --git a/frontend/src/components/Activities.vue b/frontend/src/components/Activities.vue index c914a125..642de000 100644 --- a/frontend/src/components/Activities.vue +++ b/frontend/src/components/Activities.vue @@ -474,8 +474,17 @@
+ > +
+
+ +
+