1
+ from django import forms
1
2
from django .contrib import admin
2
3
3
4
from .models import Release
4
5
6
+ _ARTIFACTS = ["checksum" , "tarball" , "wheel" ]
7
+
8
+
9
+ class ReleaseAdminForm (forms .ModelForm ):
10
+ def __init__ (self , * args , ** kwargs ):
11
+ super ().__init__ (* args , ** kwargs )
12
+
13
+ # Add `accept` attributes to the artifact file fields to make it a bit
14
+ # easier to pick the right files in the browser's 'filepicker
15
+ extensions = {"tarball" : ".tar.gz" , "wheel" : ".whl" , "checksum" : ".asc,.txt" }
16
+ for field , accept in extensions .items ():
17
+ widget = self .fields [field ].widget
18
+ widget .attrs .setdefault ("accept" , accept )
19
+
20
+ self ._previous_file_fields = {a : getattr (self .instance , a ) for a in _ARTIFACTS }
21
+
22
+ def save (self , commit = True ):
23
+ if not commit :
24
+ raise ValueError ("ReleaseAdminForm.save() doesn't support commit=True" )
25
+ instance = super ().save (commit = commit )
26
+
27
+ # Delete any files from storage that might have been cleared
28
+ for a in _ARTIFACTS :
29
+ if self ._previous_file_fields [a ] and not getattr (instance , a ):
30
+ self ._previous_file_fields [a ].delete (save = False )
31
+ return instance
32
+
5
33
6
34
@admin .register (Release )
7
35
class ReleaseAdmin (admin .ModelAdmin ):
@@ -10,6 +38,7 @@ class ReleaseAdmin(admin.ModelAdmin):
10
38
("Dates" , {"fields" : ["date" , "eol_date" ]}),
11
39
("Artifacts" , {"fields" : ["tarball" , "wheel" , "checksum" ]}),
12
40
]
41
+ form = ReleaseAdminForm
13
42
list_display = (
14
43
"version" ,
15
44
"show_is_published" ,
@@ -25,16 +54,6 @@ class ReleaseAdmin(admin.ModelAdmin):
25
54
list_filter = ("status" , "is_lts" , "is_active" )
26
55
ordering = ("-major" , "-minor" , "-micro" , "-status" , "-iteration" )
27
56
28
- def get_form (self , request , obj = None , change = False , ** kwargs ):
29
- form_class = super ().get_form (request , obj = obj , change = change , ** kwargs )
30
- # Add `accept` attributes to the artifact file fields to make it a bit
31
- # easier to pick the right files in the browser's 'filepicker
32
- extensions = {"tarball" : ".tar.gz" , "wheel" : ".whl" , "checksum" : ".asc,.txt" }
33
- for field , accept in extensions .items ():
34
- widget = form_class .base_fields [field ].widget
35
- widget .attrs .setdefault ("accept" , accept )
36
- return form_class
37
-
38
57
@admin .display (
39
58
description = "status" ,
40
59
ordering = "status" ,
0 commit comments