Login

merge action in django admin

Author:
yeago
Posted:
September 29, 2010
Language:
Python
Version:
1.1
Score:
3 (after 3 ratings)

This is a merge function for django admin

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def merge_selected(modeladmin,request,queryset): #This is an admin/
    import copy
    model = queryset.model
    model_name = model._meta.object_name
    return_url = "."
    list_display = copy.deepcopy(modeladmin.list_display)
    ids = []

    if '_selected_action' in request.POST: #List of PK's of the selected models
        ids = request.POST.getlist('_selected_action')

    if 'id' in request.GET: #This is passed in for specific merge links. This id comes from the linking model (Consumer, IR, Contact, ...)
        id = request.GET.get('id')
        ids.append(id)
        try:
            queryset = queryset | model.objects.filter(pk=id)
        except AssertionError:
            queryset = model.objects.filter(pk__in=ids)
        return_url = model.objects.get(pk=id).get_absolute_url() or "."

    if 'return_url' in request.POST:
        return_url = request.POST['return_url']

    if 'master' in request.POST:
        master = model.objects.get(id=request.POST['master'])
        queryset = model.objects.filter(pk__in=ids)
        for q in queryset.exclude(pk=master.pk):
            model_merge(master,q)
        messages.success(request,"All " + model_name + " records have been merged into the selected " + model_name + ".")
        return HttpResponseRedirect(return_url)

    #Build the display_table... This is just for the template.
    #----------------------------------------
    display_table = []
    try: list_display.remove('action_checkbox')
    except ValueError: pass

    titles = []
    for ld in list_display:
        if hasattr(ld,'short_description'):
            titles.append(strings.pretty(ld.short_description))
        elif hasattr(ld,'func_name'):
            titles.append(strings.pretty(ld.func_name))
        elif ld == "__str__":
            titles.append(model_name)
        else:
            titles.append(ld)
    display_table.append(titles)

    for q in queryset:
        row = []
        for ld in list_display:
            if callable(ld):
                row.append(mark_safe(ld(q)))
            elif ld == "__str__":
                row.append(q)
            else:
                row.append(mark_safe(getattr(q,ld)))
        display_table.append(row)
        display_table[-1:][0].insert(0,q.pk)
    #----------------------------------------

    return render_to_response('merge_preview.html',{'queryset': queryset, 'model': model, 'return_url':return_url,\
            'display_table':display_table, 'ids': ids}, context_instance=RequestContext(request))

merge_selected.short_description = "Merge selected records"

#==========html===============

{% extends "section_base.html" %}
{% load form_tags %}
{% block title %}Confirm Merger{% endblock %}
{% block breadcrumbs %}{{ block.super }} Merge Preview{% endblock %}
{% block body %}
<div class="span-24">
<h2>Important!</h2>
<p>Please select which record you would like to keep. The selected record's information will be used,
and all related information from the other records will be moved to it. The other records will be deleted.
If you are unsure how to use this, please contact your supervisor or COMS Support. Changes made here cannot
be reversed.</p>
<hr />
<form method="POST" action=".">{% csrf_token %}
<table class="admin small no_actions">
    <thead>
        <tr>
            <th>&nbsp;</th>
            {% for row in display_table.0 %}
            <th>{{ row }}</th>
            {% endfor %}
        </tr>
    </thead>
    <tbody>
        {% for row in display_table %}
        {% if forloop.counter0 != 0 %}
        <tr class="{% cycle '' 'clarify' %}">
            {% for cell in row %}
            {% if forloop.first %}
            <td><input type="radio" name="master" value="{{ cell }}"></td>
            {% else %}
            <td>{{ cell }}</td>
            {% endif %}
            {% endfor %}
        </tr>
        {% endif %}
        {% endfor %}
    </tbody>
</table>
<br>

{# Request variables from Django Admin's handling of actions. #}
{% for id in ids %}
<input type="hidden" name="_selected_action" value="{{ id }}">
{% endfor %}
<input type="hidden" name="action" value="merge_selected">
<input type="hidden" name="return_url" value="{{ return_url }}">

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Archatas (on September 30, 2010):

It's totally unclear what that model merging has to do. Is it archiving of data to another model? Where is model_merge defined?

#

Please login first before commenting.