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
+ # Extending _save_m2m() instead of save() lets us support both save(commit=True/False)
23
+ def _save_m2m (self ):
24
+ super ()._save_m2m ()
25
+
26
+ # Delete any files from storage that might have been cleared
27
+ for a in _ARTIFACTS :
28
+ if self ._previous_file_fields [a ] and not getattr (self .instance , a ):
29
+ self ._previous_file_fields [a ].delete (save = False )
30
+
5
31
6
32
@admin .register (Release )
7
33
class ReleaseAdmin (admin .ModelAdmin ):
@@ -10,6 +36,7 @@ class ReleaseAdmin(admin.ModelAdmin):
10
36
("Dates" , {"fields" : ["date" , "eol_date" ]}),
11
37
("Artifacts" , {"fields" : ["tarball" , "wheel" , "checksum" ]}),
12
38
]
39
+ form = ReleaseAdminForm
13
40
list_display = (
14
41
"version" ,
15
42
"show_is_published" ,
@@ -25,16 +52,6 @@ class ReleaseAdmin(admin.ModelAdmin):
25
52
list_filter = ("status" , "is_lts" , "is_active" )
26
53
ordering = ("-major" , "-minor" , "-micro" , "-status" , "-iteration" )
27
54
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
55
@admin .display (
39
56
description = "status" ,
40
57
ordering = "status" ,
0 commit comments