perf: course cards get all memberships at once

This commit is contained in:
Jannat Patel
2022-09-29 16:59:08 +05:30
parent 41a9e422d5
commit 246b26079b
9 changed files with 82 additions and 47 deletions

View File

@@ -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": []
}

View File

@@ -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/"
}

View File

@@ -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

View File

@@ -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) %}
<div class="common-card-style course-card" data-course="{{ course.name }}">
<div class="course-image {% if not course.image %}default-image{% endif %}"
<div class="course-image {% if not course.image %} default-image {% endif %}"
{% if course.image %} style="background-image: url( {{ course.image | urlencode }} );" {% endif %}>
<div class="course-tags">
{% for tag in get_tags(course.name) %}
<div class="course-card-pills">{{ tag }}</div>
<div class="course-card-pills">{{ tag }}</div>
{% endfor %}
</div>
{% if not course.image %}
<div class="default-image-text">{{ course.title[0] }}</div>
<div class="default-image-text">{{ course.title[0] }}</div>
{% endif %}
</div>
<div class="course-card-content">
<div class="course-card-meta">
{% set lesson_count = get_lesson_count(course.name) %}
@@ -104,27 +108,21 @@
</div>
{% if read_only %}
<a class="stretched-link" href="/courses/{{ course.name }}"></a>
{% else %}
<a class="stretched-link" href="/courses/{{ course.name }}"></a>
{% 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 %}
<a class="stretched-link" href="/courses/{{ course.name }}"></a>
{% if progress != 100 and membership and not course.upcoming %}
<a class="stretched-link" href="{{ get_lesson_url(course.name, lesson_index) }}{{ query_parameter }}"></a>
{% elif course.upcoming %}
<a class="stretched-link" href="/courses/{{ course.name }}"></a>
{% else %}
<a class="stretched-link" href="/courses/{{ course.name }}"></a>
{% elif membership %}
<a class="stretched-link" href="{{ get_lesson_url(course.name, lesson_index) }}{{ query_parameter }}"></a>
{% else %}
<a class="stretched-link" href="/courses/{{ course.name }}"></a>
{% endif %}
{% endif %}
{% endif %}
</div>
</div>

View File

@@ -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;
}

View File

@@ -1,12 +1,12 @@
<div class="{{ classes }}">
{% if courses | length %}
<div class="course-home-headings">
{{ title }}
</div>
<div class="cards-parent">
{% for course in courses %}
{{ widgets.CourseCard(course=course, read_only=False) }}
{% endfor %}
</div>
{% endif %}
{% if courses | length %}
<div class="course-home-headings">
{{ title }}
</div>
<div class="cards-parent">
{% for course in courses %}
{{ widgets.CourseCard(course=course, read_only=False) }}
{% endfor %}
</div>
{% endif %}
</div>

View File

@@ -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)

View File

@@ -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 %}
<div class="common-page-style">
<div class="container">
{% if restriction.restrict %}
{% if restriction %}
{% set profile_link = "<a href='/edit-profile'> profile </a>" %}
<div class="empty-state">
<div class="course-home-headings text-center mb-0" style="color: inherit;">

View File

@@ -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},