From 25a0dd02ac8a19bc49e85ea2ddee739029ee0948 Mon Sep 17 00:00:00 2001 From: pateljannat Date: Fri, 12 Mar 2021 12:22:12 +0530 Subject: [PATCH] feat: community member for profile and edit profile form --- .../community_course_member.json | 3 +- .../community_course_member.py | 4 +- .../community_member/community_member.json | 14 ++++- .../community_member/community_member.py | 35 ++++++++++- .../templates/community_member.html | 18 ++++-- .../__init__.py | 0 .../update_profile.js} | 0 .../update_profile.json} | 60 +++++++++++++++---- .../update_profile.py} | 0 community/hooks.py | 12 ++-- community/www/courses/course.html | 2 +- community/www/courses/course.js | 9 ++- community/www/courses/course.py | 1 + community/www/my-courses/index.py | 4 +- 14 files changed, 124 insertions(+), 38 deletions(-) rename community/community/web_form/{community_course_membership => update_profile}/__init__.py (100%) rename community/community/web_form/{community_course_membership/community_course_membership.js => update_profile/update_profile.js} (100%) rename community/community/web_form/{community_course_membership/community_course_membership.json => update_profile/update_profile.json} (54%) rename community/community/web_form/{community_course_membership/community_course_membership.py => update_profile/update_profile.py} (100%) diff --git a/community/community/doctype/community_course_member/community_course_member.json b/community/community/doctype/community_course_member/community_course_member.json index f945a918..9a48dab9 100644 --- a/community/community/doctype/community_course_member/community_course_member.json +++ b/community/community/doctype/community_course_member/community_course_member.json @@ -69,7 +69,7 @@ "index_web_pages_for_search": 1, "is_published_field": "enabled", "links": [], - "modified": "2021-03-03 15:00:46.298535", + "modified": "2021-03-09 11:17:40.842666", "modified_by": "Administrator", "module": "Community", "name": "Community Course Member", @@ -89,7 +89,6 @@ } ], "quick_entry": 1, - "route": "me", "sort_field": "modified", "sort_order": "DESC", "track_changes": 1 diff --git a/community/community/doctype/community_course_member/community_course_member.py b/community/community/doctype/community_course_member/community_course_member.py index 2ee58255..06861201 100644 --- a/community/community/doctype/community_course_member/community_course_member.py +++ b/community/community/doctype/community_course_member/community_course_member.py @@ -17,7 +17,7 @@ class CommunityCourseMember(WebsiteGenerator): def validate(self): self.validate_user_name() if not self.route: - self.route = "me/" + self.user_name + self.route = self.user_name def validate_user_name(self): if len(self.user_name) < 4: @@ -60,7 +60,7 @@ class CommunityCourseMember(WebsiteGenerator): "username": self.user_name, "send_welcome_email": 0, "user_type": 'Website User', - "redirect_url": "me/" + self.name + "redirect_url": self.name }) user.save(ignore_permissions=True) update_password_link = user.reset_password() diff --git a/community/community/doctype/community_member/community_member.json b/community/community/doctype/community_member/community_member.json index 0880d1a7..9ac49269 100644 --- a/community/community/doctype/community_member/community_member.json +++ b/community/community/doctype/community_member/community_member.json @@ -1,6 +1,6 @@ { "actions": [], - "autoname": "field:user_name", + "allow_guest_to_view": 1, "creation": "2021-02-12 15:47:23.591567", "doctype": "DocType", "editable_grid": 1, @@ -8,6 +8,7 @@ "field_order": [ "enabled", "full_name", + "email", "role", "photo", "short_intro", @@ -64,11 +65,20 @@ "fieldname": "route", "fieldtype": "Data", "label": "Route" + }, + { + "fieldname": "email", + "fieldtype": "Data", + "label": "Email", + "options": "Email", + "reqd": 1, + "unique": 1 } ], + "has_web_view": 1, "index_web_pages_for_search": 1, "links": [], - "modified": "2021-03-02 11:24:28.763348", + "modified": "2021-03-12 10:23:09.709669", "modified_by": "Administrator", "module": "Community", "name": "Community Member", diff --git a/community/community/doctype/community_member/community_member.py b/community/community/doctype/community_member/community_member.py index a5d88aba..57762bcb 100644 --- a/community/community/doctype/community_member/community_member.py +++ b/community/community/doctype/community_member/community_member.py @@ -3,8 +3,39 @@ # For license information, please see license.txt from __future__ import unicode_literals -# import frappe +import frappe from frappe.website.website_generator import WebsiteGenerator +import re +from frappe import _ class CommunityMember(WebsiteGenerator): - pass + def get_context(self, context): + context.abbr = ("").join([ s[0] for s in self.full_name.split() ]) + return context + + def validate(self): + self.validate_user_name() + if self.route != self.user_name: + self.route = self.user_name + + def validate_user_name(self): + if self.user_name: + if len(self.user_name) < 4: + frappe.throw(_("Username must be atleast 4 characters long.")) + if not re.match("^[A-Za-z0-9]*$", self.user_name): + frappe.throw(_("Username can only contain alphabets, and numbers.")) + self.user_name = self.user_name.lower() + + def autoname(self): + self.name = self.user_name + +def create_member_from_user(doc, method): + member = frappe.get_doc({ + "doctype": "Community Member", + "full_name": doc.full_name, + "user_name": doc.username, + "email": doc.email, + "route": doc.username, + "owner": doc.email + }) + member.save(ignore_permissions=True) diff --git a/community/community/doctype/community_member/templates/community_member.html b/community/community/doctype/community_member/templates/community_member.html index 566ae37d..fabec4d2 100644 --- a/community/community/doctype/community_member/templates/community_member.html +++ b/community/community/doctype/community_member/templates/community_member.html @@ -1,17 +1,23 @@ {% extends "templates/web.html" %} - {% block page_content %}
+ {% if photo %}
{{ full_name }}
+ {% else %} +
+
{{ abbr }}
+
+ {% endif %}

{{ full_name }}

-

{{ short_intro }}

-
-
Role: {{ role }}
-
-

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

+ {% if short_intro %} +

{{ short_intro }}

+ {% endif %} + {% if bio %} +

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

+ {% endif %}
{% endblock %} diff --git a/community/community/web_form/community_course_membership/__init__.py b/community/community/web_form/update_profile/__init__.py similarity index 100% rename from community/community/web_form/community_course_membership/__init__.py rename to community/community/web_form/update_profile/__init__.py diff --git a/community/community/web_form/community_course_membership/community_course_membership.js b/community/community/web_form/update_profile/update_profile.js similarity index 100% rename from community/community/web_form/community_course_membership/community_course_membership.js rename to community/community/web_form/update_profile/update_profile.js diff --git a/community/community/web_form/community_course_membership/community_course_membership.json b/community/community/web_form/update_profile/update_profile.json similarity index 54% rename from community/community/web_form/community_course_membership/community_course_membership.json rename to community/community/web_form/update_profile/update_profile.json index dadd267f..24d1f2db 100644 --- a/community/community/web_form/community_course_membership/community_course_membership.json +++ b/community/community/web_form/update_profile/update_profile.json @@ -2,37 +2,39 @@ "accept_payment": 0, "allow_comments": 0, "allow_delete": 0, - "allow_edit": 0, + "allow_edit": 1, "allow_incomplete": 0, "allow_multiple": 0, "allow_print": 0, "amount": 0.0, "amount_based_on_field": 0, "apply_document_permissions": 0, - "button_label": "Submit", - "creation": "2021-03-02 11:31:39.072501", - "doc_type": "Community Course Member", + "breadcrumbs": "", + "button_label": "Save", + "creation": "2021-03-09 17:34:03.394301", + "doc_type": "Community Member", "docstatus": 0, "doctype": "Web Form", "idx": 0, "is_standard": 1, - "login_required": 0, + "login_required": 1, "max_attachment_size": 0, - "modified": "2021-03-03 11:02:33.907687", + "modified": "2021-03-12 10:23:37.625177", "modified_by": "Administrator", "module": "Community", - "name": "community-course-membership", + "name": "update-profile", "owner": "Administrator", "payment_button_label": "Buy Now", "published": 1, - "route": "community-course-membership", + "route": "edit-profile", "route_to_success_link": 0, "show_attachments": 0, "show_in_grid": 0, "show_sidebar": 0, "sidebar_items": [], - "success_url": "/community-course-membership", - "title": "Community Course Membership", + "success_message": "Profile updated successfully.", + "success_url": "/", + "title": "Update Profile", "web_form_fields": [ { "allow_read_on_all_link_options": 0, @@ -55,7 +57,7 @@ "max_length": 0, "max_value": 0, "read_only": 0, - "reqd": 1, + "reqd": 0, "show_in_filter": 0 }, { @@ -70,6 +72,42 @@ "read_only": 0, "reqd": 0, "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "short_intro", + "fieldtype": "Data", + "hidden": 0, + "label": "Short Intro", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "bio", + "fieldtype": "Data", + "hidden": 0, + "label": "Bio", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 + }, + { + "allow_read_on_all_link_options": 0, + "fieldname": "photo", + "fieldtype": "Attach Image", + "hidden": 0, + "label": "Photo", + "max_length": 0, + "max_value": 0, + "read_only": 0, + "reqd": 0, + "show_in_filter": 0 } ] } \ No newline at end of file diff --git a/community/community/web_form/community_course_membership/community_course_membership.py b/community/community/web_form/update_profile/update_profile.py similarity index 100% rename from community/community/web_form/community_course_membership/community_course_membership.py rename to community/community/web_form/update_profile/update_profile.py diff --git a/community/hooks.py b/community/hooks.py index c82a8a66..edfdd032 100644 --- a/community/hooks.py +++ b/community/hooks.py @@ -90,13 +90,11 @@ app_include_js = "/assets/community/js/community.js" # --------------- # Hook on document methods and events -# doc_events = { -# "*": { -# "on_update": "method", -# "on_cancel": "method", -# "on_trash": "method" -# } -# } +doc_events = { + "User": { + "after_insert": "community.community.doctype.community_member.community_member.create_member_from_user" + } + } # Scheduled Tasks # --------------- diff --git a/community/www/courses/course.html b/community/www/courses/course.html index 682b9255..781b2ad3 100644 --- a/community/www/courses/course.html +++ b/community/www/courses/course.html @@ -19,7 +19,7 @@
Enrolled
{% if not course_enrolled and frappe.session.user != "Guest" %} - + {% endif %}

{{ course.title }}

diff --git a/community/www/courses/course.js b/community/www/courses/course.js index 9a3923b7..d8c50f98 100644 --- a/community/www/courses/course.js +++ b/community/www/courses/course.js @@ -1,7 +1,6 @@ frappe.ready(() => { if(frappe.session.user != "Guest"){ - var url_params = new URLSearchParams(window.location.search); - frappe.call('community.www.courses.course.has_enrolled', { course: url_params.get("course") }, (data) => { + frappe.call('community.www.courses.course.has_enrolled', { course: get_search_params().get("course") }, (data) => { if (data.message) { show_enrollment_badge() } @@ -14,8 +13,12 @@ var show_enrollment_badge = () => { $(".enrollment-badge").removeClass("hide"); } +var get_search_params = () => { + return new URLSearchParams(window.location.search) +} + $('.btn-enroll').on('click', (e) => { - frappe.call('community.www.courses.course.enroll', { course: $(e.target).attr("data-course") }, (data) => { + frappe.call('community.www.courses.course.enroll', { course: get_search_params().get("course") }, (data) => { show_enrollment_badge() }); }); diff --git a/community/www/courses/course.py b/community/www/courses/course.py index 5a41858b..60ea4c99 100644 --- a/community/www/courses/course.py +++ b/community/www/courses/course.py @@ -24,6 +24,7 @@ def get_course(name): @frappe.whitelist() def has_enrolled(course): + print(frappe.db) return frappe.db.get_value("LMS Course Enrollment", {"course": course, "owner": frappe.session.user}) @frappe.whitelist() diff --git a/community/www/my-courses/index.py b/community/www/my-courses/index.py index c40c8ecd..b3902f51 100644 --- a/community/www/my-courses/index.py +++ b/community/www/my-courses/index.py @@ -6,10 +6,10 @@ def get_context(context): def get_my_courses(): my_courses = [] - courses = frappe.get_all("Community Course Enrollment", {"owner": frappe.session.user}, ["course"]) + courses = frappe.get_all("LMS Course Enrollment", {"owner": frappe.session.user}, ["course"]) for course in courses: my_courses.append({ "name": course.course, - "title": frappe.db.get_value("Community Course", course.course, ["title"]) + "title": frappe.db.get_value("LMS Course", course.course, ["title"]) }) return my_courses \ No newline at end of file