diff --git a/community/community/doctype/community_member/community_member.json b/community/community/doctype/community_member/community_member.json index 58c40451..1b45eeb6 100644 --- a/community/community/doctype/community_member/community_member.json +++ b/community/community/doctype/community_member/community_member.json @@ -8,14 +8,19 @@ "editable_grid": 1, "engine": "InnoDB", "field_order": [ - "enabled", "full_name", "email", + "enabled", + "column_break_4", "role", - "photo", "short_intro", + "section_break_7", "bio", + "section_break_9", "username", + "photo", + "column_break_12", + "email_preference", "route" ], "fields": [ @@ -29,6 +34,7 @@ "fieldname": "full_name", "fieldtype": "Data", "in_list_view": 1, + "in_standard_filter": 1, "label": "Full Name", "reqd": 1 }, @@ -64,6 +70,7 @@ { "fieldname": "email", "fieldtype": "Data", + "in_standard_filter": 1, "label": "Email", "options": "Email", "reqd": 1, @@ -74,12 +81,34 @@ "fieldtype": "Data", "label": "User Name", "unique": 1 + }, + { + "fieldname": "email_preference", + "fieldtype": "Select", + "label": "Email preference", + "options": "Email on every Message\nOne Digest Mail per day\nNever" + }, + { + "fieldname": "column_break_4", + "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_7", + "fieldtype": "Section Break" + }, + { + "fieldname": "section_break_9", + "fieldtype": "Section Break" + }, + { + "fieldname": "column_break_12", + "fieldtype": "Column Break" } ], "has_web_view": 1, "index_web_pages_for_search": 1, "links": [], - "modified": "2021-03-22 12:16:18.823037", + "modified": "2021-03-30 17:25:57.075151", "modified_by": "Administrator", "module": "Community", "name": "Community Member", diff --git a/community/hooks.py b/community/hooks.py index edfdd032..0836b34c 100644 --- a/community/hooks.py +++ b/community/hooks.py @@ -98,24 +98,11 @@ doc_events = { # Scheduled Tasks # --------------- - -# scheduler_events = { -# "all": [ -# "community.tasks.all" -# ], -# "daily": [ -# "community.tasks.daily" -# ], -# "hourly": [ -# "community.tasks.hourly" -# ], -# "weekly": [ -# "community.tasks.weekly" -# ] -# "monthly": [ -# "community.tasks.monthly" -# ] -# } +scheduler_events = { + "daily": [ + "erpnext.stock.reorder_item.reorder_item" + ] +} # Testing # ------- diff --git a/community/lms/doctype/lms_message/lms_message.py b/community/lms/doctype/lms_message/lms_message.py index 4ce718ba..739f07b6 100644 --- a/community/lms/doctype/lms_message/lms_message.py +++ b/community/lms/doctype/lms_message/lms_message.py @@ -3,8 +3,57 @@ # For license information, please see license.txt from __future__ import unicode_literals -# import frappe +import frappe from frappe.model.document import Document - +from frappe import _ +from frappe.utils import add_days, nowdate class LMSMessage(Document): - pass + def after_insert(self): + membership = frappe.get_all("LMS Batch Membership", {"batch": self.batch}, ["member"]) + for entry in membership: + member = frappe.get_doc("Community Member", entry.member) + if member.name != self.author and member.email_preference == "Email on every Message": + frappe.sendmail( + recipients = member.email, + subject = _("New Message on ") + self.batch, + header = _("New Message on ") + self.batch, + template = "lms_message", + args = { + "author": self.author, + "message": frappe.utils.md_to_html(self.message), + "creation": frappe.utils.format_datetime(self.creation, "medium"), + "course": frappe.db.get_value("LMS Batch", self.batch, ["course"]) + } + ) +def send_daily_digest(): + emails = frappe._dict() + messages = frappe.get_all("LMS Message", {"creation": [">=", add_days(nowdate(), -1)]}, ["message", "batch", "author", "creation"]) + for message in messages: + membership = frappe.get_all("LMS Batch Membership", {"batch": message.batch}, ["member"]) + for entry in membership: + member = frappe.db.get_value("Community Member", entry.member, ["name", "email_preference", "email"], as_dict=1) + if member.name != message.author and member.email_preference == "One Digest Mail per day": + if member.name in emails.keys(): + emails[member.name]["messages"].append(message) + else: + emails[member.name] = frappe._dict({ + "email": member.email, + "messages": [message] + }) + for email in emails: + group_by_batch = frappe._dict() + for message in emails[email]["messages"]: + if message.batch in group_by_batch.keys(): + group_by_batch[message.batch].append(message) + else: + group_by_batch[message.batch] = [message] + frappe.sendmail( + recipients = frappe.db.get_value("Community Member", email, "email"), + subject = _("Message Digest"), + header = _("Message Digest"), + template = "lms_daily_digest", + args = { + "batches": group_by_batch + }, + delayed = False + ) \ No newline at end of file diff --git a/community/patches.txt b/community/patches.txt index e69de29b..5374512d 100644 --- a/community/patches.txt +++ b/community/patches.txt @@ -0,0 +1 @@ +community.patches.set_email_preferences \ No newline at end of file diff --git a/community/patches/set_email_preferences.py b/community/patches/set_email_preferences.py new file mode 100644 index 00000000..ac7d3acd --- /dev/null +++ b/community/patches/set_email_preferences.py @@ -0,0 +1,8 @@ +from __future__ import unicode_literals +import frappe + +def execute(): + members = frappe.get_all("Community Member", ["name", "email_preference"]) + for member in members: + if not member.email_preference: + frappe.db.set_value("Community Member", member.name, "email_preference", "Email on every Message") \ No newline at end of file diff --git a/community/templates/emails/lms_daily_digest.html b/community/templates/emails/lms_daily_digest.html new file mode 100644 index 00000000..87739d8b --- /dev/null +++ b/community/templates/emails/lms_daily_digest.html @@ -0,0 +1,12 @@ +{% for batch in batches %} +

{{ batch }}

+
+ {% for message in batches[batch] %} +
+

{{ frappe.utils.md_to_html(message.message) }}

+

By {{message.author}}

+
{{ frappe.utils.format_datetime(message.creation, "medium") }}
+
+ {% endfor %} +
+{% endfor %} \ No newline at end of file diff --git a/community/templates/emails/lms_message.html b/community/templates/emails/lms_message.html new file mode 100644 index 00000000..4cfad56f --- /dev/null +++ b/community/templates/emails/lms_message.html @@ -0,0 +1,13 @@ + + +

{{ message }}

+

By {{author}}

+
{{ creation }}
+Open Course diff --git a/community/www/courses/course.html b/community/www/courses/course.html index 9dc74046..e4190705 100644 --- a/community/www/courses/course.html +++ b/community/www/courses/course.html @@ -61,9 +61,11 @@ {% endif %} -
- - +
+
+ + +
{% if discussions %}