forked from openedx/openedx-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
173 lines (145 loc) · 4.8 KB
/
admin.py
File metadata and controls
173 lines (145 loc) · 4.8 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Django admin for publishing models
"""
from __future__ import annotations
from django.contrib import admin
from openedx_learning.lib.admin_utils import ReadOnlyModelAdmin, one_to_one_related_model_html
from .models import LearningPackage, PublishableEntity, Published, PublishLog, PublishLogRecord
@admin.register(LearningPackage)
class LearningPackageAdmin(ReadOnlyModelAdmin):
"""
Read-only admin for LearningPackage model
"""
fields = ["key", "title", "uuid", "created", "updated"]
readonly_fields = ["key", "title", "uuid", "created", "updated"]
list_display = ["key", "title", "uuid", "created", "updated"]
search_fields = ["key", "title", "uuid"]
class PublishLogRecordTabularInline(admin.TabularInline):
"""
Inline read-only tabular view for PublishLogRecords
"""
model = PublishLogRecord
fields = (
"entity",
"title",
"old_version_num",
"new_version_num",
)
readonly_fields = fields
def get_queryset(self, request):
queryset = super().get_queryset(request)
return queryset.select_related("entity", "old_version", "new_version")
def old_version_num(self, pl_record: PublishLogRecord):
if pl_record.old_version is None:
return "-"
return pl_record.old_version.version_num
def new_version_num(self, pl_record: PublishLogRecord):
if pl_record.new_version is None:
return "-"
return pl_record.new_version.version_num
def title(self, pl_record: PublishLogRecord):
"""
Get the title to display for the PublishLogRecord
"""
if pl_record.new_version:
return pl_record.new_version.title
if pl_record.old_version:
return pl_record.old_version.title
return ""
@admin.register(PublishLog)
class PublishLogAdmin(ReadOnlyModelAdmin):
"""
Read-only admin view for PublishLog
"""
inlines = [PublishLogRecordTabularInline]
fields = ("uuid", "learning_package", "published_at", "published_by", "message")
readonly_fields = fields
list_display = fields
list_filter = ["learning_package"]
@admin.register(PublishableEntity)
class PublishableEntityAdmin(ReadOnlyModelAdmin):
"""
Read-only admin view for Publishable Entities
"""
list_display = [
"key",
"draft_version",
"published_version",
"uuid",
"learning_package",
"created",
"created_by",
"can_stand_alone",
]
list_filter = ["learning_package"]
search_fields = ["key", "uuid"]
fields = [
"key",
"draft_version",
"published_version",
"uuid",
"learning_package",
"created",
"created_by",
"see_also",
"can_stand_alone",
]
readonly_fields = [
"key",
"draft_version",
"published_version",
"uuid",
"learning_package",
"created",
"created_by",
"see_also",
"can_stand_alone",
]
def get_queryset(self, request):
queryset = super().get_queryset(request)
return queryset.select_related(
"learning_package", "published__version",
)
def see_also(self, entity):
return one_to_one_related_model_html(entity)
def draft_version(self, entity):
if entity.draft.version:
return entity.draft.version.version_num
return None
def published_version(self, entity):
if entity.published.version:
return entity.published.version.version_num
return None
@admin.register(Published)
class PublishedAdmin(ReadOnlyModelAdmin):
"""
Read-only admin view for Published model
"""
fields = ("entity", "version_num", "previous", "published_at", "message")
list_display = fields
def get_queryset(self, request):
queryset = super().get_queryset(request)
return queryset.select_related(
"entity",
"version",
"publish_log_record",
"publish_log_record__old_version",
"publish_log_record__publish_log",
)
def version_num(self, published_obj):
if published_obj.version:
return published_obj.version.version_num
return None
def previous(self, published_obj):
"""
Determine what to show in the "Previous" field
"""
old_version = published_obj.publish_log_record.old_version
# if there was no previous old version, old version is None
if not old_version:
return old_version
return old_version.version_num
def published_at(self, published_obj):
return published_obj.publish_log_record.publish_log.published_at
def message(self, published_obj):
return published_obj.publish_log_record.publish_log.message