diff --git a/crm/api/comment.py b/crm/api/comment.py
index 7d78f0d4e..c3b7ca65a 100644
--- a/crm/api/comment.py
+++ b/crm/api/comment.py
@@ -5,6 +5,7 @@
from bs4 import BeautifulSoup
from crm.fcrm.doctype.crm_notification.crm_notification import notify_user
+
def on_update(self, method):
notify_mentions(self)
@@ -24,25 +25,31 @@ def notify_mentions(doc):
doctype = doc.reference_doctype
if doctype.startswith("CRM "):
doctype = doctype[4:].lower()
- name = reference_doc.lead_name or name if doctype == "lead" else reference_doc.organization or reference_doc.lead_name or name
+ name = (
+ reference_doc.lead_name
+ if doctype == "lead"
+ else reference_doc.organization or reference_doc.lead_name
+ )
notification_text = f"""
-
- { owner }
+
+ { owner }{ _('mentioned you in {0}').format(doctype) }
- { name }
+ { name }
"""
- notify_user({
- "owner": doc.owner,
- "assigned_to": mention.email,
- "notification_type": "Mention",
- "message": doc.content,
- "notification_text": notification_text,
- "reference_doctype": "Comment",
- "reference_docname": doc.name,
- "redirect_to_doctype": doc.reference_doctype,
- "redirect_to_docname": doc.reference_name,
- })
+ notify_user(
+ {
+ "owner": doc.owner,
+ "assigned_to": mention.email,
+ "notification_type": "Mention",
+ "message": doc.content,
+ "notification_text": notification_text,
+ "reference_doctype": "Comment",
+ "reference_docname": doc.name,
+ "redirect_to_doctype": doc.reference_doctype,
+ "redirect_to_docname": doc.reference_name,
+ }
+ )
def extract_mentions(html):
@@ -56,39 +63,42 @@ def extract_mentions(html):
)
return mentions
+
@frappe.whitelist()
def add_attachments(name: str, attachments: Iterable[str | dict]) -> None:
- """Add attachments to the given Comment
+ """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
+ :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_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
+ _file = frappe.new_doc("File")
+ _file.update(file_args)
+ _file.save(ignore_permissions=True)
diff --git a/crm/api/todo.py b/crm/api/todo.py
index f30e19f41..dc9dcf28b 100644
--- a/crm/api/todo.py
+++ b/crm/api/todo.py
@@ -2,102 +2,131 @@
from frappe import _
from crm.fcrm.doctype.crm_notification.crm_notification import notify_user
+
def after_insert(doc, method):
- if doc.reference_type in ["CRM Lead", "CRM Deal"] and doc.reference_name and doc.allocated_to:
- fieldname = "lead_owner" if doc.reference_type == "CRM Lead" else "deal_owner"
- lead_owner = frappe.db.get_value(doc.reference_type, doc.reference_name, fieldname)
- if not lead_owner:
- frappe.db.set_value(doc.reference_type, doc.reference_name, fieldname, doc.allocated_to)
+ if (
+ doc.reference_type in ["CRM Lead", "CRM Deal"]
+ and doc.reference_name
+ and doc.allocated_to
+ ):
+ fieldname = "lead_owner" if doc.reference_type == "CRM Lead" else "deal_owner"
+ lead_owner = frappe.db.get_value(
+ doc.reference_type, doc.reference_name, fieldname
+ )
+ if not lead_owner:
+ frappe.db.set_value(
+ doc.reference_type, doc.reference_name, fieldname, doc.allocated_to
+ )
+
+ if (
+ doc.reference_type in ["CRM Lead", "CRM Deal", "CRM Task"]
+ and doc.reference_name
+ and doc.allocated_to
+ ):
+ notify_assigned_user(doc)
- if doc.reference_type in ["CRM Lead", "CRM Deal", "CRM Task"] and doc.reference_name and doc.allocated_to:
- notify_assigned_user(doc)
def on_update(doc, method):
- if doc.has_value_changed("status") and doc.status == "Cancelled" and doc.reference_type in ["CRM Lead", "CRM Deal", "CRM Task"] and doc.reference_name and doc.allocated_to:
- notify_assigned_user(doc, is_cancelled=True)
+ if (
+ doc.has_value_changed("status")
+ and doc.status == "Cancelled"
+ and doc.reference_type in ["CRM Lead", "CRM Deal", "CRM Task"]
+ and doc.reference_name
+ and doc.allocated_to
+ ):
+ notify_assigned_user(doc, is_cancelled=True)
+
def notify_assigned_user(doc, is_cancelled=False):
- _doc = frappe.get_doc(doc.reference_type, doc.reference_name)
- owner = frappe.get_cached_value("User", frappe.session.user, "full_name")
- notification_text = get_notification_text(owner, doc, _doc, is_cancelled)
-
- message = _("Your assignment on {0} {1} has been removed by {2}").format(
- doc.reference_type,
- doc.reference_name,
- owner
- ) if is_cancelled else _("{0} assigned a {1} {2} to you").format(
- owner,
- doc.reference_type,
- doc.reference_name
- )
-
- redirect_to_doctype, redirect_to_name = get_redirect_to_doc(doc)
-
- notify_user({
- "owner": frappe.session.user,
- "assigned_to": doc.allocated_to,
- "notification_type": "Assignment",
- "message": message,
- "notification_text": notification_text,
- "reference_doctype": doc.reference_type,
- "reference_docname": doc.reference_name,
- "redirect_to_doctype": redirect_to_doctype,
- "redirect_to_docname": redirect_to_name,
- })
+ _doc = frappe.get_doc(doc.reference_type, doc.reference_name)
+ owner = frappe.get_cached_value("User", frappe.session.user, "full_name")
+ notification_text = get_notification_text(owner, doc, _doc, is_cancelled)
+
+ message = (
+ _("Your assignment on {0} {1} has been removed by {2}").format(
+ doc.reference_type, doc.reference_name, owner
+ )
+ if is_cancelled
+ else _("{0} assigned a {1} {2} to you").format(
+ owner, doc.reference_type, doc.reference_name
+ )
+ )
+
+ redirect_to_doctype, redirect_to_name = get_redirect_to_doc(doc)
+
+ notify_user(
+ {
+ "owner": frappe.session.user,
+ "assigned_to": doc.allocated_to,
+ "notification_type": "Assignment",
+ "message": message,
+ "notification_text": notification_text,
+ "reference_doctype": doc.reference_type,
+ "reference_docname": doc.reference_name,
+ "redirect_to_doctype": redirect_to_doctype,
+ "redirect_to_docname": redirect_to_name,
+ }
+ )
+
def get_notification_text(owner, doc, reference_doc, is_cancelled=False):
- name = doc.reference_name
- doctype = doc.reference_type
-
- if doctype.startswith("CRM "):
- doctype = doctype[4:].lower()
-
- if doctype in ["lead", "deal"]:
- name = reference_doc.lead_name or name if doctype == "lead" else reference_doc.organization or reference_doc.lead_name or name
-
- if is_cancelled:
- return f"""
-
- { _('Your assignment on {0} {1} has been removed by {2}').format(
- doctype,
- f'{ name }',
- f'{ owner }'
- ) }
-
- """
-
- return f"""
-
- { owner }
- { _('assigned a {0} {1} to you').format(
- doctype,
- f'{ name }'
- ) }
-
- """
-
- if doctype == "task":
- if is_cancelled:
- return f"""
-
- { _('Your assignment on task {0} has been removed by {1}').format(
- f'{ reference_doc.title }',
- f'{ owner }'
- ) }
-
- """
- return f"""
-
- { owner }
- { _('assigned a new task {0} to you').format(
- f'{ reference_doc.title }'
- ) }
-
- """
+ name = doc.reference_name
+ doctype = doc.reference_type
+
+ if doctype.startswith("CRM "):
+ doctype = doctype[4:].lower()
+
+ if doctype in ["lead", "deal"]:
+ name = (
+ reference_doc.lead_name or name
+ if doctype == "lead"
+ else reference_doc.organization or reference_doc.lead_name or name
+ )
+
+ if is_cancelled:
+ return f"""
+
+ { _('Your assignment on {0} {1} has been removed by {2}').format(
+ doctype,
+ f'{ name }',
+ f'{ owner }'
+ ) }
+
+ """
+
+ return f"""
+
+ { owner }
+ { _('assigned a {0} {1} to you').format(
+ doctype,
+ f'{ name }'
+ ) }
+
+ """
+
+ if doctype == "task":
+ if is_cancelled:
+ return f"""
+
+ { _('Your assignment on task {0} has been removed by {1}').format(
+ f'{ reference_doc.title }',
+ f'{ owner }'
+ ) }
+
+ """
+ return f"""
+
+ { owner }
+ { _('assigned a new task {0} to you').format(
+ f'{ reference_doc.title }'
+ ) }
+