Code

Ticket #2569: adminforeign.3.diff

File adminforeign.3.diff, 5.1 KB (added by Joel Heenan <joelh-django@…>, 8 years ago)

Improvements

Line 
1Index: django/db/models/fields/related.py
2===================================================================
3--- django/db/models/fields/related.py  (revision 3657)
4+++ django/db/models/fields/related.py  (working copy)
5@@ -566,6 +566,11 @@
6 
7         self.db_index = True
8 
9+    def get_related_pk(self):
10+        """ Returning the primary key of the object we are related to
11+        """
12+        return self.get_choices()[1][0]
13+
14     def get_attname(self):
15         return '%s_id' % self.name
16 
17Index: django/contrib/admin/media/js/admin/RelatedObjectLookups.js
18===================================================================
19--- django/contrib/admin/media/js/admin/RelatedObjectLookups.js (revision 3657)
20+++ django/contrib/admin/media/js/admin/RelatedObjectLookups.js (working copy)
21@@ -55,3 +55,30 @@
22     }
23     win.close();
24 }
25+
26+function showEditPopup(triggeringLink) {
27+    var name = triggeringLink.id.replace(/^edit_/,'');
28+    var elem = document.getElementById(name);
29+    if (elem) {
30+        if (elem.nodeName == 'SELECT') {
31+            var id = elem.options[elem.selectedIndex].value
32+            name = name.replace(/\./g,'___');
33+            var win = window.open(triggeringLink.href + id + '/?_popup=1',name, 'height=500,width=800,resizable=yes,scrollbars=yes');
34+            return false;
35+        }
36+    }
37+    return false;
38+}
39+
40+function dismissEditPopup(win, newId, newRepr) {
41+    var name = win.name.replace(/___/g, '.');
42+    var elem = document.getElementById(name);
43+    if(elem) {
44+        if( elem.nodeName == 'SELECT') {
45+            var o = new Option(newRepr, newId);
46+            elem.options[elem.selectedIndex] = o;
47+            o.selected = true;
48+        }
49+    }
50+    win.close();
51+}
52Index: django/contrib/admin/views/main.py
53===================================================================
54--- django/contrib/admin/views/main.py  (revision 3657)
55+++ django/contrib/admin/views/main.py  (working copy)
56@@ -130,6 +130,10 @@
57 
58         if field.rel:
59             self.related_url = '../../../%s/%s/' % (field.rel.to._meta.app_label, field.rel.to._meta.object_name.lower())
60+            try:
61+                self.rel_to_pk = field.get_related_pk() # for one-to-one fields
62+            except:
63+                pass
64 
65     def original_value(self):
66         if self.original:
67@@ -343,6 +347,10 @@
68             LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, str(new_object), CHANGE, change_message)
69 
70             msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object}
71+            if request.REQUEST.has_key("_popup"):
72+                http_response = '<script type="text/javascript">opener.dismissEditPopup(window, %s, "%s");</script>' % \
73+                    (pk_value, str(new_object).replace('"', '\\"'))
74+                return HttpResponse(http_response)
75             if request.POST.has_key("_continue"):
76                 request.user.message_set.create(message=msg + ' ' + _("You may edit it again below."))
77                 if request.REQUEST.has_key('_popup'):
78Index: django/contrib/admin/templates/widget/one_to_one.html
79===================================================================
80--- django/contrib/admin/templates/widget/one_to_one.html       (revision 3657)
81+++ django/contrib/admin/templates/widget/one_to_one.html       (working copy)
82@@ -1,2 +1,4 @@
83+{% load admin_modify adminmedia %}
84 {% if add %}{% include "widget/foreign.html" %}{% endif %}
85-{% if change %}{% if bound_field.existing_display %}&nbsp;<strong>{{ bound_field.existing_display|truncatewords:"14"|escape }}</strong>{% endif %}{% endif %}
86+{% if change %}
87+{% if bound_field.rel_to_pk %}&nbsp;<a href="{{ bound_field.related_url }}{{ bound_field.rel_to_pk}}"><strong>{{ bound_field.existing_display|truncatewords:"14"|escape }}</strong></a>{% endif %}{% endif %}
88Index: django/contrib/admin/templates/widget/foreign.html
89===================================================================
90--- django/contrib/admin/templates/widget/foreign.html  (revision 3657)
91+++ django/contrib/admin/templates/widget/foreign.html  (working copy)
92@@ -8,8 +8,11 @@
93     {% endif %}
94 {% else %}
95 {% if bound_field.needs_add_label %}
96-    <a href="{{ bound_field.related_url }}add/" class="add-another" id="add_{{ bound_field.element_id }}" onclick="return showAddAnotherPopup(this);"> <img src="{% admin_media_prefix %}img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>
97-{% endif %}{% endif %}
98+    <a href="{{ bound_field.related_url }}add/" class="add-another" id="add_{{ bound_field.element_id }}" onclick="return showAddAnotherPopup(this);"> <img src="{% admin_media_prefix %}img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>
99+    <a href="{{ bound_field.related_url }}" class="related-lookup" id="edit_{{ bound_field.element_id }}" onclick="return showEditPopup(this);"> <img src="{% admin_media_prefix %}img/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>
100+
101+{% endif %}
102+{% endif %}
103 {% if change %}
104     {% if bound_field.field.primary_key %}
105         {{ bound_field.original_value }}