Ticket #4979: 4979-admin-file-deletion.2.diff
File 4979-admin-file-deletion.2.diff, 4.1 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/files.py
188 188 return os.path.join(self.get_directory_name(), self.get_filename(filename)) 189 189 190 190 def save_form_data(self, instance, data): 191 if data and isinstance(data, UploadedFile): 191 if data is False: 192 getattr(instance, self.name).delete(save=False) 193 elif data and isinstance(data, UploadedFile): 192 194 getattr(instance, self.name).save(data.name, data, save=False) 193 195 194 196 def formfield(self, **kwargs): -
django/forms/fields.py
453 453 454 454 def clean(self, data, initial=None): 455 455 super(FileField, self).clean(initial or data) 456 if not self.required and data in EMPTY_VALUES: 457 return None 456 if not self.required: 457 if data is False: 458 return False 459 elif data in EMPTY_VALUES: 460 return None 458 461 elif not data and initial: 459 462 return initial 460 463 … … 483 486 PNG, possibly others -- whatever the Python Imaging Library supports). 484 487 """ 485 488 f = super(ImageField, self).clean(data, initial) 486 if f is None:487 return None489 if not f: 490 return f 488 491 elif not data and initial: 489 492 return initial 490 493 from PIL import Image -
django/contrib/admin/options.py
105 105 kwargs['widget'] = widgets.AdminTextInputWidget 106 106 return db_field.formfield(**kwargs) 107 107 108 # For FileFields and ImageFields add a link to the current file .108 # For FileFields and ImageFields add a link to the current file and a delete option where appropriate 109 109 if isinstance(db_field, models.ImageField) or isinstance(db_field, models.FileField): 110 kwargs['widget'] = widgets.AdminFileWidget 110 if db_field.blank: 111 kwargs['widget'] = widgets.AdminClearableFileWidget 112 else: 113 kwargs['widget'] = widgets.AdminFileWidget 111 114 return db_field.formfield(**kwargs) 112 115 113 116 # For ForeignKey or ManyToManyFields, use a special widget. -
django/contrib/admin/widgets.py
95 95 output.append(super(AdminFileWidget, self).render(name, value, attrs)) 96 96 return mark_safe(u''.join(output)) 97 97 98 99 class AdminClearableFileWidget(AdminFileWidget): 100 def render(self, name, value, attrs=None): 101 output = [super(AdminClearableFileWidget, self).render(name, value, attrs)] 102 if value and hasattr(value, "url"): 103 checkbox = forms.CheckboxInput() 104 checkbox_name = name + '-delete' 105 output.append(' ') 106 output.append(_('Delete?')) 107 output.append(checkbox.render(checkbox_name, False, attrs)) 108 return mark_safe(u''.join(output)) 109 110 def value_from_datadict(self, data, files, name): 111 """Checks to see whether a file was uploaded and if not, 112 whether the Delete checkbox was set. 113 114 To allow _has_changed to determine between no change and 115 and delete request, no change will return None and deletion 116 will return False. 117 """ 118 try: 119 return files[name] 120 except KeyError: 121 if (name + '-delete') in data: 122 return False 123 return None 124 125 98 126 class ForeignKeyRawIdWidget(forms.TextInput): 99 127 """ 100 128 A Widget for displaying ForeignKeys in the "raw_id" interface rather than