Code

Ticket #1035: add_popup.diff

File add_popup.diff, 3.2 KB (added by Esaj <jason at jasondavies.com>, 8 years ago)
Line 
1Index: django/contrib/admin/templatetags/admin_modify.py
2===================================================================
3--- django/contrib/admin/templatetags/admin_modify.py   (revision 1587)
4+++ django/contrib/admin/templatetags/admin_modify.py   (working copy)
5@@ -246,6 +246,7 @@
6 
7     return {
8         'add': context['add'],
9+        'app_permission': context['app_permission'],
10         'change': context['change'],
11         'bound_fields': bound_fields,
12         'class_names': " ".join(class_names),
13@@ -257,3 +258,11 @@
14     return bound_manip.get_ordered_object_pk(ordered_obj)
15 
16 object_pk = register.simple_tag(object_pk)
17+
18+#@register.filter
19+def has_perm(perm_obj, perm):
20+    if perm_obj:
21+        return perm_obj[perm]
22+    return False
23+
24+has_perm = register.filter(has_perm)
25Index: django/contrib/admin/views/main.py
26===================================================================
27--- django/contrib/admin/views/main.py  (revision 1587)
28+++ django/contrib/admin/views/main.py  (working copy)
29@@ -286,6 +286,7 @@
30         self.is_date_time = isinstance(field, meta.DateTimeField)
31         self.is_file_field = isinstance(field, meta.FileField)
32         self.needs_add_label = field.rel and isinstance(field.rel, meta.ManyToOne) or isinstance(field.rel, meta.ManyToMany) and field.rel.to.admin
33+        self.add_permission = self.needs_add_label and "can_add_%s" % (field.rel.to.verbose_name)
34         self.hidden = isinstance(self.field, meta.AutoField)
35         self.first = False
36 
37@@ -375,11 +376,13 @@
38         return ""
39 
40 def render_change_form(opts, manipulator, app_label, context, add=False, change=False, show_delete=False, form_url=''):
41+    app_permission = context['perms'][app_label]
42     extra_context = {
43         'add': add,
44         'change': change,
45         'bound_manipulator': AdminBoundManipulator(opts, manipulator, context['form']),
46-        'has_delete_permission': context['perms'][app_label][opts.get_delete_permission()],
47+        'has_delete_permission': app_permission[opts.get_delete_permission()],
48+        'app_permission': app_permission,
49         'form_url': form_url,
50         'app_label': app_label,
51     }
52Index: django/contrib/admin/templates/widget/foreign.html
53===================================================================
54--- django/contrib/admin/templates/widget/foreign.html  (revision 1587)
55+++ django/contrib/admin/templates/widget/foreign.html  (working copy)
56@@ -4,5 +4,6 @@
57     <a href="../../../{{ bound_field.field.rel.to.app_label }}/{{ bound_field.field.rel.to.module_name }}/" class="related-lookup" id="lookup_{{ bound_field.element_id }}" onclick="return showRelatedObjectLookupPopup(this);"> <img src="{% admin_media_prefix %}img/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>
58 {% else %}
59 {% if bound_field.needs_add_label %}
60+{% if app_permission|has_perm:bound_field.add_permission %}
61     <a href="../../../{{ bound_field.field.rel.to.app_label }}/{{ bound_field.field.rel.to.module_name }}/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>
62-{% endif %}{% endif %}
63+{% endif %}{% endif %}{% endif %}