From 8bf269cb22d53b7d7f0af5adcbc467a27d6ec7fc Mon Sep 17 00:00:00 2001 From: zawan-ila <87228907+zawan-ila@users.noreply.github.com> Date: Thu, 9 Jan 2025 15:12:16 +0500 Subject: [PATCH] perf: optimize loading of django admin program pages (#4537) --- course_discovery/apps/course_metadata/admin.py | 15 ++++++++++++--- .../apps/course_metadata/tests/test_widgets.py | 6 +++--- course_discovery/apps/course_metadata/widgets.py | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/course_discovery/apps/course_metadata/admin.py b/course_discovery/apps/course_metadata/admin.py index 20b668605f..8c7da6330f 100644 --- a/course_discovery/apps/course_metadata/admin.py +++ b/course_discovery/apps/course_metadata/admin.py @@ -7,8 +7,9 @@ from django.db.utils import IntegrityError from django.forms import CheckboxSelectMultiple, ModelForm from django.http import HttpResponseRedirect +from django.templatetags.static import static from django.urls import re_path, reverse -from django.utils.html import format_html +from django.utils.html import format_html, html_safe from django.utils.translation import gettext_lazy as _ from django_object_actions import DjangoObjectActions from parler.admin import TranslatableAdmin @@ -53,6 +54,13 @@ class Meta: } +@html_safe +class SortableSelectJSPath: + def __str__(self): + abs_path = static('js/sortable_select.js') + return f'' + + class ProgramEligibilityFilter(admin.SimpleListFilter): title = _('eligible for one-click purchase') parameter_name = 'eligible_for_one_click_purchase' @@ -125,6 +133,7 @@ class ProductValueAdmin(admin.ModelAdmin): list_display = [ 'id', 'per_click_usa', 'per_click_international', 'per_lead_usa', 'per_lead_international' ] + search_fields = ('id',) @admin.register(Course) @@ -390,7 +399,7 @@ class ProgramAdmin(DjangoObjectActions, SimpleHistoryAdmin): raw_id_fields = ('video',) autocomplete_fields = ( 'corporate_endorsements', 'faq', 'individual_endorsements', 'job_outlook_items', - 'expected_learning_items', + 'expected_learning_items', 'in_year_value' ) search_fields = ('uuid', 'title', 'marketing_slug') exclude = ('card_image_url',) @@ -523,7 +532,7 @@ class Media: js = ( 'bower_components/jquery-ui/ui/minified/jquery-ui.min.js', 'bower_components/jquery/dist/jquery.min.js', - 'js/sortable_select.js' + SortableSelectJSPath() ) diff --git a/course_discovery/apps/course_metadata/tests/test_widgets.py b/course_discovery/apps/course_metadata/tests/test_widgets.py index 6f121be99e..e1b7497206 100644 --- a/course_discovery/apps/course_metadata/tests/test_widgets.py +++ b/course_discovery/apps/course_metadata/tests/test_widgets.py @@ -7,9 +7,9 @@ @ddt.ddt class SortedModelSelect2MultipleTests(TestCase): @ddt.data( - (['1', '2'], [1, 2, 3]), - (['2', '1'], [2, 1, 3]), - (['3'], [3, 1, 2]), + (['1', '2'], [1, 2]), + (['2', '1'], [2, 1]), + (['3'], [3,]), ) @ddt.unpack def test_optgroups_are_sorted(self, value, result_order): diff --git a/course_discovery/apps/course_metadata/widgets.py b/course_discovery/apps/course_metadata/widgets.py index 8d789d47fb..38635ee4eb 100644 --- a/course_discovery/apps/course_metadata/widgets.py +++ b/course_discovery/apps/course_metadata/widgets.py @@ -29,4 +29,4 @@ def optgroups(self, name, value, attrs=None): ordered.append(item) break - return ordered + unselected + return ordered