From e04bbb633d56e1a89d657a8aed9b4755fa2b2e87 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Fri, 21 May 2021 13:12:52 +0530 Subject: [PATCH] refactor: moved courses/*/index pages to batch/* --- community/hooks.py | 12 +++--- community/lms/doctype/lms_batch/lms_batch.py | 30 +++++++++++-- community/public/css/style.less | 25 +++++++++++ community/www/batch/__init__.py | 0 .../about/index.html => batch/about.html} | 3 +- community/www/batch/about.py | 7 +++ .../discuss/index.html => batch/discuss.html} | 5 ++- .../discuss/index.js => batch/discuss.js} | 0 community/www/batch/discuss.py | 9 ++++ .../learn/index.html => batch/learn.html} | 2 +- .../learn/index.py => batch/learn.py} | 27 +++--------- .../members/index.html => batch/members.html} | 7 +-- community/www/batch/members.py | 8 ++++ .../index.html => batch/schedule.html} | 2 +- .../about/index.py => batch/schedule.py} | 1 - community/www/batch/utils.py | 43 +++++++++++++++++++ community/www/courses/discuss/index.py | 15 ------- community/www/courses/members/index.py | 12 ------ community/www/courses/schedule/index.py | 8 ---- community/www/macros/sidebar.html | 13 +++--- 20 files changed, 148 insertions(+), 81 deletions(-) create mode 100644 community/www/batch/__init__.py rename community/www/{courses/about/index.html => batch/about.html} (97%) create mode 100644 community/www/batch/about.py rename community/www/{courses/discuss/index.html => batch/discuss.html} (93%) rename community/www/{courses/discuss/index.js => batch/discuss.js} (100%) create mode 100644 community/www/batch/discuss.py rename community/www/{courses/learn/index.html => batch/learn.html} (99%) rename community/www/{courses/learn/index.py => batch/learn.py} (54%) rename community/www/{courses/members/index.html => batch/members.html} (90%) create mode 100644 community/www/batch/members.py rename community/www/{courses/schedule/index.html => batch/schedule.html} (91%) rename community/www/{courses/about/index.py => batch/schedule.py} (99%) create mode 100644 community/www/batch/utils.py delete mode 100644 community/www/courses/discuss/index.py delete mode 100644 community/www/courses/members/index.py delete mode 100644 community/www/courses/schedule/index.py diff --git a/community/hooks.py b/community/hooks.py index 1b6f2151..62abd1ed 100644 --- a/community/hooks.py +++ b/community/hooks.py @@ -141,12 +141,12 @@ primary_rules = [ {"from_route": "/hackathons//", "to_route": "hackathons/project"}, {"from_route": "/dashboard", "to_route": ""}, {"from_route": "/add-a-new-batch", "to_route": "add-a-new-batch"}, - {"from_route": "/courses///learn", "to_route": "courses/learn"}, - {"from_route": "/courses///learn/.", "to_route": "courses/learn"}, - {"from_route": "/courses///schedule", "to_route": "courses/schedule"}, - {"from_route": "/courses///members", "to_route": "courses/members"}, - {"from_route": "/courses///discuss", "to_route": "courses/discuss"}, - {"from_route": "/courses///about", "to_route": "courses/about"} + {"from_route": "/courses///learn", "to_route": "batch/learn"}, + {"from_route": "/courses///learn/.", "to_route": "batch/learn"}, + {"from_route": "/courses///schedule", "to_route": "batch/schedule"}, + {"from_route": "/courses///members", "to_route": "batch/members"}, + {"from_route": "/courses///discuss", "to_route": "batch/discuss"}, + {"from_route": "/courses///about", "to_route": "batch/about"}, ] # Any frappe default URL is blocked by profile-rules, add it here to unblock it diff --git a/community/lms/doctype/lms_batch/lms_batch.py b/community/lms/doctype/lms_batch/lms_batch.py index 16d1ec72..1e5ae856 100644 --- a/community/lms/doctype/lms_batch/lms_batch.py +++ b/community/lms/doctype/lms_batch/lms_batch.py @@ -37,13 +37,37 @@ class LMSBatch(Document): member_names = [m['member'] for m in memberships] return find_all("Community Member", name=["IN", member_names]) - def is_member(self, email): + def is_member(self, email, member_type=None): """Checks if a person is part of a batch. + + If member_type is specified, checks if the person is a Student/Mentor. """ member = find("Community Member", email=email) - return member and frappe.db.exists( + if not member: + return + + filters = { + "batch": self.name, + "member": member.name + } + if member_type: + filters['member_type'] = member_type + return frappe.db.exists("LMS Batch Membership", filters) + + def get_students(self): + """Returns (email, full_name, username) of all the students of this batch as a list of dict. + """ + memberships = frappe.get_all( "LMS Batch Membership", - {"batch": self.name, "member": member.name}) + {"batch": self.name, "member_type": "Student"}, + ["member"]) + member_names = [m['member'] for m in memberships] + members = frappe.get_all( + "Community Member", + {"name": ["IN", member_names]}, + ["email", "full_name", "username"]) + return members + @frappe.whitelist() def get_messages(batch): diff --git a/community/public/css/style.less b/community/public/css/style.less index e92d02ef..87454c16 100644 --- a/community/public/css/style.less +++ b/community/public/css/style.less @@ -312,3 +312,28 @@ section.lightgray { border: 1px solid #ddd; margin-bottom: 20px; } + +.svg-200 svg { + width: 200px; + height: 200px; +} + +.livecode-editor-small .livecode-editor { + .CodeMirror-scroll { + max-height: 160px; + min-height: 160px; + } + + canvas { + width: 150px; + height: 150px; + } +} + +.mentor-dashboard { + margin-top: 20px; + + .submission { + margin: 40px 0px 0px 20px; + } +} diff --git a/community/www/batch/__init__.py b/community/www/batch/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/community/www/courses/about/index.html b/community/www/batch/about.html similarity index 97% rename from community/www/courses/about/index.html rename to community/www/batch/about.html index 46bf0e13..4edabaef 100644 --- a/community/www/courses/about/index.html +++ b/community/www/batch/about.html @@ -11,7 +11,8 @@ {% endblock %} {% block content %} -{{ Sidebar(course.name, batch.name) }} +{{ Sidebar(course, batch) }} +
{{ CourseBasicDetail(course)}} {{ InstructorsSection(course.get_instructor()) }} diff --git a/community/www/batch/about.py b/community/www/batch/about.py new file mode 100644 index 00000000..b847d8c8 --- /dev/null +++ b/community/www/batch/about.py @@ -0,0 +1,7 @@ +import frappe +from . import utils + +def get_context(context): + utils.get_common_context(context) + + print("context", context) diff --git a/community/www/courses/discuss/index.html b/community/www/batch/discuss.html similarity index 93% rename from community/www/courses/discuss/index.html rename to community/www/batch/discuss.html index 73270d8e..31cf1813 100644 --- a/community/www/courses/discuss/index.html +++ b/community/www/batch/discuss.html @@ -11,10 +11,11 @@ {% endblock %} {% block content %} -{{ Sidebar(course_slug, batch_code) }} +{{ Sidebar(course, batch) }} +
- {{ BatchHearder(course.name, member_count) }} + {{ BatchHearder(course.title, member_count) }}
diff --git a/community/www/courses/discuss/index.js b/community/www/batch/discuss.js similarity index 100% rename from community/www/courses/discuss/index.js rename to community/www/batch/discuss.js diff --git a/community/www/batch/discuss.py b/community/www/batch/discuss.py new file mode 100644 index 00000000..fcd5ebda --- /dev/null +++ b/community/www/batch/discuss.py @@ -0,0 +1,9 @@ +import frappe +from . import utils +from community.lms.doctype.lms_batch.lms_batch import get_messages + +def get_context(context): + utils.get_common_context(context) + + context.members = utils.get_batch_members(context.batch.name) + context.member_count = len(context.members) diff --git a/community/www/courses/learn/index.html b/community/www/batch/learn.html similarity index 99% rename from community/www/courses/learn/index.html rename to community/www/batch/learn.html index db3a4549..31750946 100644 --- a/community/www/courses/learn/index.html +++ b/community/www/batch/learn.html @@ -23,7 +23,7 @@ {% block content %} -{{ Sidebar(course.name, batch.name) }} +{{ Sidebar(course, batch) }}
diff --git a/community/www/courses/learn/index.py b/community/www/batch/learn.py similarity index 54% rename from community/www/courses/learn/index.py rename to community/www/batch/learn.py index 4aea3388..983ea8be 100644 --- a/community/www/courses/learn/index.py +++ b/community/www/batch/learn.py @@ -1,37 +1,25 @@ import frappe -from community.lms.models import Course +from . import utils def get_context(context): - context.no_cache = 1 + utils.get_common_context(context) - course_name = frappe.form_dict["course"] - batch_name = frappe.form_dict["batch"] chapter_index = frappe.form_dict.get("chapter") lesson_index = frappe.form_dict.get("lesson") lesson_number = f"{chapter_index}.{lesson_index}" - course = Course.find(course_name) - if not course: - context.template = "www/404.html" - return - - batch = course.get_batch(batch_name) - if not batch: - frappe.local.flags.redirect_location = "/courses/" + course_name - raise frappe.Redirect + course_name = context.course.name + batch_name = context.batch.name if not chapter_index or not lesson_index: frappe.local.flags.redirect_location = f"/courses/{course_name}/{batch_name}/learn/1.1" raise frappe.Redirect - context.course = course - context.batch = batch - context.lesson = course.get_lesson(chapter_index, lesson_index) + context.lesson = context.course.get_lesson(chapter_index, lesson_index) context.lesson_index = lesson_index context.chapter_index = chapter_index - context.livecode_url = get_livecode_url() - outline = course.get_outline() + outline = context.course.get_outline() next_ = outline.get_next(lesson_number) prev_ = outline.get_prev(lesson_number) context.next_url = get_learn_url(course_name, batch_name, next_) @@ -41,6 +29,3 @@ def get_learn_url(course_name, batch_name, lesson_number): if not lesson_number: return return f"/courses/{course_name}/{batch_name}/learn/{lesson_number}" - -def get_livecode_url(): - return frappe.db.get_single_value("LMS Settings", "livecode_url") diff --git a/community/www/courses/members/index.html b/community/www/batch/members.html similarity index 90% rename from community/www/courses/members/index.html rename to community/www/batch/members.html index bc7ce6f1..ac092403 100644 --- a/community/www/courses/members/index.html +++ b/community/www/batch/members.html @@ -11,9 +11,10 @@ {% endblock %} {% block content %} -{{ Sidebar(course_slug, batch_code) }} +{{ Sidebar(course, batch) }} +
- {{ BatchHearder(course.name, member_count)}} + {{ BatchHearder(course.title, member_count)}} {{ MembersList(members)}}
{% endblock %} @@ -36,4 +37,4 @@
{% endfor %}
-{% endmacro %} \ No newline at end of file +{% endmacro %} diff --git a/community/www/batch/members.py b/community/www/batch/members.py new file mode 100644 index 00000000..1d04ac15 --- /dev/null +++ b/community/www/batch/members.py @@ -0,0 +1,8 @@ +import frappe +from . import utils + +def get_context(context): + utils.get_common_context(context) + + context.members = utils.get_batch_members(context.batch.name) + context.member_count = len(context.members) diff --git a/community/www/courses/schedule/index.html b/community/www/batch/schedule.html similarity index 91% rename from community/www/courses/schedule/index.html rename to community/www/batch/schedule.html index f78a9397..07aa84d4 100644 --- a/community/www/courses/schedule/index.html +++ b/community/www/batch/schedule.html @@ -8,7 +8,7 @@ {% endblock %} {% block content %} -{{ Sidebar(course, batch_code) }} +{{ Sidebar(course, batch) }}
{% endblock %} diff --git a/community/www/courses/about/index.py b/community/www/batch/schedule.py similarity index 99% rename from community/www/courses/about/index.py rename to community/www/batch/schedule.py index 83b3df0e..65d68fc3 100644 --- a/community/www/courses/about/index.py +++ b/community/www/batch/schedule.py @@ -3,7 +3,6 @@ from community.lms.models import Course def get_context(context): context.no_cache = 1 - course_name = frappe.form_dict["course"] batch_name = frappe.form_dict["batch"] diff --git a/community/www/batch/utils.py b/community/www/batch/utils.py new file mode 100644 index 00000000..51e77eba --- /dev/null +++ b/community/www/batch/utils.py @@ -0,0 +1,43 @@ +import frappe +from community.lms.models import Course + +def get_common_context(context): + context.no_cache = 1 + + course_name = frappe.form_dict["course"] + batch_name = frappe.form_dict["batch"] + + course = Course.find(course_name) + if not course: + context.template = "www/404.html" + return + + batch = course.get_batch(batch_name) + if not batch: + frappe.local.flags.redirect_location = "/courses/" + course_name + raise frappe.Redirect + + context.course = course + context.batch = batch + context.livecode_url = get_livecode_url() + +def get_livecode_url(): + return frappe.db.get_single_value("LMS Settings", "livecode_url") + +def get_batch_members(batch_name): + members = [] + memberships = frappe.get_all("LMS Batch Membership", {"batch": batch_name}, ["member", "member_type"]) + + for membership in memberships: + member = get_member_with_name(membership.member) + if membership.member_type == "Mentor": + member.is_mentor = True + members.append(member) + return members + +def get_member_with_name(name): + try: + return frappe.get_doc("Community Member", name) + except frappe.DoesNotExistError: + return + diff --git a/community/www/courses/discuss/index.py b/community/www/courses/discuss/index.py deleted file mode 100644 index 7c34761a..00000000 --- a/community/www/courses/discuss/index.py +++ /dev/null @@ -1,15 +0,0 @@ -import frappe -from community.www.courses.utils import redirect_if_not_a_member, get_course, get_batch_members, get_batch -from community.lms.doctype.lms_batch.lms_batch import get_messages - -def get_context(context): - context.no_cache = 1 - context.course_slug = frappe.form_dict["course"] - context.course = get_course(context.course_slug) - context.batch_code = frappe.form_dict["batch"] - redirect_if_not_a_member(context.course_slug, context.batch_code) - - context.batch = get_batch(context.batch_code) - context.members = get_batch_members(context.batch.name) - context.member_count = len(context.members) - context.messages = get_messages(context.batch.name) \ No newline at end of file diff --git a/community/www/courses/members/index.py b/community/www/courses/members/index.py deleted file mode 100644 index 7e9bf36a..00000000 --- a/community/www/courses/members/index.py +++ /dev/null @@ -1,12 +0,0 @@ -import frappe -from community.www.courses.utils import redirect_if_not_a_member, get_batch, get_member_with_name, get_course, get_batch_members - -def get_context(context): - context.no_cache = 1 - context.course_slug = frappe.form_dict["course"] - context.course = get_course(context.course_slug) - context.batch_code = frappe.form_dict["batch"] - redirect_if_not_a_member(context.course_slug, context.batch_code) - context.batch = get_batch(context.batch_code) - context.members = get_batch_members(context.batch.name) - context.member_count = len(context.members) \ No newline at end of file diff --git a/community/www/courses/schedule/index.py b/community/www/courses/schedule/index.py deleted file mode 100644 index 03180785..00000000 --- a/community/www/courses/schedule/index.py +++ /dev/null @@ -1,8 +0,0 @@ -import frappe -from community.www.courses.utils import redirect_if_not_a_member - -def get_context(context): - context.no_cache = 1 - context.course = frappe.form_dict["course"] - context.batch_code = frappe.form_dict["batch"] - redirect_if_not_a_member(context.course, context.batch_code) \ No newline at end of file diff --git a/community/www/macros/sidebar.html b/community/www/macros/sidebar.html index c36ab8ee..125962ec 100644 --- a/community/www/macros/sidebar.html +++ b/community/www/macros/sidebar.html @@ -1,12 +1,11 @@ -{% macro Sidebar(course, batch_code) %} +{% macro Sidebar(course, batch, is_mentor=False) %} {% endmacro %}