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_list.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import datetime
  2. from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
  3. from django.contrib.admin.utils import (
  4. display_for_field, display_for_value, label_for_field, lookup_field,
  5. )
  6. from django.contrib.admin.views.main import (
  7. ALL_VAR, ORDER_VAR, PAGE_VAR, SEARCH_VAR,
  8. )
  9. from django.core.exceptions import ObjectDoesNotExist
  10. from django.db import models
  11. from django.template import Library
  12. from django.template.loader import get_template
  13. from django.templatetags.static import static
  14. from django.urls import NoReverseMatch
  15. from django.utils import formats
  16. from django.utils.html import format_html
  17. from django.utils.safestring import mark_safe
  18. from django.utils.text import capfirst
  19. from django.utils.translation import gettext as _
  20. from .base import InclusionAdminNode
  21. register = Library()
  22. DOT = '.'
  23. @register.simple_tag
  24. def paginator_number(cl, i):
  25. """
  26. Generate an individual page index link in a paginated list.
  27. """
  28. if i == DOT:
  29. return '... '
  30. elif i == cl.page_num:
  31. return format_html('<span class="this-page">{}</span> ', i + 1)
  32. else:
  33. return format_html('<a href="{}"{}>{}</a> ',
  34. cl.get_query_string({PAGE_VAR: i}),
  35. mark_safe(' class="end"' if i == cl.paginator.num_pages - 1 else ''),
  36. i + 1)
  37. def pagination(cl):
  38. """
  39. Generate the series of links to the pages in a paginated list.
  40. """
  41. paginator, page_num = cl.paginator, cl.page_num
  42. pagination_required = (not cl.show_all or not cl.can_show_all) and cl.multi_page
  43. if not pagination_required:
  44. page_range = []
  45. else:
  46. ON_EACH_SIDE = 3
  47. ON_ENDS = 2
  48. # If there are 10 or fewer pages, display links to every page.
  49. # Otherwise, do some fancy
  50. if paginator.num_pages <= 10:
  51. page_range = range(paginator.num_pages)
  52. else:
  53. # Insert "smart" pagination links, so that there are always ON_ENDS
  54. # links at either end of the list of pages, and there are always
  55. # ON_EACH_SIDE links at either end of the "current page" link.
  56. page_range = []
  57. if page_num > (ON_EACH_SIDE + ON_ENDS):
  58. page_range += [
  59. *range(0, ON_ENDS), DOT,
  60. *range(page_num - ON_EACH_SIDE, page_num + 1),
  61. ]
  62. else:
  63. page_range.extend(range(0, page_num + 1))
  64. if page_num < (paginator.num_pages - ON_EACH_SIDE - ON_ENDS - 1):
  65. page_range += [
  66. *range(page_num + 1, page_num + ON_EACH_SIDE + 1), DOT,
  67. *range(paginator.num_pages - ON_ENDS, paginator.num_pages)
  68. ]
  69. else:
  70. page_range.extend(range(page_num + 1, paginator.num_pages))
  71. need_show_all_link = cl.can_show_all and not cl.show_all and cl.multi_page
  72. return {
  73. 'cl': cl,
  74. 'pagination_required': pagination_required,
  75. 'show_all_url': need_show_all_link and cl.get_query_string({ALL_VAR: ''}),
  76. 'page_range': page_range,
  77. 'ALL_VAR': ALL_VAR,
  78. '1': 1,
  79. }
  80. @register.tag(name='pagination')
  81. def pagination_tag(parser, token):
  82. return InclusionAdminNode(
  83. parser, token,
  84. func=pagination,
  85. template_name='pagination.html',
  86. takes_context=False,
  87. )
  88. def result_headers(cl):
  89. """
  90. Generate the list column headers.
  91. """
  92. ordering_field_columns = cl.get_ordering_field_columns()
  93. for i, field_name in enumerate(cl.list_display):
  94. text, attr = label_for_field(
  95. field_name, cl.model,
  96. model_admin=cl.model_admin,
  97. return_attr=True
  98. )
  99. is_field_sortable = cl.sortable_by is None or field_name in cl.sortable_by
  100. if attr:
  101. field_name = _coerce_field_name(field_name, i)
  102. # Potentially not sortable
  103. # if the field is the action checkbox: no sorting and special class
  104. if field_name == 'action_checkbox':
  105. yield {
  106. "text": text,
  107. "class_attrib": mark_safe(' class="action-checkbox-column"'),
  108. "sortable": False,
  109. }
  110. continue
  111. admin_order_field = getattr(attr, "admin_order_field", None)
  112. if not admin_order_field:
  113. is_field_sortable = False
  114. if not is_field_sortable:
  115. # Not sortable
  116. yield {
  117. 'text': text,
  118. 'class_attrib': format_html(' class="column-{}"', field_name),
  119. 'sortable': False,
  120. }
  121. continue
  122. # OK, it is sortable if we got this far
  123. th_classes = ['sortable', 'column-{}'.format(field_name)]
  124. order_type = ''
  125. new_order_type = 'asc'
  126. sort_priority = 0
  127. # Is it currently being sorted on?
  128. is_sorted = i in ordering_field_columns
  129. if is_sorted:
  130. order_type = ordering_field_columns.get(i).lower()
  131. sort_priority = list(ordering_field_columns).index(i) + 1
  132. th_classes.append('sorted %sending' % order_type)
  133. new_order_type = {'asc': 'desc', 'desc': 'asc'}[order_type]
  134. # build new ordering param
  135. o_list_primary = [] # URL for making this field the primary sort
  136. o_list_remove = [] # URL for removing this field from sort
  137. o_list_toggle = [] # URL for toggling order type for this field
  138. def make_qs_param(t, n):
  139. return ('-' if t == 'desc' else '') + str(n)
  140. for j, ot in ordering_field_columns.items():
  141. if j == i: # Same column
  142. param = make_qs_param(new_order_type, j)
  143. # We want clicking on this header to bring the ordering to the
  144. # front
  145. o_list_primary.insert(0, param)
  146. o_list_toggle.append(param)
  147. # o_list_remove - omit
  148. else:
  149. param = make_qs_param(ot, j)
  150. o_list_primary.append(param)
  151. o_list_toggle.append(param)
  152. o_list_remove.append(param)
  153. if i not in ordering_field_columns:
  154. o_list_primary.insert(0, make_qs_param(new_order_type, i))
  155. yield {
  156. "text": text,
  157. "sortable": True,
  158. "sorted": is_sorted,
  159. "ascending": order_type == "asc",
  160. "sort_priority": sort_priority,
  161. "url_primary": cl.get_query_string({ORDER_VAR: '.'.join(o_list_primary)}),
  162. "url_remove": cl.get_query_string({ORDER_VAR: '.'.join(o_list_remove)}),
  163. "url_toggle": cl.get_query_string({ORDER_VAR: '.'.join(o_list_toggle)}),
  164. "class_attrib": format_html(' class="{}"', ' '.join(th_classes)) if th_classes else '',
  165. }
  166. def _boolean_icon(field_val):
  167. icon_url = static('admin/img/icon-%s.svg' %
  168. {True: 'yes', False: 'no', None: 'unknown'}[field_val])
  169. return format_html('<img src="{}" alt="{}">', icon_url, field_val)
  170. def _coerce_field_name(field_name, field_index):
  171. """
  172. Coerce a field_name (which may be a callable) to a string.
  173. """
  174. if callable(field_name):
  175. if field_name.__name__ == '<lambda>':
  176. return 'lambda' + str(field_index)
  177. else:
  178. return field_name.__name__
  179. return field_name
  180. def items_for_result(cl, result, form):
  181. """
  182. Generate the actual list of data.
  183. """
  184. def link_in_col(is_first, field_name, cl):
  185. if cl.list_display_links is None:
  186. return False
  187. if is_first and not cl.list_display_links:
  188. return True
  189. return field_name in cl.list_display_links
  190. first = True
  191. pk = cl.lookup_opts.pk.attname
  192. for field_index, field_name in enumerate(cl.list_display):
  193. empty_value_display = cl.model_admin.get_empty_value_display()
  194. row_classes = ['field-%s' % _coerce_field_name(field_name, field_index)]
  195. try:
  196. f, attr, value = lookup_field(field_name, result, cl.model_admin)
  197. except ObjectDoesNotExist:
  198. result_repr = empty_value_display
  199. else:
  200. empty_value_display = getattr(attr, 'empty_value_display', empty_value_display)
  201. if f is None or f.auto_created:
  202. if field_name == 'action_checkbox':
  203. row_classes = ['action-checkbox']
  204. boolean = getattr(attr, 'boolean', False)
  205. result_repr = display_for_value(value, empty_value_display, boolean)
  206. if isinstance(value, (datetime.date, datetime.time)):
  207. row_classes.append('nowrap')
  208. else:
  209. if isinstance(f.remote_field, models.ManyToOneRel):
  210. field_val = getattr(result, f.name)
  211. if field_val is None:
  212. result_repr = empty_value_display
  213. else:
  214. result_repr = field_val
  215. else:
  216. result_repr = display_for_field(value, f, empty_value_display)
  217. if isinstance(f, (models.DateField, models.TimeField, models.ForeignKey)):
  218. row_classes.append('nowrap')
  219. if str(result_repr) == '':
  220. result_repr = mark_safe('&nbsp;')
  221. row_class = mark_safe(' class="%s"' % ' '.join(row_classes))
  222. # If list_display_links not defined, add the link tag to the first field
  223. if link_in_col(first, field_name, cl):
  224. table_tag = 'th' if first else 'td'
  225. first = False
  226. # Display link to the result's change_view if the url exists, else
  227. # display just the result's representation.
  228. try:
  229. url = cl.url_for_result(result)
  230. except NoReverseMatch:
  231. link_or_text = result_repr
  232. else:
  233. url = add_preserved_filters({'preserved_filters': cl.preserved_filters, 'opts': cl.opts}, url)
  234. # Convert the pk to something that can be used in Javascript.
  235. # Problem cases are non-ASCII strings.
  236. if cl.to_field:
  237. attr = str(cl.to_field)
  238. else:
  239. attr = pk
  240. value = result.serializable_value(attr)
  241. link_or_text = format_html(
  242. '<a href="{}"{}>{}</a>',
  243. url,
  244. format_html(
  245. ' data-popup-opener="{}"', value
  246. ) if cl.is_popup else '',
  247. result_repr)
  248. yield format_html('<{}{}>{}</{}>',
  249. table_tag,
  250. row_class,
  251. link_or_text,
  252. table_tag)
  253. else:
  254. # By default the fields come from ModelAdmin.list_editable, but if we pull
  255. # the fields out of the form instead of list_editable custom admins
  256. # can provide fields on a per request basis
  257. if (form and field_name in form.fields and not (
  258. field_name == cl.model._meta.pk.name and
  259. form[cl.model._meta.pk.name].is_hidden)):
  260. bf = form[field_name]
  261. result_repr = mark_safe(str(bf.errors) + str(bf))
  262. yield format_html('<td{}>{}</td>', row_class, result_repr)
  263. if form and not form[cl.model._meta.pk.name].is_hidden:
  264. yield format_html('<td>{}</td>', form[cl.model._meta.pk.name])
  265. class ResultList(list):
  266. """
  267. Wrapper class used to return items in a list_editable changelist, annotated
  268. with the form object for error reporting purposes. Needed to maintain
  269. backwards compatibility with existing admin templates.
  270. """
  271. def __init__(self, form, *items):
  272. self.form = form
  273. super().__init__(*items)
  274. def results(cl):
  275. if cl.formset:
  276. for res, form in zip(cl.result_list, cl.formset.forms):
  277. yield ResultList(form, items_for_result(cl, res, form))
  278. else:
  279. for res in cl.result_list:
  280. yield ResultList(None, items_for_result(cl, res, None))
  281. def result_hidden_fields(cl):
  282. if cl.formset:
  283. for res, form in zip(cl.result_list, cl.formset.forms):
  284. if form[cl.model._meta.pk.name].is_hidden:
  285. yield mark_safe(form[cl.model._meta.pk.name])
  286. def result_list(cl):
  287. """
  288. Display the headers and data list together.
  289. """
  290. headers = list(result_headers(cl))
  291. num_sorted_fields = 0
  292. for h in headers:
  293. if h['sortable'] and h['sorted']:
  294. num_sorted_fields += 1
  295. return {'cl': cl,
  296. 'result_hidden_fields': list(result_hidden_fields(cl)),
  297. 'result_headers': headers,
  298. 'num_sorted_fields': num_sorted_fields,
  299. 'results': list(results(cl))}
  300. @register.tag(name='result_list')
  301. def result_list_tag(parser, token):
  302. return InclusionAdminNode(
  303. parser, token,
  304. func=result_list,
  305. template_name='change_list_results.html',
  306. takes_context=False,
  307. )
  308. def date_hierarchy(cl):
  309. """
  310. Display the date hierarchy for date drill-down functionality.
  311. """
  312. if cl.date_hierarchy:
  313. field_name = cl.date_hierarchy
  314. year_field = '%s__year' % field_name
  315. month_field = '%s__month' % field_name
  316. day_field = '%s__day' % field_name
  317. field_generic = '%s__' % field_name
  318. year_lookup = cl.params.get(year_field)
  319. month_lookup = cl.params.get(month_field)
  320. day_lookup = cl.params.get(day_field)
  321. def link(filters):
  322. return cl.get_query_string(filters, [field_generic])
  323. if not (year_lookup or month_lookup or day_lookup):
  324. # select appropriate start level
  325. date_range = cl.queryset.aggregate(first=models.Min(field_name),
  326. last=models.Max(field_name))
  327. if date_range['first'] and date_range['last']:
  328. if date_range['first'].year == date_range['last'].year:
  329. year_lookup = date_range['first'].year
  330. if date_range['first'].month == date_range['last'].month:
  331. month_lookup = date_range['first'].month
  332. if year_lookup and month_lookup and day_lookup:
  333. day = datetime.date(int(year_lookup), int(month_lookup), int(day_lookup))
  334. return {
  335. 'show': True,
  336. 'back': {
  337. 'link': link({year_field: year_lookup, month_field: month_lookup}),
  338. 'title': capfirst(formats.date_format(day, 'YEAR_MONTH_FORMAT'))
  339. },
  340. 'choices': [{'title': capfirst(formats.date_format(day, 'MONTH_DAY_FORMAT'))}]
  341. }
  342. elif year_lookup and month_lookup:
  343. days = getattr(cl.queryset, 'dates')(field_name, 'day')
  344. return {
  345. 'show': True,
  346. 'back': {
  347. 'link': link({year_field: year_lookup}),
  348. 'title': str(year_lookup)
  349. },
  350. 'choices': [{
  351. 'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
  352. 'title': capfirst(formats.date_format(day, 'MONTH_DAY_FORMAT'))
  353. } for day in days]
  354. }
  355. elif year_lookup:
  356. months = getattr(cl.queryset, 'dates')(field_name, 'month')
  357. return {
  358. 'show': True,
  359. 'back': {
  360. 'link': link({}),
  361. 'title': _('All dates')
  362. },
  363. 'choices': [{
  364. 'link': link({year_field: year_lookup, month_field: month.month}),
  365. 'title': capfirst(formats.date_format(month, 'YEAR_MONTH_FORMAT'))
  366. } for month in months]
  367. }
  368. else:
  369. years = getattr(cl.queryset, 'dates')(field_name, 'year')
  370. return {
  371. 'show': True,
  372. 'choices': [{
  373. 'link': link({year_field: str(year.year)}),
  374. 'title': str(year.year),
  375. } for year in years]
  376. }
  377. @register.tag(name='date_hierarchy')
  378. def date_hierarchy_tag(parser, token):
  379. return InclusionAdminNode(
  380. parser, token,
  381. func=date_hierarchy,
  382. template_name='date_hierarchy.html',
  383. takes_context=False,
  384. )
  385. def search_form(cl):
  386. """
  387. Display a search form for searching the list.
  388. """
  389. return {
  390. 'cl': cl,
  391. 'show_result_count': cl.result_count != cl.full_result_count,
  392. 'search_var': SEARCH_VAR
  393. }
  394. @register.tag(name='search_form')
  395. def search_form_tag(parser, token):
  396. return InclusionAdminNode(parser, token, func=search_form, template_name='search_form.html', takes_context=False)
  397. @register.simple_tag
  398. def admin_list_filter(cl, spec):
  399. tpl = get_template(spec.template)
  400. return tpl.render({
  401. 'title': spec.title,
  402. 'choices': list(spec.choices(cl)),
  403. 'spec': spec,
  404. })
  405. def admin_actions(context):
  406. """
  407. Track the number of times the action field has been rendered on the page,
  408. so we know which value to use.
  409. """
  410. context['action_index'] = context.get('action_index', -1) + 1
  411. return context
  412. @register.tag(name='admin_actions')
  413. def admin_actions_tag(parser, token):
  414. return InclusionAdminNode(parser, token, func=admin_actions, template_name='actions.html')
  415. @register.tag(name='change_list_object_tools')
  416. def change_list_object_tools_tag(parser, token):
  417. """Display the row of change list object tools."""
  418. return InclusionAdminNode(
  419. parser, token,
  420. func=lambda context: context,
  421. template_name='change_list_object_tools.html',
  422. )