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

1# (c) 2018-2020 

2# MPIB <https://www.mpib-berlin.mpg.de/>, 

3# MPI-CBS <https://www.cbs.mpg.de/>, 

4# MPIP <http://www.psych.mpg.de/> 

5# 

6# This file is part of Castellum. 

7# 

8# Castellum is free software; you can redistribute it and/or modify it 

9# under the terms of the GNU Affero General Public License as published 

10# by the Free Software Foundation; either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# Castellum is distributed in the hope that it will be useful, but 

14# WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

16# Affero General Public License for more details. 

17# 

18# You should have received a copy of the GNU Affero General Public 

19# License along with Castellum. If not, see 

20# <http://www.gnu.org/licenses/>. 

21 

22import datetime 

23 

24from django import template 

25from django.conf import settings 

26from django.db import models 

27from django.shortcuts import reverse 

28from django.utils.translation import gettext_lazy as _ 

29 

30register = template.Library() 

31 

32 

33@register.filter 

34def verbose_name(instance, field_name): 

35 if hasattr(instance, 'get_verbose_name'): 35 ↛ 36line 35 didn't jump to line 36, because the condition on line 35 was never true

36 return instance.get_verbose_name(field_name) 

37 else: 

38 field = instance._meta.get_field(field_name) 

39 if isinstance(field, models.Field): 

40 return field.verbose_name 

41 else: 

42 # FIXME: backrefs do not have a verbose_name 

43 return field.name 

44 

45 

46@register.filter 

47def model_verbose_name(instance, plural=False): 

48 return instance._meta.verbose_name_plural if plural else instance._meta.verbose_name 

49 

50 

51@register.filter 

52def display(instance, field_name): 

53 if hasattr(instance, 'get_display'): 53 ↛ 54line 53 didn't jump to line 54, because the condition on line 53 was never true

54 return instance.get_display(field_name) 

55 else: 

56 getter = getattr(instance, 'get_{}_display'.format(field_name), None) 

57 value = getter() if getter else getattr(instance, field_name) 

58 if value is True: 58 ↛ 59line 58 didn't jump to line 59, because the condition on line 58 was never true

59 return _('Yes') 

60 elif value is False: 

61 return _('No') 

62 elif value is None or value == '': 

63 return '—' 

64 elif hasattr(value, 'all'): 

65 return ', '.join(str(x) for x in value.all()) or '—' 

66 else: 

67 return value 

68 

69 

70@register.filter 

71def field_names(instance): 

72 if hasattr(instance, 'get_field_names'): 

73 return instance.get_field_names() 

74 else: 

75 return [field.name for field in instance._meta.get_fields()] 

76 

77 

78@register.filter(name='list') 

79def _list(qs): 

80 return list(qs) 

81 

82 

83@register.filter 

84def selected_label(bound_field): 

85 selected = str(bound_field.value()) 

86 for value, label in bound_field.field.choices: 86 ↛ exitline 86 didn't return from function 'selected_label', because the loop on line 86 didn't complete

87 if str(value) == selected: 

88 return label 

89 

90 

91@register.filter 

92def is_today(date): 

93 if isinstance(date, datetime.datetime): 

94 date = date.date() 

95 return date == datetime.date.today() 

96 

97 

98@register.simple_tag(takes_context=True) 

99def absolute_url(context, view_name, *args, **kwargs): 

100 request = context['request'] 

101 return request.build_absolute_uri(reverse(view_name, args=args, kwargs=kwargs)) 

102 

103 

104@register.simple_tag 

105def get_production(): 

106 return settings.PRODUCTION 

107 

108 

109@register.simple_tag 

110def get_enable_study_export(): 

111 return settings.CASTELLUM_ENABLE_STUDY_EXPORT