Ticket #22: file_del.patch

File file_del.patch, 4.8 KB (added by jlabath@…, 19 years ago)

Patch for admin app to allow deleting of files.

  • contrib/admin/urls/admin.py

     
    5353    ('^(?P<app_label>[^/]+)/(?P<module_name>[^/]+)/add/$', 'django.contrib.admin.views.main.add_stage'),
    5454    ('^(?P<app_label>[^/]+)/(?P<module_name>[^/]+)/(?P<object_id>.+)/history/$', 'django.contrib.admin.views.main.history'),
    5555    ('^(?P<app_label>[^/]+)/(?P<module_name>[^/]+)/(?P<object_id>.+)/delete/$', 'django.contrib.admin.views.main.delete_stage'),
     56    ('^(?P<app_label>[^/]+)/(?P<module_name>[^/]+)/(?P<object_id>.+)/delete_file/(?P<attname>.+)/$', 'django.contrib.admin.views.main.delete_file'),
    5657    ('^(?P<app_label>[^/]+)/(?P<module_name>[^/]+)/(?P<object_id>.+)/$', 'django.contrib.admin.views.main.change_stage'),
    5758)
    5859urlpatterns = patterns('', *urlpatterns)
  • contrib/admin/templatetags/admin_modify.py

     
    243243def object_pk(bound_manip, ordered_obj):
    244244    return bound_manip.get_ordered_object_pk(ordered_obj)
    245245object_pk = register.simple_tag(object_pk)
     246
     247def admin_file_delete_link(bound_field):
     248    s = ''
     249    container_obj = bound_field.original
     250    if container_obj:
     251        inf = container_obj._meta
     252        pk = getattr(container_obj,inf.pk.name)
     253        s = "../../../%s/%s/%s/delete_file/%s/" % \
     254            (inf.app_label,inf.module_name,pk,bound_field.field.attname)
     255    return s
     256admin_file_delete_link = register.simple_tag(admin_file_delete_link)
  • contrib/admin/views/main.py

     
    670670        'object': obj,
    671671    }, context_instance=Context(request))
    672672history = staff_member_required(history)
     673
     674def delete_file(request, app_label, module_name, object_id,attname):
     675    try:
     676        mod = meta.get_module(app_label, module_name)
     677    except ImportError:
     678        raise Http404
     679    opts = mod.Klass._meta
     680    if not request.user.has_perm(app_label + '.' + opts.get_delete_permission()):
     681        raise PermissionDenied
     682    obj = get_object_or_404(mod, pk=object_id)
     683    try:
     684        delete_f = getattr(obj, 'delete_%s_file' % attname)
     685        delete_f()
     686    except AttributeError: pass
     687    return HttpResponseRedirect(request.META['HTTP_REFERER'])
     688delete_file = staff_member_required(delete_file)
  • contrib/admin/templates/widget/file.html

     
    11{% load admin_modify i18n %}{% if bound_field.original_value %}
    2 {% trans "Currently:" %} <a href="{{ bound_field.original_url }}" > {{ bound_field.original_value }} </a><br />
     2{% trans "Currently:" %} <a href="{{ bound_field.original_url }}" > {{ bound_field.original_value }} </a>
     3<br />
     4<a href="{% admin_file_delete_link bound_field %}">{% trans "Delete this file." %}</a>
     5<br />
    36{% trans "Change:" %}{% output_all bound_field.form_fields %}
    47{% else %} {% output_all bound_field.form_fields %} {% endif %}
  • core/meta/__init__.py

     
    828828                func = curry(method_save_file, f)
    829829                func.alters_data = True
    830830                setattr(new_class, 'save_%s_file' % f.name, func)
     831                func = curry(method_delete_file, f)
     832                func.alters_data = True
     833                setattr(new_class, 'delete_%s_file' % f.name, func)
    831834                if isinstance(f, ImageField):
    832835                    # Add get_BLAH_width and get_BLAH_height methods, but only
    833836                    # if the image field doesn't have width and height cache
     
    13101313    # Save the object, because it has changed.
    13111314    self.save()
    13121315
     1316def method_delete_file(field, self):
     1317    filename = getattr(self, 'get_%s_filename' % field.name)()
     1318    #start of physical file removal logic
     1319    #you might want to comment out this block if you are accustomed
     1320    #the share one physical file/image among many objects
     1321    old_att_value = getattr(self, field.attname)
     1322    if old_att_value:
     1323        if os.path.exists(filename):
     1324            try: os.unlink(filename)
     1325            except: pass
     1326    #end of physical file removal logic
     1327    setattr(self, field.attname, '')
     1328    # Save the object, because it has changed.
     1329    self.save()
     1330
    13131331# IMAGE FIELD METHODS ######################
    13141332
    13151333def method_get_image_width(field, self):
Back to Top