PageRenderTime 152ms CodeModel.GetById 80ms app.highlight 6ms RepoModel.GetById 65ms app.codeStats 0ms

/django/contrib/admin/templatetags/admin_modify.py

https://code.google.com/p/mango-py/
Python | 56 lines | 46 code | 3 blank | 7 comment | 13 complexity | d2c87f1db20e88568748716a0856b2f9 MD5 | raw file
 1from django import template
 2
 3register = template.Library()
 4
 5def prepopulated_fields_js(context):
 6    """
 7    Creates a list of prepopulated_fields that should render Javascript for
 8    the prepopulated fields for both the admin form and inlines.
 9    """
10    prepopulated_fields = []
11    if context['add'] and 'adminform' in context:
12        prepopulated_fields.extend(context['adminform'].prepopulated_fields)
13    if 'inline_admin_formsets' in context:
14        for inline_admin_formset in context['inline_admin_formsets']:
15            for inline_admin_form in inline_admin_formset:
16                if inline_admin_form.original is None:
17                    prepopulated_fields.extend(inline_admin_form.prepopulated_fields)
18    context.update({'prepopulated_fields': prepopulated_fields})
19    return context
20prepopulated_fields_js = register.inclusion_tag('admin/prepopulated_fields_js.html', takes_context=True)(prepopulated_fields_js)
21
22def submit_row(context):
23    """
24    Displays the row of buttons for delete and save.
25    """
26    opts = context['opts']
27    change = context['change']
28    is_popup = context['is_popup']
29    save_as = context['save_as']
30    return {
31        'onclick_attrib': (opts.get_ordered_objects() and change
32                            and 'onclick="submitOrderForm();"' or ''),
33        'show_delete_link': (not is_popup and context['has_delete_permission']
34                              and (change or context['show_delete'])),
35        'show_save_as_new': not is_popup and change and save_as,
36        'show_save_and_add_another': context['has_add_permission'] and
37                            not is_popup and (not save_as or context['add']),
38        'show_save_and_continue': not is_popup and context['has_change_permission'],
39        'is_popup': is_popup,
40        'show_save': True
41    }
42submit_row = register.inclusion_tag('admin/submit_line.html', takes_context=True)(submit_row)
43
44def cell_count(inline_admin_form):
45    """Returns the number of cells used in a tabular inline"""
46    count = 1 # Hidden cell with hidden 'id' field
47    for fieldset in inline_admin_form:
48        # Loop through all the fields (one per cell)
49        for line in fieldset:
50            for field in line:
51                count += 1
52    if inline_admin_form.formset.can_delete:
53        # Delete checkbox
54        count += 1
55    return count
56cell_count = register.filter(cell_count)