forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
182 lines (148 loc) · 5.01 KB
/
admin.py
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
174
175
176
177
178
179
180
181
182
"""Django admin interface for `~builds.models.Build` and related models."""
from django.contrib import admin, messages
from polymorphic.admin import PolymorphicChildModelAdmin, PolymorphicParentModelAdmin
from readthedocs.builds.models import (
Build,
BuildCommandResult,
RegexAutomationRule,
Version,
VersionAutomationRule,
)
from readthedocs.core.utils import trigger_build
from readthedocs.core.utils.admin import pretty_json_field
from readthedocs.projects.models import HTMLFile
from readthedocs.search.utils import _indexing_helper
class BuildCommandResultInline(admin.TabularInline):
model = BuildCommandResult
fields = ("command", "exit_code", "output")
class BuildAdmin(admin.ModelAdmin):
fields = (
"project",
"version",
"type",
"state",
"error",
"success",
"cold_storage",
"date",
"builder",
"length",
"build_config_file",
"pretty_config",
)
readonly_fields = (
"date", # required to be read-only because it's a @property
"pretty_config", # required to be read-only because it's a @property
"builder",
"length",
)
list_display = (
"id",
"project_slug",
"version_slug",
"success",
"type",
"state",
"date",
"builder",
"length",
)
list_filter = ("type", "state", "success")
list_select_related = ("project", "version")
raw_id_fields = ("project", "version")
inlines = (BuildCommandResultInline,)
search_fields = ("project__slug", "version__slug")
def project_slug(self, obj):
return obj.project.slug
def version_slug(self, obj):
return obj.version.slug
def pretty_config(self, instance):
return pretty_json_field(instance, "config")
pretty_config.short_description = "Config File"
class VersionAdmin(admin.ModelAdmin):
list_display = (
"slug",
"project_slug",
"type",
"privacy_level",
"active",
"built",
)
readonly_fields = (
"pretty_config", # required to be read-only because it's a @property
)
list_filter = ("type", "privacy_level", "active", "built")
search_fields = ("slug", "project__slug")
raw_id_fields = ("project",)
actions = ["build_version", "reindex_version", "wipe_version_indexes"]
def project_slug(self, obj):
return obj.project.slug
def pretty_config(self, instance):
return pretty_json_field(instance, "config")
pretty_config.short_description = "Config File"
def build_version(self, request, queryset):
"""Trigger a build for the project version."""
total = 0
for version in queryset:
trigger_build(
project=version.project,
version=version,
)
total += 1
messages.add_message(
request,
messages.INFO,
"Triggered builds for {} version(s).".format(total),
)
build_version.short_description = "Build version"
def reindex_version(self, request, queryset):
"""Reindexes all selected versions to ES."""
html_objs_qs = []
for version in queryset.iterator():
html_objs = HTMLFile.objects.filter(
project=version.project, version=version
)
if html_objs.exists():
html_objs_qs.append(html_objs)
if html_objs_qs:
_indexing_helper(html_objs_qs, wipe=False)
self.message_user(request, "Task initiated successfully.", messages.SUCCESS)
reindex_version.short_description = "Reindex version to ES"
def wipe_version_indexes(self, request, queryset):
"""Wipe selected versions from ES."""
html_objs_qs = []
for version in queryset.iterator():
html_objs = HTMLFile.objects.filter(
project=version.project, version=version
)
if html_objs.exists():
html_objs_qs.append(html_objs)
if html_objs_qs:
_indexing_helper(html_objs_qs, wipe=True)
self.message_user(
request,
"Task initiated successfully",
messages.SUCCESS,
)
wipe_version_indexes.short_description = "Wipe version from ES"
@admin.register(RegexAutomationRule)
class RegexAutomationRuleAdmin(PolymorphicChildModelAdmin, admin.ModelAdmin):
raw_id_fields = ("project",)
base_model = RegexAutomationRule
@admin.register(VersionAutomationRule)
class VersionAutomationRuleAdmin(PolymorphicParentModelAdmin, admin.ModelAdmin):
base_model = VersionAutomationRule
list_display = (
"id",
"project",
"priority",
"predefined_match_arg",
"match_arg",
"action",
"version_type",
)
child_models = (RegexAutomationRule,)
search_fields = ("project__slug",)
list_filter = ("action", "version_type")
admin.site.register(Build, BuildAdmin)
admin.site.register(Version, VersionAdmin)