From 246b26079b8f16fb3b66767d719ce2f89a884910 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Thu, 29 Sep 2022 16:59:08 +0530 Subject: [PATCH] perf: course cards get all memberships at once --- lms/hooks.py | 4 +- lms/lms/doctype/lms_settings/lms_settings.py | 9 ----- lms/lms/utils.py | 25 ++++++++++++ lms/lms/widgets/CourseCard.html | 40 ++++++++++---------- lms/public/css/style.css | 10 ++++- lms/templates/course_list.html | 20 +++++----- lms/www/courses/course.py | 2 - lms/www/courses/index.html | 11 +++++- lms/www/courses/index.py | 8 +++- 9 files changed, 82 insertions(+), 47 deletions(-) diff --git a/lms/hooks.py b/lms/hooks.py index 1e7176e8..8f05753d 100644 --- a/lms/hooks.py +++ b/lms/hooks.py @@ -199,7 +199,9 @@ jinja = { "lms.lms.utils.has_course_moderator_role", "lms.lms.utils.get_certificates", "lms.lms.utils.format_number", - "lms.lms.utils.get_lesson_count" + "lms.lms.utils.get_lesson_count", + "lms.lms.utils.get_all_memberships", + "lms.lms.utils.get_course_membership" ], "filters": [] } diff --git a/lms/lms/doctype/lms_settings/lms_settings.py b/lms/lms/doctype/lms_settings/lms_settings.py index 8d9ef1ed..ab23693d 100644 --- a/lms/lms/doctype/lms_settings/lms_settings.py +++ b/lms/lms/doctype/lms_settings/lms_settings.py @@ -10,12 +10,3 @@ from frappe import _ class LMSSettings(Document): pass -@frappe.whitelist() -def check_profile_restriction(): - force_profile_completion = frappe.db.get_single_value("LMS Settings", "force_profile_completion") - user = frappe.db.get_value("User", frappe.session.user, ["profile_complete", "username"], as_dict=True) - return { - "restrict": force_profile_completion and not user.profile_complete, - "username": user.username, - "prefix": frappe.get_hooks("profile_url_prefix")[0] or "/users/" - } diff --git a/lms/lms/utils.py b/lms/lms/utils.py index b644fcae..965021ce 100644 --- a/lms/lms/utils.py +++ b/lms/lms/utils.py @@ -138,8 +138,10 @@ def get_students(course, batch=None): "course": course, "member_type": "Student" } + if batch: filters["batch"] = batch + return frappe.get_all( "LMS Batch Membership", filters, @@ -515,3 +517,26 @@ def get_lesson_count(course): lesson_count += frappe.db.count("Lesson Reference", {"parent": chapter.chapter}) return lesson_count + + +def check_profile_restriction(): + return frappe.db.get_single_value("LMS Settings", "force_profile_completion") + +def get_restriction_details(): + user = frappe.db.get_value("User", frappe.session.user, ["profile_complete", "username"], as_dict=True) + return { + "restrict": not user.profile_complete, + "username": user.username, + "prefix": frappe.get_hooks("profile_url_prefix")[0] or "/users/" + } + + +def get_all_memberships(member): + return frappe.get_all("LMS Batch Membership", { + "member": member + }, ["name", "course", "batch", "current_lesson", "member_type", "progress"]) + + +def get_course_membership(course, memberships): + current_membership = list(filter(lambda x: x.course == course, memberships)) + return current_membership[0] if len(current_membership) else None diff --git a/lms/lms/widgets/CourseCard.html b/lms/lms/widgets/CourseCard.html index 770247d5..f6cb25f4 100644 --- a/lms/lms/widgets/CourseCard.html +++ b/lms/lms/widgets/CourseCard.html @@ -1,19 +1,23 @@ -{% set membership = get_membership(course.name, frappe.session.user) %} +{% set memberships = get_all_memberships(frappe.session.user) %} +{% set membership = get_course_membership(course.name, memberships) %} {% set progress = frappe.utils.cint(membership.progress) %} + +
-
{% for tag in get_tags(course.name) %} -
{{ tag }}
+
{{ tag }}
{% endfor %}
{% if not course.image %} -
{{ course.title[0] }}
+
{{ course.title[0] }}
{% endif %}
+
{% set lesson_count = get_lesson_count(course.name) %} @@ -104,27 +108,21 @@
{% if read_only %} - - {% else %} + + {% else %} - {% set lesson_index = get_lesson_index(membership.current_lesson) if membership and - membership.current_lesson else '1.1' %} - {% set query_parameter = "?batch=" + membership.batch if membership and - membership.batch else "" %} + {% set lesson_index = get_lesson_index(membership.current_lesson) if membership and + membership.current_lesson else '1.1' %} + {% set query_parameter = "?batch=" + membership.batch if membership and + membership.batch else "" %} - {% if progress == 100 %} - + {% if progress != 100 and membership and not course.upcoming %} + - {% elif course.upcoming %} - + {% else %} + - {% elif membership %} - - - {% else %} - - - {% endif %} + {% endif %} {% endif %}
diff --git a/lms/public/css/style.css b/lms/public/css/style.css index 99e90c33..be85af70 100644 --- a/lms/public/css/style.css +++ b/lms/public/css/style.css @@ -368,7 +368,8 @@ input[type=checkbox] { } .course-home-page .course-home-outline { - margin: 5rem 0 4rem; + margin-top: 5rem; + padding-bottom: 4rem; } .course-home-page { @@ -1739,3 +1740,10 @@ li { flex-direction: column; align-items: center; } + +.indicator-pill.green::before { + height: 0; + width: 0; + border-radius: 0; + margin-right: 0; +} diff --git a/lms/templates/course_list.html b/lms/templates/course_list.html index 0b21160b..347c4283 100644 --- a/lms/templates/course_list.html +++ b/lms/templates/course_list.html @@ -1,12 +1,12 @@
- {% if courses | length %} -
- {{ title }} -
-
- {% for course in courses %} - {{ widgets.CourseCard(course=course, read_only=False) }} - {% endfor %} -
-{% endif %} + {% if courses | length %} +
+ {{ title }} +
+
+ {% for course in courses %} + {{ widgets.CourseCard(course=course, read_only=False) }} + {% endfor %} +
+ {% endif %}
diff --git a/lms/www/courses/course.py b/lms/www/courses/course.py index e09aa052..5956b1f5 100644 --- a/lms/www/courses/course.py +++ b/lms/www/courses/course.py @@ -1,5 +1,4 @@ import frappe -from lms.lms.doctype.lms_settings.lms_settings import check_profile_restriction from lms.lms.utils import get_membership, has_course_moderator_role, is_instructor, is_certified, get_evaluation_details, redirect_to_courses_list def get_context(context): @@ -46,7 +45,6 @@ def set_course_context(context, course_name): membership = get_membership(course.name, frappe.session.user) context.course.query_parameter = "?batch=" + membership.batch if membership and membership.batch else "" context.membership = membership - context.restriction = check_profile_restriction() context.show_start_learing_cta = show_start_learing_cta(course, membership, context.restriction) context.certificate = is_certified(course.name) eval_details = get_evaluation_details(course.name) diff --git a/lms/www/courses/index.html b/lms/www/courses/index.html index e57a5da9..21a50117 100644 --- a/lms/www/courses/index.html +++ b/lms/www/courses/index.html @@ -1,13 +1,20 @@ {% extends "templates/base.html" %} -{% block title %}{{ 'Courses' }}{% endblock %} + + +{% block title %} +{{ 'Courses' }} +{% endblock %} + + {% block head_include %} {% include "public/icons/symbol-defs.svg" %} {% endblock %} + {% block content %}
- {% if restriction.restrict %} + {% if restriction %} {% set profile_link = " profile " %}
diff --git a/lms/www/courses/index.py b/lms/www/courses/index.py index 52694424..ce03c98a 100644 --- a/lms/www/courses/index.py +++ b/lms/www/courses/index.py @@ -1,11 +1,16 @@ import frappe from frappe import _ -from lms.lms.doctype.lms_settings.lms_settings import check_profile_restriction +from lms.lms.utils import check_profile_restriction, get_restriction_details + def get_context(context): context.no_cache = 1 context.live_courses, context.upcoming_courses = get_courses() context.restriction = check_profile_restriction() + + if context.restriction: + context.restriction_details = get_restriction_details() + context.metatags = { "title": _("All Live Courses"), "image": frappe.db.get_single_value("Website Settings", "banner_image"), @@ -13,6 +18,7 @@ def get_context(context): "keywords": "All Courses, Courses, Learn" } + def get_courses(): courses = frappe.get_all("LMS Course", filters={"published": True},