Ticket #4979: 4979-admin-file-deletion.diff

File 4979-admin-file-deletion.diff, 3.9 KB (added by Daniel Pope <dan@…>, 16 years ago)

Patch for admin filefield deletion

  • django/db/models/fields/files.py

     
    188188        return os.path.join(self.get_directory_name(), self.get_filename(filename))
    189189
    190190    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):
    192194            getattr(instance, self.name).save(data.name, data, save=False)
    193195
    194196    def formfield(self, **kwargs):
  • django/forms/fields.py

     
    453453
    454454    def clean(self, data, initial=None):
    455455        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
    458461        elif not data and initial:
    459462            return initial
    460463
     
    483486        PNG, possibly others -- whatever the Python Imaging Library supports).
    484487        """
    485488        f = super(ImageField, self).clean(data, initial)
    486         if f is None:
    487             return None
     489        if not f:
     490            return f
    488491        elif not data and initial:
    489492            return initial
    490493        from PIL import Image
  • django/contrib/admin/options.py

     
    105105            kwargs['widget'] = widgets.AdminTextInputWidget
    106106            return db_field.formfield(**kwargs)
    107107   
    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
    109109        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
    111114            return db_field.formfield(**kwargs)
    112115
    113116        # For ForeignKey or ManyToManyFields, use a special widget.
  • django/contrib/admin/widgets.py

     
    9595        output.append(super(AdminFileWidget, self).render(name, value, attrs))
    9696        return mark_safe(u''.join(output))
    9797
     98
     99class 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('&nbsp;')
     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
    98126class ForeignKeyRawIdWidget(forms.TextInput):
    99127    """
    100128    A Widget for displaying ForeignKeys in the "raw_id" interface rather than
Back to Top