forked from openedx/openedx-authz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
89 lines (69 loc) · 3 KB
/
admin.py
File metadata and controls
89 lines (69 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Admin configuration for openedx_authz."""
import json
from casbin_adapter.models import CasbinRule
from django import forms
from django.contrib import admin
from django.utils.html import format_html
from openedx_authz.models import AuthzCourseAuthoringMigrationRun, ExtendedCasbinRule
def pretty_json(value) -> str:
"""Return an indented JSON representation of a value."""
if value is None:
return "-"
try:
formatted = json.dumps(value, indent=2, ensure_ascii=False)
except (TypeError, ValueError):
return str(value)
return format_html("<pre>{}</pre>", formatted)
class CasbinRuleForm(forms.ModelForm):
"""Custom form for CasbinRule to make v3, v4, v5 fields optional."""
class Meta:
"""Meta class for CasbinRuleForm."""
model = CasbinRule
fields = "__all__"
def __init__(self, *args, **kwargs):
"""Initialize CasbinRuleForm."""
super().__init__(*args, **kwargs)
# Make v2, v3, v4, v5 optional in the form
# These fields are not always required depending on the policy type
self.fields["v2"].required = False
self.fields["v3"].required = False
self.fields["v4"].required = False
self.fields["v5"].required = False
class ExtendedCasbinRuleInline(admin.StackedInline):
"""Inline admin for ExtendedCasbinRule to display additional metadata."""
model = ExtendedCasbinRule
extra = 0
fields = ("casbin_rule_key", "scope", "subject", "description", "metadata", "created_at", "updated_at")
readonly_fields = ("casbin_rule_key", "scope", "subject", "created_at", "updated_at")
can_delete = False
@admin.register(CasbinRule)
class CasbinRuleAdmin(admin.ModelAdmin):
"""Admin for CasbinRule to display additional metadata."""
form = CasbinRuleForm
list_display = ("id", "ptype", "v0", "v1", "v2", "v3", "v4", "v5")
search_fields = ("ptype", "v0", "v1", "v2", "v3", "v4", "v5")
list_filter = ("ptype",)
# TODO: In a future, possibly we should only show an inline for the rules that
# have an extended rule, and show the subject and scope information in detail.
inlines = [ExtendedCasbinRuleInline]
@admin.register(AuthzCourseAuthoringMigrationRun)
class AuthzCourseAuthoringMigrationRunAdmin(admin.ModelAdmin):
"""Admin for AuthzCourseAuthoringMigrationRun to display additional metadata."""
list_display = ("id", "scope_type", "scope_key", "migration_type", "status", "created_at", "updated_at")
search_fields = ("scope_type", "scope_key", "migration_type", "status")
list_filter = ("scope_type", "migration_type", "status")
readonly_fields = (
"scope_type",
"scope_key",
"migration_type",
"status",
"pretty_metadata",
"completed_at",
"created_at",
"updated_at",
)
fields = readonly_fields
@admin.display(description="Metadata")
def pretty_metadata(self, obj):
"""Return formatted JSON for the metadata field."""
return pretty_json(obj.metadata)