Skip to content

Commit

Permalink
feat: show API serialization of policy details from admin screen
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Oct 12, 2023
1 parent 8d3edb1 commit 48cd87b
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions enterprise_access/apps/subsidy_access_policy/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
""" Admin configuration for subsidy_access_policy models. """
import json
import logging

from django.conf import settings
from django.contrib import admin
from django.utils.safestring import mark_safe
from django.utils.text import Truncator # for shortening a text
from djangoql.admin import DjangoQLSearchMixin
from pygments import highlight
from pygments.formatters import HtmlFormatter # pylint: disable=no-name-in-module
from pygments.lexers import JsonLexer # pylint: disable=no-name-in-module

from enterprise_access.apps.api.serializers.subsidy_access_policy import SubsidyAccessPolicyResponseSerializer
from enterprise_access.apps.subsidy_access_policy import constants, models

logger = logging.getLogger(__name__)


EVERY_SPEND_LIMIT_FIELD = [
'spend_limit',
'policy_spend_limit_dollars',
Expand Down Expand Up @@ -50,8 +60,32 @@ class BaseSubsidyAccessPolicyMixin(admin.ModelAdmin):
'created',
'modified',
'policy_spend_limit_dollars',
'api_serialized_repr',
)

@admin.display(description='REST API serialization')
def api_serialized_repr(self, obj):
"""
Convenience method to see what the policy details REST API
response is. Thanks to:
https://daniel.feldroy.com/posts/pretty-formatting-json-django-admin
for this styling idea.
"""
data = SubsidyAccessPolicyResponseSerializer(obj).data
json_string = json.dumps(data, indent=4, sort_keys=True)

# Get the Pygments formatter
formatter = HtmlFormatter(style='default')

# Highlight the data
response = highlight(json_string, JsonLexer(), formatter)

# Get the stylesheet
style = "<style>" + formatter.get_style_defs() + "</style><br>"

# Safe the output
return mark_safe(style + response)

def _short_description(self, obj):
return Truncator(str(obj.description)).chars(255)

Expand Down Expand Up @@ -110,6 +144,12 @@ class PerLearnerEnrollmentCreditAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAcc
] if not super_admin_enabled() else EVERY_SPEND_LIMIT_FIELD
}
),
(
'Extra',
{
'fields': ['api_serialized_repr'],
},
),
]


Expand Down Expand Up @@ -159,6 +199,12 @@ class PerLearnerSpendCreditAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAccessPo
] if not super_admin_enabled() else EVERY_SPEND_LIMIT_FIELD
}
),
(
'Extra',
{
'fields': ['api_serialized_repr'],
},
),
]

@admin.display(description='Per-learner spend limit (dollars)')
Expand Down Expand Up @@ -208,4 +254,10 @@ class LearnerContentAssignmentAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAcces
] if not super_admin_enabled() else EVERY_SPEND_LIMIT_FIELD
}
),
(
'Extra',
{
'fields': ['api_serialized_repr'],
},
),
]

0 comments on commit 48cd87b

Please sign in to comment.