1
1
"""Django admin interface for `~builds.models.Build` and related models."""
2
2
3
+ import json
3
4
from django .contrib import admin , messages
5
+ from django .utils .safestring import mark_safe
4
6
from polymorphic .admin import (
5
7
PolymorphicChildModelAdmin ,
6
8
PolymorphicParentModelAdmin ,
7
9
)
8
10
11
+ from pygments import highlight
12
+ from pygments .lexers import JsonLexer
13
+ from pygments .formatters import HtmlFormatter
14
+
9
15
from readthedocs .builds .models import (
10
16
Build ,
11
17
BuildCommandResult ,
19
25
from readthedocs .search .utils import _indexing_helper
20
26
21
27
28
+ def _pretty_config (instance ):
29
+ """
30
+ Function to display pretty version of our data.
31
+
32
+ Thanks to PyDanny: https://www.pydanny.com/pretty-formatting-json-django-admin.html
33
+ """
34
+
35
+ # Convert the data to sorted, indented JSON
36
+ response = json .dumps (instance .config , sort_keys = True , indent = 2 )
37
+
38
+ # Get the Pygments formatter
39
+ formatter = HtmlFormatter ()
40
+
41
+ # Highlight the data
42
+ response = highlight (response , JsonLexer (), formatter )
43
+
44
+ # Get the stylesheet
45
+ style = "<style>" + formatter .get_style_defs () + "</style><br>"
46
+
47
+ # Safe the output
48
+ return mark_safe (style + response )
49
+
50
+
22
51
class BuildCommandResultInline (admin .TabularInline ):
23
52
model = BuildCommandResult
24
53
fields = ('command' , 'exit_code' , 'output' )
@@ -34,6 +63,10 @@ class BuildAdmin(admin.ModelAdmin):
34
63
'success' ,
35
64
'length' ,
36
65
'cold_storage' ,
66
+ 'pretty_config' ,
67
+ )
68
+ readonly_fields = (
69
+ 'pretty_config' , # required to be read-only becuase it's a @property
37
70
)
38
71
list_display = (
39
72
'id' ,
@@ -54,6 +87,11 @@ class BuildAdmin(admin.ModelAdmin):
54
87
def version_name (self , obj ):
55
88
return obj .version .verbose_name
56
89
90
+ def pretty_config (self , instance ):
91
+ return _pretty_config (instance )
92
+
93
+ pretty_config .short_description = 'Config File'
94
+
57
95
58
96
class VersionAdmin (admin .ModelAdmin ):
59
97
@@ -65,6 +103,9 @@ class VersionAdmin(admin.ModelAdmin):
65
103
'active' ,
66
104
'built' ,
67
105
)
106
+ readonly_fields = (
107
+ 'pretty_config' , # required to be read-only becuase it's a @property
108
+ )
68
109
list_filter = ('type' , 'privacy_level' , 'active' , 'built' )
69
110
search_fields = ('slug' , 'project__slug' )
70
111
raw_id_fields = ('project' ,)
@@ -83,6 +124,10 @@ def wipe_selected_versions(self, request, queryset):
83
124
level = messages .SUCCESS
84
125
)
85
126
127
+ def pretty_config (self , instance ):
128
+ return _pretty_config (instance )
129
+
130
+ pretty_config .short_description = 'Config File'
86
131
wipe_selected_versions .short_description = 'Wipe selected versions'
87
132
88
133
def build_version (self , request , queryset ):
0 commit comments