Code

Ticket #7028: improved-raw-id-admin-feedback-fixed-insertion.diff

File improved-raw-id-admin-feedback-fixed-insertion.diff, 3.4 KB (added by Marcob <marcoberi@…>, 6 years ago)

Fixed two minor problem with unicode representation and create id description placeholder also during insertion of a new record

Line 
1Index: contrib/admin/media/js/admin/RelatedObjectLookups.js
2===================================================================
3--- contrib/admin/media/js/admin/RelatedObjectLookups.js        (revision 7433)
4+++ contrib/admin/media/js/admin/RelatedObjectLookups.js        (working copy)
5@@ -26,14 +26,18 @@
6     return false;
7 }
8 
9-function dismissRelatedLookupPopup(win, chosenId) {
10+function dismissRelatedLookupPopup(win, chosenId, chosenName) {
11     var name = win.name.replace(/___/g, '.');
12     var elem = document.getElementById(name);
13+    var nameElem = document.getElementById("view_lookup_" + name);
14     if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
15         elem.value += ',' + chosenId;
16     } else {
17         document.getElementById(name).value = chosenId;
18     }
19+    if(nameElem) {
20+      nameElem.innerHTML = chosenName;
21+    }
22     win.close();
23 }
24 
25Index: contrib/admin/templatetags/admin_list.py
26===================================================================
27--- contrib/admin/templatetags/admin_list.py    (revision 7433)
28+++ contrib/admin/templatetags/admin_list.py    (working copy)
29@@ -196,8 +196,9 @@
30             # Convert the pk to something that can be used in Javascript.
31             # Problem cases are long ints (23L) and non-ASCII strings.
32             result_id = repr(force_unicode(getattr(result, pk)))[1:]
33+            result_name = repr(force_unicode(result))[1:]
34             yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \
35-                (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))
36+                (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s, %s); return false;"' % (result_id or '', result_name)), conditional_escape(result_repr), table_tag))
37         else:
38             yield mark_safe(u'<td%s>%s</td>' % (row_class, conditional_escape(result_repr)))
39 
40Index: contrib/admin/widgets.py
41===================================================================
42--- contrib/admin/widgets.py    (revision 7433)
43+++ contrib/admin/widgets.py    (working copy)
44@@ -101,13 +101,19 @@
45         output.append('<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
46             (related_url, url, name))
47         output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>' % settings.ADMIN_MEDIA_PREFIX)
48-        if value:
49-            output.append(self.label_for_value(value))
50+        # TODO: "id_" is hard-coded here. This should instead use the correct
51+        # API to determine the ID dynamically.
52+        output.append(self.label_for_value(value, "id_%s" % name))
53         return mark_safe(u''.join(output))
54     
55-    def label_for_value(self, value):
56-        return '&nbsp;<strong>%s</strong>' % \
57-            truncate_words(self.rel.to.objects.get(pk=value), 14)
58+    def label_for_value(self, value, name=''):
59+        # Placeholder used to display description during insertion
60+        if value:
61+            value = self.rel.to.objects.get(pk=value)
62+        else:
63+            value = ''
64+        return '&nbsp;<strong id="view_lookup_%s">%s</strong>' % \
65+            (name, truncate_words(value, 14))
66             
67 class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
68     """