1+ from django import forms
12from django .contrib import admin
23
34from .models import Release
45
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" : ".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+
531
632@admin .register (Release )
733class ReleaseAdmin (admin .ModelAdmin ):
@@ -10,6 +36,7 @@ class ReleaseAdmin(admin.ModelAdmin):
1036 ("Dates" , {"fields" : ["date" , "eol_date" ]}),
1137 ("Artifacts" , {"fields" : ["tarball" , "wheel" , "checksum" ]}),
1238 ]
39+ form = ReleaseAdminForm
1340 list_display = (
1441 "version" ,
1542 "show_is_published" ,
@@ -25,16 +52,6 @@ class ReleaseAdmin(admin.ModelAdmin):
2552 list_filter = ("status" , "is_lts" , "is_active" )
2653 ordering = ("-major" , "-minor" , "-micro" , "-status" , "-iteration" )
2754
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-
3855 @admin .display (
3956 description = "status" ,
4057 ordering = "status" ,
0 commit comments