Repository navigation
Expand file tree
/
Copy pathadmin.py
More file actions
269 lines (236 loc) · 7.56 KB
/
Copy pathadmin.py
File metadata and controls
269 lines (236 loc) · 7.56 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import structlog
from django.contrib import admin, messages
from django.http import HttpResponseRedirect
from django.utils.html import format_html
from siarnaq.api.compete.models import Match
from siarnaq.api.episodes.models import (
EligibilityCriterion,
Episode,
Map,
Tournament,
TournamentRound,
)
from siarnaq.api.user.models import User
logger = structlog.get_logger(__name__)
@admin.action(description="Export all submitted resumes")
def export_resumes(modeladmin, request, queryset):
return HttpResponseRedirect(User.objects.export_resumes(episodes=queryset))
class MapInline(admin.TabularInline):
model = Map
extra = 0
fields = ("name", "is_public")
ordering = ("name",)
@admin.register(Episode)
class EpisodeAdmin(admin.ModelAdmin):
actions = [export_resumes]
fieldsets = (
(
"General",
{
"fields": (
"name_short",
"name_long",
"language",
"blurb",
"eligibility_criteria",
),
},
),
(
"Distribution",
{
"fields": (
"scaffold",
"artifact_name",
"release_version_public",
"release_version_saturn",
),
},
),
(
"Release options",
{
"fields": ("registration", "game_release", "game_archive"),
},
),
(
"Match management",
{
"fields": ("submission_frozen", "autoscrim_schedule"),
},
),
)
filter_horizontal = ("eligibility_criteria",)
inlines = [MapInline]
list_display = (
"name_short",
"name_long",
"registration",
"game_release",
"game_archive",
)
ordering = ("-game_release",)
search_fields = ("name_short", "name_long")
search_help_text = "Search for a full or abbreviated name."
@admin.register(Map)
class MapAdmin(admin.ModelAdmin):
fields = ("name", "episode", "is_public")
list_display = ("name", "episode", "is_public")
list_filter = ("episode", "is_public")
list_select_related = ("episode",)
ordering = ("-episode__game_release", "name")
search_fields = ("name",)
search_help_text = "Search for a map name."
@admin.register(EligibilityCriterion)
class EligibilityCriterionAdmin(admin.ModelAdmin):
fields = ("title", "description", "icon")
list_display = ("title", "icon")
ordering = ("title",)
search_fields = ("title",)
search_help_text = "Search for a title."
class TournamentRoundInline(admin.TabularInline):
model = TournamentRound
extra = 0
fields = ("name", "maps", "external_id", "release_status")
ordering = (
"display_order",
"external_id",
)
raw_id_fields = ("maps",)
readonly_fields = ("external_id",)
def get_queryset(self, request):
return super().get_queryset(request).prefetch_related("maps")
@admin.action(description="Initialize a tournament")
def initialize(modeladmin, request, queryset):
logger.info("initialize", message="Initializing tournaments.")
for tournament in queryset:
tournament.initialize()
@admin.register(Tournament)
class TournamentAdmin(admin.ModelAdmin):
actions = [initialize]
fieldsets = (
(
"General",
{"fields": ("name_short", "name_long", "episode", "style", "blurb")},
),
(
"Release options",
{
"fields": (
"submission_freeze",
"submission_unfreeze",
"display_date",
"is_public",
),
},
),
(
"Eligibility options",
{
"fields": (
"eligibility_includes",
"eligibility_excludes",
"require_resume",
),
},
),
(
"Bracket service configuration",
{
"fields": (
"external_id_private",
"external_id_public",
),
},
),
)
filter_horizontal = ("eligibility_includes", "eligibility_excludes")
inlines = [TournamentRoundInline]
list_display = (
"name_short",
"name_long",
"episode",
"display_date",
"submission_freeze",
"is_public",
)
list_filter = ("episode",)
list_select_related = ("episode",)
ordering = ("-episode__game_release", "-submission_freeze")
search_fields = ("name_short", "name_long")
search_help_text = "Search for a full or abbreviated name."
class MatchInline(admin.TabularInline):
model = Match
classes = ("collapse",)
extra = 0
fields = ("__str__", "created", "replay_link", "status")
ordering = ("-pk",)
readonly_fields = fields
def get_queryset(self, request):
return super().get_queryset(request).prefetch_related("participants__team")
def has_add_permission(self, request, obj):
return False
def has_delete_permission(self, request, obj):
return False
@admin.display(description="Replay link")
def replay_link(self, obj):
link = "https://releases.battlecode.org/client/{}/{}/visualizer.html?{}".format(
obj.episode.artifact_name,
obj.episode.release_version_public,
# Client should make this something urlencode-able, instead of the below...
"tournamentMode&" + obj.get_replay_url(),
)
return format_html('<a href="{}">Watch</a>', link)
@admin.action(description="Create and enqueue matches")
def enqueue(modeladmin, request, queryset):
logger.info("round_enqueue", message="Enqueueing tournament rounds.")
for round in queryset:
try:
round.enqueue()
except RuntimeError as e:
messages.error(request, str(e))
@admin.action(description="Release results to public bracket service")
def release_to_public_bracket(modeladmin, request, queryset):
logger.info("round_release", message="Releasing to public bracket.")
for round in queryset:
round.request_publish_to_bracket(is_public=False)
@admin.register(TournamentRound)
class TournamentRoundAdmin(admin.ModelAdmin):
actions = [enqueue, release_to_public_bracket]
fields = (
"name",
"tournament",
"display_order",
"external_id",
"release_status",
"maps",
"in_progress",
)
inlines = [MatchInline]
list_display = (
"name",
"external_id",
"tournament",
"release_status",
"in_progress",
)
list_filter = ("tournament", "release_status")
list_select_related = ("tournament",)
ordering = (
"-tournament__submission_freeze",
"tournament__name_short",
"display_order",
"external_id",
)
def get_queryset(self, request):
return (
super()
.get_queryset(request)
.select_related("tournament")
.prefetch_related("matches__episode")
)
def formfield_for_manytomany(self, db_field, request, **kwargs):
pk = request.resolver_match.kwargs.get("object_id", None)
if db_field.name == "maps" and pk is not None:
kwargs["queryset"] = Map.objects.filter(episode__tournaments__rounds=pk)
return super().formfield_for_manytomany(db_field, request, **kwargs)