From 413aeaccb19570c0ad8176c76ff08e146554577e Mon Sep 17 00:00:00 2001 From: pateljannat Date: Thu, 6 May 2021 12:17:28 +0530 Subject: [PATCH] feat: avatar widget --- .../community_member/community_member.py | 20 +++++++++++++++++++ community/community/widgets/Avatar.html | 12 +++++++++++ community/lms/doctype/lms_batch/lms_batch.py | 2 +- community/widgets.py | 3 ++- community/www/courses/course.html | 4 +--- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/community/community/doctype/community_member/community_member.py b/community/community/doctype/community_member/community_member.py index 333771f0..dd7bed8b 100644 --- a/community/community/doctype/community_member/community_member.py +++ b/community/community/doctype/community_member/community_member.py @@ -8,6 +8,8 @@ import re from frappe import _ from frappe.model.document import Document import random +from frappe.utils import cint +import hashlib class CommunityMember(Document): @@ -45,6 +47,24 @@ class CommunityMember(Document): 'member_type': 'Mentor' }) + def get_palette(self): + palette = [ + ['--orange-avatar-bg', '--orange-avatar-color'], + ['--pink-avatar-bg', '--pink-avatar-color'], + ['--blue-avatar-bg', '--blue-avatar-color'], + ['--green-avatar-bg', '--green-avatar-color'], + ['--dark-green-avatar-bg', '--dark-green-avatar-color'], + ['--red-avatar-bg', '--red-avatar-color'], + ['--yellow-avatar-bg', '--yellow-avatar-color'], + ['--purple-avatar-bg', '--purple-avatar-color'], + ['--gray-avatar-bg', '--gray-avatar-color0'] + ] + + encoded_name = str(self.full_name).encode("utf-8") + hash_name = hashlib.md5(encoded_name).hexdigest() + idx = cint((int(hash_name[4:6], 16) + 1) / 5.33) + return palette[idx % 8] + def __repr__(self): return f"" diff --git a/community/community/widgets/Avatar.html b/community/community/widgets/Avatar.html index e69de29b..8158f3df 100644 --- a/community/community/widgets/Avatar.html +++ b/community/community/widgets/Avatar.html @@ -0,0 +1,12 @@ +{% set color = member.get_palette() %} + + {% if member.photo %} + + + {% else %} + + {{ member.abbr }} + + {% endif %} + diff --git a/community/lms/doctype/lms_batch/lms_batch.py b/community/lms/doctype/lms_batch/lms_batch.py index 7fc547f3..94aab789 100644 --- a/community/lms/doctype/lms_batch/lms_batch.py +++ b/community/lms/doctype/lms_batch/lms_batch.py @@ -24,7 +24,7 @@ class LMSBatch(Document): {"batch": self.name, "member_type": "Mentor"}, ["member"]) for membership in memberships: - member = frappe.db.get_value("Community Member", membership.member, ["full_name", "photo", "abbr"], as_dict=1) + member = frappe.get_doc("Community Member", membership.member) mentors.append(member) return mentors diff --git a/community/widgets.py b/community/widgets.py index b5475ecc..1e135187 100644 --- a/community/widgets.py +++ b/community/widgets.py @@ -14,7 +14,8 @@ from frappe.utils.jinja import get_jenv # When {{widgets.SomeWidget()}} is called, it looks for # widgets/SomeWidgets.html in each of these modules. MODULES = [ - "lms" + "lms", + "community" ] def update_website_context(context): diff --git a/community/www/courses/course.html b/community/www/courses/course.html index 3c08fc97..4ff1606f 100644 --- a/community/www/courses/course.html +++ b/community/www/courses/course.html @@ -76,9 +76,7 @@ {% for m in batch.get_mentors() %}
- {% if m.photo_url %} - - {% endif %} + {{ widgets.Avatar(member=m, avatar_class="avatar-medium" ) }} {{m.full_name}}
{% endfor %}