fix: course list page performance

This commit is contained in:
Jannat Patel
2022-09-29 13:19:09 +05:30
parent 0704d7f4a4
commit 41a9e422d5
3 changed files with 115 additions and 107 deletions

View File

@@ -198,7 +198,8 @@ jinja = {
"lms.lms.utils.has_course_instructor_role",
"lms.lms.utils.has_course_moderator_role",
"lms.lms.utils.get_certificates",
"lms.lms.utils.format_number"
"lms.lms.utils.format_number",
"lms.lms.utils.get_lesson_count"
],
"filters": []
}

View File

@@ -508,4 +508,10 @@ def create_notification_log(doc, method):
make_notification_logs(notification, users)
def get_lesson_count(course):
lesson_count = 0
chapters = frappe.get_all("Chapter Reference", {"parent": course}, ["chapter"])
for chapter in chapters:
lesson_count += frappe.db.count("Lesson Reference", {"parent": chapter.chapter})
return lesson_count

View File

@@ -14,116 +14,117 @@
{% endif %}
</div>
<div class="course-card-content">
<div class="course-card-meta">
{% if get_lessons(course.name) | length %}
<div class="vertically-center">
<svg class="icon icon-md">
<use href="#icon-education"></use>
</svg>
{{ get_lessons(course.name) | length }}
</div>
{% endif %}
{% if course.status and course.status != "Approved" %}
{% set pill_color = "gray" if course.status == "In Progress" else "orange" %}
<div class="pull-right indicator-pill {{ pill_color }} "> {{ course.status }} </div>
{% endif %}
{% set student_count = get_students(course.name) | length %}
{% set avg_rating = get_average_rating(course.name) %}
{% if student_count %}
<div class="course-card-content">
<div class="course-card-meta">
{% set lesson_count = get_lesson_count(course.name) %}
{% if lesson_count %}
<div class="vertically-center">
<svg class="icon icon-md">
<use class="" href="#icon-users">
<svg class="icon icon-md">
<use href="#icon-education"></use>
</svg>
{{ student_count }}
{{ lesson_count }}
</div>
{% endif %}
{% if avg_rating %}
<div class="vertically-center">
<svg class="icon icon-md">
<use href="#icon-star"></use>
</svg>
{{ frappe.utils.flt(avg_rating, frappe.get_system_settings("float_precision") or 3) }}
</div>
{% endif %}
{% if course.paid_certificate %}
<div class="vertically-center">
<svg class="icon icon-md">
<use href="#icon-badge"></use>
</svg>
<span class="certificate-price" data-price="{{ course.price_certificate }}">
{{ format_amount(course.price_certificate, course.currency) }}
</span>
</div>
{% endif %}
</div>
<div class="course-card-title">{{ course.title }}</div>
{% if membership and not read_only %}
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="{{ progress }}"
aria-valuemin="0" aria-valuemax="100" style="width:{{ progress }}%">
<span class="sr-only"> {{ progress }} {{ _("Complete") }} </span>
</div>
</div>
<div class="progress-percent">{{ progress }}% {{ _("Completed") }} </div>
{% endif %}
<div class="course-card-footer">
<span class="">
{% set instructors = get_instructors(course.name) %}
{% set ins_len = instructors | length %}
{% for instructor in instructors %}
{% if ins_len > 1 and loop.index == 1 %}
<div class="avatar-group overlap">
{% endif %}
{{ widgets.Avatar(member=instructor, avatar_class="avatar-small") }}
{% if ins_len > 1 and loop.index == ins_len %}
</div>
{% endif %}
{% endfor %}
<a class="button-links" href="{{ get_profile_url(instructors[0].username) }}">
<span class="course-instructor">
{% if ins_len == 1 %}
{{ instructors[0].full_name }}
{% elif ins_len == 2 %}
{{ instructors[0].full_name.split(" ")[0] }} and {{ instructors[1].full_name.split(" ")[0] }}
{% else %}
{% set suffix = "other" if ins_len - 1 == 1 else "others" %}
{{ instructors[0].full_name.split(" ")[0] }} and {{ ins_len - 1 }} {{ suffix }}
{% endif %}
{% if course.status and course.status != "Approved" %}
{% set pill_color = "gray" if course.status == "In Progress" else "orange" %}
<div class="pull-right indicator-pill {{ pill_color }} "> {{ course.status }} </div>
{% endif %}
{% set student_count = get_students(course.name) | length %}
{% set avg_rating = get_average_rating(course.name) %}
{% if student_count %}
<div class="vertically-center">
<svg class="icon icon-md">
<use class="" href="#icon-users">
</svg>
{{ student_count }}
</div>
{% endif %}
{% if avg_rating %}
<div class="vertically-center">
<svg class="icon icon-md">
<use href="#icon-star"></use>
</svg>
{{ frappe.utils.flt(avg_rating, frappe.get_system_settings("float_precision") or 3) }}
</div>
{% endif %}
{% if course.paid_certificate %}
<div class="vertically-center">
<svg class="icon icon-md">
<use href="#icon-badge"></use>
</svg>
<span class="certificate-price" data-price="{{ course.price_certificate }}">
{{ format_amount(course.price_certificate, course.currency) }}
</span>
</div>
{% endif %}
</div>
<div class="course-card-title">{{ course.title }}</div>
{% if membership and not read_only %}
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="{{ progress }}"
aria-valuemin="0" aria-valuemax="100" style="width:{{ progress }}%">
<span class="sr-only"> {{ progress }} {{ _("Complete") }} </span>
</div>
</div>
<div class="progress-percent">{{ progress }}% {{ _("Completed") }} </div>
{% endif %}
<div class="course-card-footer">
<span class="">
{% set instructors = get_instructors(course.name) %}
{% set ins_len = instructors | length %}
{% for instructor in instructors %}
{% if ins_len > 1 and loop.index == 1 %}
<div class="avatar-group overlap">
{% endif %}
{{ widgets.Avatar(member=instructor, avatar_class="avatar-small") }}
{% if ins_len > 1 and loop.index == ins_len %}
</div>
{% endif %}
{% endfor %}
<a class="button-links" href="{{ get_profile_url(instructors[0].username) }}">
<span class="course-instructor">
{% if ins_len == 1 %}
{{ instructors[0].full_name }}
{% elif ins_len == 2 %}
{{ instructors[0].full_name.split(" ")[0] }} and {{ instructors[1].full_name.split(" ")[0] }}
{% else %}
{% set suffix = "other" if ins_len - 1 == 1 else "others" %}
{{ instructors[0].full_name.split(" ")[0] }} and {{ ins_len - 1 }} {{ suffix }}
{% endif %}
</span>
</a>
</span>
</a>
</span>
</div>
{% if read_only %}
<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 "" %}
{% if progress == 100 %}
<a class="stretched-link" href="/courses/{{ course.name }}"></a>
{% elif course.upcoming %}
<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 %}
</div>
{% if read_only %}
<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 "" %}
{% if progress == 100 %}
<a class="stretched-link" href="/courses/{{ course.name }}"></a>
{% elif course.upcoming %}
<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 %}
</div>
</div>