Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin_modify.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import json
  2. from django import template
  3. from django.template.context import Context
  4. from .base import InclusionAdminNode
  5. register = template.Library()
  6. def prepopulated_fields_js(context):
  7. """
  8. Create a list of prepopulated_fields that should render Javascript for
  9. the prepopulated fields for both the admin form and inlines.
  10. """
  11. prepopulated_fields = []
  12. if 'adminform' in context:
  13. prepopulated_fields.extend(context['adminform'].prepopulated_fields)
  14. if 'inline_admin_formsets' in context:
  15. for inline_admin_formset in context['inline_admin_formsets']:
  16. for inline_admin_form in inline_admin_formset:
  17. if inline_admin_form.original is None:
  18. prepopulated_fields.extend(inline_admin_form.prepopulated_fields)
  19. prepopulated_fields_json = []
  20. for field in prepopulated_fields:
  21. prepopulated_fields_json.append({
  22. "id": "#%s" % field["field"].auto_id,
  23. "name": field["field"].name,
  24. "dependency_ids": ["#%s" % dependency.auto_id for dependency in field["dependencies"]],
  25. "dependency_list": [dependency.name for dependency in field["dependencies"]],
  26. "maxLength": field["field"].field.max_length or 50,
  27. "allowUnicode": getattr(field["field"].field, "allow_unicode", False)
  28. })
  29. context.update({
  30. 'prepopulated_fields': prepopulated_fields,
  31. 'prepopulated_fields_json': json.dumps(prepopulated_fields_json),
  32. })
  33. return context
  34. @register.tag(name='prepopulated_fields_js')
  35. def prepopulated_fields_js_tag(parser, token):
  36. return InclusionAdminNode(parser, token, func=prepopulated_fields_js, template_name="prepopulated_fields_js.html")
  37. def submit_row(context):
  38. """
  39. Display the row of buttons for delete and save.
  40. """
  41. add = context['add']
  42. change = context['change']
  43. is_popup = context['is_popup']
  44. save_as = context['save_as']
  45. show_save = context.get('show_save', True)
  46. show_save_and_continue = context.get('show_save_and_continue', True)
  47. has_add_permission = context['has_add_permission']
  48. has_change_permission = context['has_change_permission']
  49. has_view_permission = context['has_view_permission']
  50. has_editable_inline_admin_formsets = context['has_editable_inline_admin_formsets']
  51. can_save = (has_change_permission and change) or (has_add_permission and add) or has_editable_inline_admin_formsets
  52. can_save_and_continue = not is_popup and can_save and has_view_permission and show_save_and_continue
  53. can_change = has_change_permission or has_editable_inline_admin_formsets
  54. ctx = Context(context)
  55. ctx.update({
  56. 'can_change': can_change,
  57. 'show_delete_link': (
  58. not is_popup and context['has_delete_permission'] and
  59. change and context.get('show_delete', True)
  60. ),
  61. 'show_save_as_new': not is_popup and has_change_permission and change and save_as,
  62. 'show_save_and_add_another': (
  63. has_add_permission and not is_popup and
  64. (not save_as or add) and can_save
  65. ),
  66. 'show_save_and_continue': can_save_and_continue,
  67. 'show_save': show_save and can_save,
  68. 'show_close': not(show_save and can_save)
  69. })
  70. return ctx
  71. @register.tag(name='submit_row')
  72. def submit_row_tag(parser, token):
  73. return InclusionAdminNode(parser, token, func=submit_row, template_name='submit_line.html')
  74. @register.tag(name='change_form_object_tools')
  75. def change_form_object_tools_tag(parser, token):
  76. """Display the row of change form object tools."""
  77. return InclusionAdminNode(
  78. parser, token,
  79. func=lambda context: context,
  80. template_name='change_form_object_tools.html',
  81. )
  82. @register.filter
  83. def cell_count(inline_admin_form):
  84. """Return the number of cells used in a tabular inline."""
  85. count = 1 # Hidden cell with hidden 'id' field
  86. for fieldset in inline_admin_form:
  87. # Loop through all the fields (one per cell)
  88. for line in fieldset:
  89. for field in line:
  90. count += 1
  91. if inline_admin_form.formset.can_delete:
  92. # Delete checkbox
  93. count += 1
  94. return count