1
0
forked from test/crm
jcrm/crm/api/comment.py

105 lines
3.3 KiB
Python

from collections.abc import Iterable
import jingrow
from jingrow import _
from bs4 import BeautifulSoup
from crm.fcrm.pagetype.crm_notification.crm_notification import notify_user
def on_update(self, method):
notify_mentions(self)
def notify_mentions(pg):
"""
Extract mentions from `content`, and notify.
`content` must have `HTML` content.
"""
content = getattr(pg, "content", None)
if not content:
return
mentions = extract_mentions(content)
reference_pg = jingrow.get_pg(pg.reference_pagetype, pg.reference_name)
for mention in mentions:
owner = jingrow.get_cached_value("User", pg.owner, "full_name")
pagetype = pg.reference_pagetype
if pagetype.startswith("CRM "):
pagetype = pagetype[4:].lower()
name = (
reference_pg.lead_name
if pagetype == "lead"
else reference_pg.organization or reference_pg.lead_name
)
notification_text = f"""
<div class="mb-2 leading-5 text-ink-gray-5">
<span class="font-medium text-ink-gray-9">{ owner }</span>
<span>{ _('mentioned you in {0}').format(pagetype) }</span>
<span class="font-medium text-ink-gray-9">{ name }</span>
</div>
"""
notify_user(
{
"owner": pg.owner,
"assigned_to": mention.email,
"notification_type": "Mention",
"message": pg.content,
"notification_text": notification_text,
"reference_pagetype": "Comment",
"reference_docname": pg.name,
"redirect_to_pagetype": pg.reference_pagetype,
"redirect_to_docname": pg.reference_name,
}
)
def extract_mentions(html):
if not html:
return []
soup = BeautifulSoup(html, "html.parser")
mentions = []
for d in soup.find_all("span", attrs={"data-type": "mention"}):
mentions.append(
jingrow._dict(full_name=d.get("data-label"), email=d.get("data-id"))
)
return mentions
@jingrow.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 = jingrow.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 jingrow.attach_print()
file_args = {
"file_name": a["fname"],
"content": a["fcontent"],
"is_private": 1,
}
else:
continue
file_args.update(
{
"attached_to_pagetype": "Comment",
"attached_to_name": name,
"folder": "Home/Attachments",
}
)
_file = jingrow.new_pg("File")
_file.update(file_args)
_file.save(ignore_permissions=True)