Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: iterate on django admin fields for restricted runs #974

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions enterprise_catalog/apps/catalog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@
)


def _html_list_from_objects(objs, viewname, str_callback=None):
str_callback = str_callback or str
def _html_list_from_objects(objs, viewname, str_callback=str):
"""
Get a pretty, clickable list of objects.

Args:
objs (iterable of Django ORM objects): List/queryset of objects to display.
viewname (str): The `viewname` representing the django admin "change" view for the objects in obj.
str_callback (callable): Optionally, a function to stringify one object for display purposes.
"""
return format_html_join(
# I already tried proper HTML lists, but they format really weird in django admin.
sep=mark_safe('<br>'),
format_string='<a href="{}">{}</a>',
args_generator=((reverse(viewname, args=[obj.pk]), str_callback(obj)) for obj in objs),
Expand Down Expand Up @@ -73,7 +81,8 @@ class ContentMetadataAdmin(UnchangeableMixin):
'associated_content_metadata',
'get_catalog_queries',
'get_catalogs',
'get_restricted_courses',
'get_restricted_courses_for_this_course',
'get_restricted_courses_for_this_restricted_run',
'modified',
)
exclude = (
Expand All @@ -96,15 +105,31 @@ def get_catalogs(self, obj):
)
return _html_list_from_objects(catalogs, "admin:catalog_enterprisecatalog_change")

@admin.display(description='Restricted For Courses')
def get_restricted_courses(self, obj):
@admin.display(description='Restricted Courses For This Course')
def get_restricted_courses_for_this_course(self, obj):
restricted_courses = RestrictedCourseMetadata.objects.filter(unrestricted_parent=obj)
return _html_list_from_objects(restricted_courses, "admin:catalog_restrictedcoursemetadata_change")

@admin.display(description='Restricted Courses For This Restricted Run')
def get_restricted_courses_for_this_restricted_run(self, obj):
restricted_runs_allowed_for_restricted_course = RestrictedRunAllowedForRestrictedCourse.objects.select_related(
'course',
).filter(
run=obj,
)
restricted_courses = (relationship.course for relationship in restricted_runs_allowed_for_restricted_course)
return _html_list_from_objects(restricted_courses, "admin:catalog_contentmetadata_change")
return _html_list_from_objects(restricted_courses, "admin:catalog_restrictedcoursemetadata_change")
Comment on lines -107 to +121
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this PR also fixes a bug from the previous PR where we display an object, but link to the wrong model.


def get_form(self, *args, **kwargs):
addl_help_texts = {
'get_restricted_courses_for_this_course': (
'If this is a course, list any "restricted" versions of this course.'
),
'get_restricted_courses_for_this_restricted_run': (
'If this is a restricted run, list all RestrictedCourses to which it is related.'
),
}
return super().get_form(*args, **(kwargs | {'help_texts': addl_help_texts}))


@admin.register(RestrictedCourseMetadata)
Expand Down
Loading