Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from django import template 

2from django.core.paginator import EmptyPage 

3from django.utils.safestring import mark_safe 

4from django.urls import reverse_lazy 

5from django.conf import settings 

6 

7from classytags.helpers import InclusionTag 

8 

9register = template.Library() 

10 

11@register.inclusion_tag('djangocms_unimelb/paginator.html', takes_context=True) 

12def unimelb_paginator(context, adjacent_pages=3): 

13 """ 

14 Inclusion tag for rendering a paginator for multi-page lists. 

15 

16 Adapted from https://romanvm.pythonanywhere.com/post/bootstrap4-based-pagination-django-listview-30/ 

17 

18 Adds pagination context variables for use in displaying first, adjacent and 

19 last page links in addition to those created by the object_list generic 

20 view. 

21 

22 :param context: parent template context 

23 :param adjacent_pages: the number of pages adjacent to the current 

24 :return: context for rendering paginator html code 

25 """ 

26 start_page = max(context['page_obj'].number - adjacent_pages, 1) 

27 if start_page <= 3: 

28 start_page = 1 

29 end_page = context['page_obj'].number + adjacent_pages + 1 

30 if end_page >= context['paginator'].num_pages - 1: 

31 end_page = context['paginator'].num_pages + 1 

32 page_numbers = [n for n in range(start_page, end_page) if n in range(1, context['paginator'].num_pages + 1)] 

33 page_obj = context['page_obj'] 

34 paginator = context['paginator'] 

35 try: 

36 next_ = context['page_obj'].next_page_number() 

37 except EmptyPage: 

38 next_ = None 

39 try: 

40 previous = context['page_obj'].previous_page_number() 

41 except EmptyPage: 

42 previous = None 

43 return { 

44 'page_obj': page_obj, 

45 'paginator': paginator, 

46 'page': context['page_obj'].number, 

47 'pages': context['paginator'].num_pages, 

48 'page_numbers': page_numbers, 

49 'next': next_, 

50 'previous': previous, 

51 'has_next': context['page_obj'].has_next(), 

52 'has_previous': context['page_obj'].has_previous(), 

53 'show_first': 1 not in page_numbers, 

54 'show_last': context['paginator'].num_pages not in page_numbers, 

55 'request': context['request'] 

56 } 

57 

58 

59class PageMetaInclusionTag(InclusionTag): 

60 push_context=True 

61 def render_tag(self, context, **kwargs): 

62 if 'djangocms_page_meta' in settings.INSTALLED_APPS: 

63 return super().render_tag(context, **kwargs) 

64 return "" 

65 

66 

67class UnimelbPageMeta(PageMetaInclusionTag): 

68 name = "unimelb_page_meta" 

69 template = 'djangocms_unimelb/page_meta.html' 

70 

71register.tag(UnimelbPageMeta) 

72 

73 

74class UnimelbSchemaOrgHTMLScope(PageMetaInclusionTag): 

75 name = "unimelb_schemaorg_html_scope" 

76 template = 'djangocms_unimelb/schemaorg_html_scope.html' 

77 

78register.tag(UnimelbSchemaOrgHTMLScope) 

79 

80 

81@register.inclusion_tag('djangocms_unimelb/404.html') 

82def unimelb_404(): 

83 return {} 

84 

85 

86@register.inclusion_tag('djangocms_unimelb/500.html') 

87def unimelb_500(): 

88 return {}