From 637c7953213a6fa5c83c0718d062a200a22d28ea Mon Sep 17 00:00:00 2001 From: pateljannat Date: Fri, 21 May 2021 16:22:59 +0530 Subject: [PATCH] refactor: moved community member class functions to user override --- community/community/widgets/Avatar.html | 2 +- community/lms/doctype/lms_batch/lms_batch.py | 9 +++------ .../lms_batch_membership/lms_batch_membership.py | 4 +--- community/overrides/user.py | 9 +++++++++ community/patches.txt | 1 + .../replace_member_with_user_in_batch_membership.py | 1 - ...eplace_member_with_user_in_course_mentor_mapping.py | 9 +++++++++ community/www/batch/learn.py | 2 +- community/www/batch/members.html | 2 +- community/www/dashboard/index.html | 6 +++--- community/www/dashboard/index.py | 3 +-- community/www/macros/profile.html | 10 ---------- community/www/profiles/profile.html | 2 +- 13 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 community/patches/replace_member_with_user_in_course_mentor_mapping.py delete mode 100644 community/www/macros/profile.html diff --git a/community/community/widgets/Avatar.html b/community/community/widgets/Avatar.html index 4cb65da6..6ef6ed6d 100644 --- a/community/community/widgets/Avatar.html +++ b/community/community/widgets/Avatar.html @@ -7,7 +7,7 @@ {% else %} - {{ member.abbr }} + {{ frappe.utils.get_abbr(member.full_name) }} {% endif %} diff --git a/community/lms/doctype/lms_batch/lms_batch.py b/community/lms/doctype/lms_batch/lms_batch.py index ce23f782..192df15f 100644 --- a/community/lms/doctype/lms_batch/lms_batch.py +++ b/community/lms/doctype/lms_batch/lms_batch.py @@ -42,13 +42,10 @@ class LMSBatch(Document): If member_type is specified, checks if the person is a Student/Mentor. """ - member = find("Community Member", email=email) - if not member: - return filters = { "batch": self.name, - "member": member.name + "member": email } if member_type: filters['member_type'] = member_type @@ -63,8 +60,8 @@ class LMSBatch(Document): ["member"]) member_names = [m['member'] for m in memberships] members = frappe.get_all( - "Community Member", - {"name": ["IN", member_names]}, + "Users", + {"email": ["IN", member_names]}, ["email", "full_name", "username"]) return members diff --git a/community/lms/doctype/lms_batch_membership/lms_batch_membership.py b/community/lms/doctype/lms_batch_membership/lms_batch_membership.py index 0d6db70d..1e0976c6 100644 --- a/community/lms/doctype/lms_batch_membership/lms_batch_membership.py +++ b/community/lms/doctype/lms_batch_membership/lms_batch_membership.py @@ -29,13 +29,11 @@ class LMSBatchMembership(Document): frappe.throw(_("{0} is already a {1} of {2} course through {3} batch").format(member_name, membership.member_type, course, membership.batch)) def create_membership(batch, member=None, member_type="Student", role="Member"): - if not member: - member = frappe.db.get_value("Community Member", {"email": frappe.session.user}, "name") frappe.get_doc({ "doctype": "LMS Batch Membership", "batch": batch, "role": role, "member_type": member_type, - "member": member + "member": member or frappe.session.user }).save(ignore_permissions=True) return "OK" diff --git a/community/overrides/user.py b/community/overrides/user.py index 5eb1b0d6..500a9285 100644 --- a/community/overrides/user.py +++ b/community/overrides/user.py @@ -31,3 +31,12 @@ class CustomUser(User): idx = cint((int(hash_name[4:6], 16) + 1) / 5.33) return palette[idx % 8] + def get_batch_count(self) -> int: + """Returns the number of batches authored by this user. + """ + return frappe.db.count( + 'LMS Batch Membership', { + 'member': self.name, + 'member_type': 'Mentor' + }) + diff --git a/community/patches.txt b/community/patches.txt index e18c7fd7..5c9f065e 100644 --- a/community/patches.txt +++ b/community/patches.txt @@ -3,3 +3,4 @@ community.patches.change_name_for_community_members community.patches.save_abbr_for_community_members community.patches.create_mentor_request_email_templates community.patches.replace_member_with_user_in_batch_membership +community.patches.replace_member_with_user_in_course_mentor_mapping diff --git a/community/patches/replace_member_with_user_in_batch_membership.py b/community/patches/replace_member_with_user_in_batch_membership.py index e8e04748..4c2ae2cc 100644 --- a/community/patches/replace_member_with_user_in_batch_membership.py +++ b/community/patches/replace_member_with_user_in_batch_membership.py @@ -1,6 +1,5 @@ from __future__ import unicode_literals import frappe -from frappe import _ def execute(): frappe.reload_doc("lms", "doctype", "lms_batch_membership") diff --git a/community/patches/replace_member_with_user_in_course_mentor_mapping.py b/community/patches/replace_member_with_user_in_course_mentor_mapping.py new file mode 100644 index 00000000..1e868666 --- /dev/null +++ b/community/patches/replace_member_with_user_in_course_mentor_mapping.py @@ -0,0 +1,9 @@ +from __future__ import unicode_literals +import frappe + +def execute(): + frappe.reload_doc("lms", "doctype", "lms_course_mentor_mapping") + mappings = frappe.get_all("LMS Course Mentor Mapping", ["mentor", "name"]) + for mapping in mappings: + email = frappe.db.get_value("Community Member", mapping.mentor, "email") + frappe.db.set_value("LMS Course Mentor Mapping", mapping.name, "mentor", email) diff --git a/community/www/batch/learn.py b/community/www/batch/learn.py index 983ea8be..538fa26c 100644 --- a/community/www/batch/learn.py +++ b/community/www/batch/learn.py @@ -18,7 +18,7 @@ def get_context(context): context.lesson = context.course.get_lesson(chapter_index, lesson_index) context.lesson_index = lesson_index context.chapter_index = chapter_index - + print(context.lesson) outline = context.course.get_outline() next_ = outline.get_next(lesson_number) prev_ = outline.get_prev(lesson_number) diff --git a/community/www/batch/members.html b/community/www/batch/members.html index ac092403..1fdff505 100644 --- a/community/www/batch/members.html +++ b/community/www/batch/members.html @@ -25,7 +25,7 @@ {% for member in members %}
- {{ Profile(member.photo, member.full_name, member.abbr, "small") }} + {{ widgets.Avatar(member=member, avatar_class="avatar-medium") }} {{ member.full_name }}
{{member.full_name}} diff --git a/community/www/dashboard/index.html b/community/www/dashboard/index.html index f87a06df..bfbcf195 100644 --- a/community/www/dashboard/index.html +++ b/community/www/dashboard/index.html @@ -91,7 +91,7 @@
- {{ Profile(member.photo, member.full_name, member.abbr, "large")}} + {{ widgets.Avatar(member=member, avatar_class="avatar-medium") }} {{ member.full_name }}
@@ -125,7 +125,7 @@
- {{ Profile(message.profile, message.full_name, message.abbr, "small")}} + {{ widgets.Avatar(member=member, avatar_class="avatar-medium") }} {{ member.full_name }}
{{ frappe.utils.md_to_html(message.message) }}
@@ -209,4 +209,4 @@
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/community/www/dashboard/index.py b/community/www/dashboard/index.py index 1172b77a..d0cf43bc 100644 --- a/community/www/dashboard/index.py +++ b/community/www/dashboard/index.py @@ -7,7 +7,7 @@ def get_context(context): if frappe.session.user == "Guest": frappe.local.flags.redirect_location = "/login" raise frappe.Redirect - context.member = frappe.get_all("Community Member", {"email": frappe.session.user}, ["name", "email", "photo", "full_name", "abbr"])[0] + context.member = frappe.get_doc("User", frappe.session.user) context.memberships = get_memberships(context.member.name) context.courses = get_courses(context.memberships) context.activity = get_activity(context.memberships) @@ -38,5 +38,4 @@ def get_activity(memberships): messages = frappe.get_all("LMS Message", {"batch": ["in", ",".join(batches)]}, ["message", "author", "creation", "batch"], order_by='creation desc') for message in messages: message.course = courses[message.batch] - message.profile, message.full_name, message.abbr = frappe.db.get_value("Community Member", message.author, ["photo", "full_name", "abbr"]) return messages diff --git a/community/www/macros/profile.html b/community/www/macros/profile.html deleted file mode 100644 index c608b59f..00000000 --- a/community/www/macros/profile.html +++ /dev/null @@ -1,10 +0,0 @@ -{% macro Profile(photo, full_name, abbr, icon) %} -{% if photo %} -{{ full_name }} -{% else %} -
- {{ abbr }} -
-{% endif %} -{% endmacro %} \ No newline at end of file diff --git a/community/www/profiles/profile.html b/community/www/profiles/profile.html index 30df9c43..6e64fda3 100644 --- a/community/www/profiles/profile.html +++ b/community/www/profiles/profile.html @@ -94,7 +94,7 @@ {% block page_content %}
- {{ Profile(member.photo, member.full_name, member.abbr, "large")}} + {{ widgets.Avatar(member=member, avatar_class="avatar-medium") }} {{ member.full_name }}