Skip to content

Commit

Permalink
chore: Add django admin for RestrictedCourseMetadata
Browse files Browse the repository at this point in the history
ENT-9569
  • Loading branch information
pwnage101 committed Oct 11, 2024
1 parent 93642d4 commit a3008dd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
81 changes: 76 additions & 5 deletions enterprise_catalog/apps/catalog/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin
from django.urls import reverse
from django.utils.html import format_html
from django.utils.html import format_html, format_html_join
from django.utils.safestring import mark_safe
from edx_rbac.admin import UserRoleAssignmentAdmin

from enterprise_catalog.apps.catalog.constants import (
Expand All @@ -15,9 +16,19 @@
ContentMetadata,
EnterpriseCatalog,
EnterpriseCatalogRoleAssignment,
RestrictedCourseMetadata,
)


def _html_list_from_objects(objs, viewname, str_callback=None):
str_callback = str_callback or str
return format_html_join(
sep=mark_safe('<br>'),
format_string='<a href="{}">{}</a>',
args_generator=((reverse(viewname, args=[obj.pk]), str_callback(obj)) for obj in objs),
)


class UnchangeableMixin(admin.ModelAdmin):
"""
Mixin for disabling changing models through the admin
Expand Down Expand Up @@ -59,17 +70,77 @@ class ContentMetadataAdmin(UnchangeableMixin):
)
readonly_fields = (
'associated_content_metadata',
'catalog_queries',
'get_catalog',
'get_catalog_queries',
'get_catalogs',
'modified',
)
exclude = (
'catalog_queries',
)

@admin.display(description='Catalog Queries')
def get_catalog_queries(self, obj):
catalog_queries = obj.catalog_queries.all()
return _html_list_from_objects(
objs=catalog_queries,
viewname="admin:catalog_catalogquery_change",
str_callback=lambda cq: cq.short_str_for_listings(),
)

@admin.display(description='Enterprise Catalogs')
def get_catalog(self, obj):
def get_catalogs(self, obj):
catalogs = EnterpriseCatalog.objects.filter(
catalog_query_id__in=obj.catalog_queries.all().values_list('id')
)
return f"{list(catalogs)}"
return _html_list_from_objects(catalogs, "admin:catalog_enterprisecatalog_change")


@admin.register(RestrictedCourseMetadata)
class RestrictedCourseMetadataAdmin(UnchangeableMixin):
""" Admin configuration for the custom RestrictedCourseMetadata model. """
list_display = (
'content_key',
'get_catalog_query_for_list',
'get_unrestricted_parent',
)
search_fields = (
'content_key',
'catalog_query',
)
readonly_fields = (
'get_catalog_query',
'get_catalogs',
'modified',
)
exclude = (
'catalog_query',
)

@admin.display(
description='Catalog Query'
)
def get_catalog_query_for_list(self, obj):
link = reverse("admin:catalog_catalogquery_change", args=[obj.catalog_query.id])
return format_html('<a href="{}">{}</a>', link, obj.catalog_query.short_str_for_listings())

@admin.display(
description='Catalog Query'
)
def get_catalog_query(self, obj):
link = reverse("admin:catalog_catalogquery_change", args=[obj.catalog_query.id])
return format_html('<a href="{}">{}</a>', link, obj.catalog_query.pretty_print_content_filter())

@admin.display(
description='Unrestricted Parent'
)
def get_unrestricted_parent(self, obj):
link = reverse("admin:catalog_contentmetadata_change", args=[obj.unrestricted_parent.id])
return format_html('<a href="{}">{}</a>', link, str(obj.unrestricted_parent))

@admin.display(description='Enterprise Catalogs')
def get_catalogs(self, obj):
catalogs = EnterpriseCatalog.objects.filter(catalog_query=obj.catalog_query)
return _html_list_from_objects(catalogs, "admin:catalog_enterprisecatalog_change")


@admin.register(CatalogQuery)
Expand Down
14 changes: 9 additions & 5 deletions enterprise_catalog/apps/catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ def __str__(self):
f"and content_filter '{self.pretty_print_content_filter()}'>"
)

def short_str_for_listings(self):
"""
Return *short* human-readable string representation for listings.
"""
return (
f"<CatalogQuery: ({self.id}) with UUID '{self.uuid}'>"
)


class EnterpriseCatalog(TimeStampedModel):
"""
Expand Down Expand Up @@ -715,11 +723,7 @@ def __str__(self):
"""
Return human-readable string representation.
"""
return (
"<ContentMetadata for '{content_key}'>".format(
content_key=self.content_key
)
)
return f"<{self.__class__.__name__} for '{self.content_key}'>"


class ContentMetadata(BaseContentMetadata):
Expand Down

0 comments on commit a3008dd

Please sign in to comment.