Improve Django admin listings for program_enrollments

This commit is contained in:
Kyle McCormick
2020-03-18 12:09:05 -04:00
committed by Kyle McCormick
parent f4d5bc22f9
commit b40b55cbbf
2 changed files with 82 additions and 15 deletions

View File

@@ -1,10 +1,9 @@
# -*- coding: utf-8 -*-
"""
Admin tool for the Program Enrollments models
"""
from django.contrib import admin
from django.urls import reverse
from django.utils.html import format_html
from lms.djangoapps.program_enrollments.models import ProgramCourseEnrollment, ProgramEnrollment
@@ -13,18 +12,93 @@ class ProgramEnrollmentAdmin(admin.ModelAdmin):
"""
Admin tool for the ProgramEnrollment model
"""
list_display = ('id', 'user', 'external_user_key', 'program_uuid', 'curriculum_uuid', 'status')
# Config for instance listing.
list_display = (
'id',
'status',
'user',
'external_user_key',
'program_uuid',
'curriculum_uuid',
)
list_filter = ('status',)
search_fields = ('user__username', 'external_user_key', 'program_uuid')
# Config for instance editor.
raw_id_fields = ('user',)
search_fields = ('user__username',)
def _pce_pe_id(pce):
"""
Generate a link to edit program enrollment, with ID and status in link text.
"""
pe = pce.program_enrollment
if not pe:
return None
link_url = reverse(
"admin:program_enrollments_programenrollment_change",
args=[pe.id],
)
link_text = "id={pe.id:05} ({pe.status})".format(pe=pe)
return format_html("<a href={}>{}</a>", link_url, link_text)
def _pce_pe_user(pce):
return pce.program_enrollment.user
def _pce_pe_external_user_key(pce):
return pce.program_enrollment.external_user_key
def _pce_pe_program_uuid(pce):
return pce.program_enrollment.program_uuid
def _pce_ce(pce):
"""
Generate text for course enrollment, including ID and is_active value.
"""
enrollment = pce.course_enrollment
if not enrollment:
return None
active_string = "Active" if enrollment.is_active else "Inactive"
return "id={enrollment.id:09} ({active_string})".format(
enrollment=enrollment, active_string=active_string
)
_pce_pe_id.short_description = "Program Enrollment"
_pce_pe_user.short_description = "Pgm Enrollment: User"
_pce_pe_external_user_key.short_description = "Pgm Enrollment: Ext User Key"
_pce_pe_program_uuid.short_description = "Pgm Enrollment: Pgm UUID"
_pce_ce.short_description = "Course Enrollment"
class ProgramCourseEnrollmentAdmin(admin.ModelAdmin):
"""
Admin tool for the ProgramCourseEnrollment model
"""
list_display = ('id', 'program_enrollment', 'course_enrollment', 'course_key', 'status')
list_filter = ('course_key',)
# Config for instance listing.
list_display = (
'id',
'status',
_pce_pe_id,
_pce_pe_user,
_pce_pe_external_user_key,
_pce_pe_program_uuid,
_pce_ce,
'course_key',
)
list_filter = ('status', 'course_key')
search_fields = (
'program_enrollment__user__username',
'program_enrollment__external_user_key',
'program_enrollment__program_uuid',
'course_key',
)
# Config for instance editor.
raw_id_fields = ('program_enrollment', 'course_enrollment')

View File

@@ -24,20 +24,13 @@ class ProgramEnrollmentAdminTests(TestCase):
def test_program_enrollment_admin(self):
request = mock.Mock()
expected_list_display = (
'id', 'user', 'external_user_key', 'program_uuid', 'curriculum_uuid', 'status'
'id', 'status', 'user', 'external_user_key', 'program_uuid', 'curriculum_uuid'
)
assert expected_list_display == self.program_admin.get_list_display(request)
expected_raw_id_fields = ('user',)
assert expected_raw_id_fields == self.program_admin.raw_id_fields
def test_program_course_enrollment_admin(self):
request = mock.Mock()
expected_list_display = (
'id', 'program_enrollment', 'course_enrollment', 'course_key', 'status'
)
assert expected_list_display == self.program_course_admin.get_list_display(request)
expected_raw_id_fields = ('program_enrollment', 'course_enrollment')
assert expected_raw_id_fields == self.program_course_admin.raw_id_fields