Ticket #3380: patch_django.diff

File patch_django.diff, 5.6 KB (added by Charlax <ca.dein@…>, 8 years ago)

Patch for adding restore and backup support in the admin interface

  • django/contrib/admin/urls.py

     
    3535    # Add/change/delete/history
    3636    ('^([^/]+)/([^/]+)/$', 'django.contrib.admin.views.main.change_list'),
    3737    ('^([^/]+)/([^/]+)/add/$', 'django.contrib.admin.views.main.add_stage'),
     38    ('^([^/]+)/([^/]+)/backup/$', 'django.contrib.admin.views.main.backup'),
     39    ('^([^/]+)/([^/]+)/restore/$', 'django.contrib.admin.views.main.restore'),
    3840    ('^([^/]+)/([^/]+)/(.+)/history/$', 'django.contrib.admin.views.main.history'),
    3941    ('^([^/]+)/([^/]+)/(.+)/delete/$', 'django.contrib.admin.views.main.delete_stage'),
    4042    ('^([^/]+)/([^/]+)/(.+)/$', 'django.contrib.admin.views.main.change_stage'),
  • django/contrib/admin/views/main.py

     
    1313from django.utils.html import escape
    1414from django.utils.text import capfirst, get_text_list
    1515import operator
     16import cPickle
     17from time import strftime, gmtime
    1618
    1719from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
    1820if not LogEntry._meta.installed:
     
    767769                               'admin/%s/change_list.html' % app_label,
    768770                               'admin/change_list.html'], context_instance=c)
    769771change_list = staff_member_required(never_cache(change_list))
     772
     773def backup(request, app_label, model_name):
     774    model = models.get_model(app_label, model_name)
     775    if model is None:
     776        raise Http404("App %r, model %r, not found" % (app_label, model_name))
     777    opts = model._meta
     778
     779    if not request.user.has_perm(app_label + '.' + opts.get_add_permission()):
     780        raise PermissionDenied
     781
     782    objects = model.objects.all()
     783    l=[]
     784    for o in objects:
     785        l.append(o)
     786    response = HttpResponse(mimetype="text/plain")
     787    response['Content-Disposition'] = 'attachment; filename=backup_%s__%s.txt' % (model_name, strftime('%c').replace(" ", "_"))
     788    cPickle.dump(l, response)
     789    return response
     790   
     791def restore(request, app_label, model_name):
     792    model = models.get_model(app_label, model_name)
     793    if model is None:
     794        raise Http404("App %r, model %r, not found" % (app_label, model_name))
     795    opts = model._meta
     796
     797    if not request.user.has_perm(app_label + '.' + opts.get_add_permission()):
     798        raise PermissionDenied
     799
     800    c={"unloaded":True}
     801    if request.FILES.has_key("restore_file"):
     802        try:
     803            objects=cPickle.loads(request.FILES["restore_file"]["content"])
     804        except:
     805            c={'unloaded':True, 'file_invalid':True}
     806        else:
     807            c={"objects_updated":[], "loaded":True}
     808            for o in objects:
     809                o.save()
     810                c["objects_updated"].append(str(o))
     811    return render_to_response(["admin/%s/%s/restore.html" % (app_label, model._meta.object_name.lower()),
     812                               "admin/%s/restore.html" % app_label ,
     813                               "admin/restore.html"], c)
     814 No newline at end of file
  • django/contrib/admin/templates/admin/change_list.html

     
    77{% block coltype %}flex{% endblock %}
    88{% block content %}
    99<div id="content-main">
     10        <p><a href="backup/">{% trans 'Backup' %}</a> | <a href="restore/">{% trans 'Restore' %}</a></p>
    1011{% block object-tools %}
    1112{% if has_add_permission %}
    1213<ul class="object-tools"><li><a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">{% blocktrans with cl.opts.verbose_name|escape as name %}Add {{ name }}{% endblocktrans %}</a></li></ul>
  • django/contrib/admin/templates/admin/restore.html

     
     1{% extends "admin/base_site.html" %}
     2{% load i18n %}
     3{% block userlinks %}<a href="../../../../doc/">{% trans 'Documentation' %}</a> / <a href="../../../../password_change/">{% trans 'Change password' %}</a> / <a href="../../../../logout/">{% trans 'Log out' %}</a>{% endblock %}
     4
     5{% block content %}
     6{% if perms_lacking %}
     7    <p>{% blocktrans with object|escape as escaped_object %}Restoring the {{ object_name }} '{{ escaped_object }}' would result in updating related objects, but your account doesn't have permission to update the following types of objects:{% endblocktrans %}</p>
     8    <ul>
     9    {% for obj in perms_lacking %}
     10        <li>{{ obj|escape }}</li>
     11    {% endfor %}
     12    </ul>
     13{% endif %}
     14{% if unloaded %}
     15        {% if file_invalid %}
     16                <p>{% blocktrans %}Invalid file !{% endblocktrans %}</p>
     17
     18        {% else %}
     19            <p>{% blocktrans %}Caution ! You will override all the existing objects in the database !{% endblocktrans %}</p>
     20            <form action="" method="post" enctype="multipart/form-data">
     21                <div>
     22                        <input type="file" name="restore_file">
     23                </div>
     24            <div>
     25            <input type="submit" value="{% trans "Submit" %}" />
     26            </div>
     27            </form>
     28        {% endif %}
     29
     30{% else %}
     31        <p><p>{% blocktrans with object|escape as escaped_object %}Congratulation ! The following objects were updated :{% endblocktrans %}</p>
     32    <ul>
     33    {% for obj in objects_updated %}
     34        <li>{{ obj|escape }}</li>
     35    {% endfor %}
     36        </ul>
     37{% endif %}
     38{% endblock %}
Back to Top