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