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

1import logging 

2 

3from django import template 

4from django.utils.html import format_html 

5from wagtail.core.models import Page 

6 

7from discuss_data.ddcomments.forms import CommentForm, NotificationForm 

8from discuss_data.ddcomments.models import Notification 

9from discuss_data.ddusers.models import User 

10from discuss_data.pages.models import ManualPage, TocTree 

11from discuss_data.dddatasets.models import License 

12 

13register = template.Library() 

14DEFAULT_USER_IMAGE = "/static/images/user_default.png" 

15 

16logger = logging.getLogger(__name__) 

17 

18 

19@register.simple_tag 

20def nav_active(req_path, url_str, *args, **kwargs): 

21 """ 

22 nav_active() will check the web request path and compare it 

23 to the given url string, setting the active class if they match. 

24 

25 Needs clean URL naming scheme! 

26 

27 Use in template: {% nav_active request.path "/url_str_here"%} 

28 """ 

29 try: 

30 exclude = kwargs["exclude"] 

31 except KeyError: 

32 exclude = None 

33 

34 if exclude: 

35 exlist = exclude.split(" ") 

36 

37 try: 

38 if exclude: 

39 if req_path.startswith(url_str): 

40 for entry in exlist: 

41 if req_path.startswith(entry): 

42 return "" 

43 return "active" 

44 else: 

45 if req_path.startswith(url_str): 

46 return "active" 

47 return "" 

48 except Exception: 

49 return "" 

50 

51 

52@register.filter 

53def get_dict(some_object): 

54 """ 

55 Returns the __dict__ for every object 

56 """ 

57 return some_object.__dict__ 

58 

59 

60@register.filter 

61def remove_underscores(string): 

62 """ 

63 Returns the string with underscores replaces by spaces 

64 """ 

65 return string.replace("_", " ") 

66 

67 

68@register.filter 

69def get_user_image(uuid): 

70 user = User.objects.get(uuid=uuid) 

71 try: 

72 image_url = user.photo.url 

73 except Exception: 

74 image_url = DEFAULT_USER_IMAGE 

75 return image_url 

76 

77 

78@register.filter 

79def get_value(dictionary, key): 

80 """ 

81 Returns the value in dictionary 

82 """ 

83 try: 

84 return dictionary[key] 

85 except Exception: 

86 return None 

87 

88 

89@register.filter 

90def get_file_icon(mime_str: str) -> str: 

91 """ 

92 Returns the icon class string for the mime group 

93 """ 

94 if mime_str == "application/zip": 

95 return "i-file-zip-md" 

96 if mime_str in ("archive/pdf", "application/pdf"): 

97 return "i-file-pdf-md" 

98 

99 try: 

100 kind, ext = mime_str.split("/") 

101 return "i-file-" + kind + "-md" 

102 except Exception: 

103 return "i-file-md" 

104 

105 

106@register.filter 

107def user_has_restricted_access(dataset, user): 

108 return dataset.user_has_ra_view_right(user) 

109 

110 

111@register.filter 

112def user_has_admin_right(dataset, user): 

113 try: 

114 return dataset.user_has_admin_right(user) 

115 except AttributeError: 

116 logger.error( 

117 "Dataset mentioned in notification does not exist. Triggerd by user %s", 

118 format(user.username), 

119 ) 

120 return False 

121 

122 

123@register.filter 

124def prepare_notification_form(notification, user): 

125 if notification.notification_type == Notification.ACCESS_REQUEST: 

126 recipient_uuid = notification.owner.uuid 

127 elif notification.notification_type == Notification.PUB_REQUEST: 

128 recipient_uuid = notification.owner.uuid 

129 else: 

130 if notification.owner.uuid == user.uuid: 

131 recipient_uuid = notification.recipient.uuid 

132 else: 

133 recipient_uuid = notification.owner.uuid 

134 

135 parent_uuid = None 

136 if notification.level > 0: 

137 if not notification.get_next_sibling(): 

138 if notification.parent: 

139 parent_uuid = notification.parent.uuid 

140 initial = { 

141 "recipient_uuid": recipient_uuid, 

142 "parent_uuid": parent_uuid, 

143 } 

144 return NotificationForm(initial=initial) 

145 else: 

146 if notification.is_leaf_node(): 

147 parent_uuid = notification.uuid 

148 initial = { 

149 "recipient_uuid": recipient_uuid, 

150 "parent_uuid": parent_uuid, 

151 } 

152 return NotificationForm(initial=initial) 

153 return None 

154 

155 

156@register.filter 

157def prepare_comment_form(comment, user): 

158 parent_uuid = None 

159 if comment.level > 0: 

160 if not comment.get_next_sibling(): 

161 if comment.parent: 

162 parent_uuid = comment.parent.uuid 

163 initial = { 

164 "parent_uuid": parent_uuid, 

165 } 

166 return CommentForm(initial=initial) 

167 else: 

168 if comment.is_leaf_node(): 

169 parent_uuid = comment.uuid 

170 initial = { 

171 "parent_uuid": parent_uuid, 

172 } 

173 return CommentForm(initial=initial) 

174 return None 

175 

176 

177@register.filter 

178def get_parent_comment(comment): 

179 parent = None 

180 if comment.level > 0: 

181 if not comment.get_next_sibling(): 

182 if comment.parent: 

183 parent = comment.parent 

184 else: 

185 if comment.is_leaf_node(): 

186 parent = comment 

187 return parent 

188 

189 

190@register.filter 

191def subtract(value, arg): 

192 return value - arg 

193 

194 

195@register.filter 

196def divide(value, arg): 

197 return value / arg 

198 

199 

200@register.filter 

201def replace_vars(string, vars_dict): 

202 for key, value in vars_dict.items(): 

203 string = str(string).replace(key, value) 

204 return string 

205 

206 

207@register.simple_tag(takes_context=True) 

208def get_all_pages(context): 

209 return Page.objects.live() 

210 

211 

212@register.simple_tag(takes_context=True) 

213def get_root_help_page(context): 

214 """get the root page of help""" 

215 try: 

216 root_help_page = Page.objects.live().get(slug="discuss-data-help") 

217 except Page.DoesNotExist: 

218 root_help_page = None 

219 return root_help_page 

220 

221 

222@register.simple_tag 

223def get_toc_tree(slug): 

224 """get the table of contents saved in a TocTree for navigation""" 

225 return format_html(TocTree.objects.get(slug=slug).html) 

226 

227 

228@register.simple_tag(takes_context=True) 

229def get_curators_page(context): 

230 """get the curators page from wagtail""" 

231 try: 

232 curators_page = ManualPage.objects.live().get(slug="who-are-curators") 

233 except ManualPage.DoesNotExist: 

234 curators_page = None 

235 return curators_page 

236 

237 

238@register.filter 

239def add_url_search_params(search_params_dict): 

240 """add search params from a dictionary of lists to an url""" 

241 params_string = "?q=" 

242 query = search_params_dict["q"] 

243 if query: 

244 params_string += query 

245 

246 for key, value in search_params_dict.items(): 

247 if key != "q": 

248 if value: 

249 for entry in value: 

250 params_string += "&{}={}".format(key, entry) 

251 return params_string 

252 

253 

254@register.simple_tag 

255def get_manpage(page_slug): 

256 """ get a man page from wagtail 

257 """ 

258 try: 

259 man_page = ManualPage.objects.live().get(slug=page_slug) 

260 except ManualPage.DoesNotExist: 

261 man_page = None 

262 return man_page 

263 

264 

265@register.filter 

266def get_license(slug): 

267 """ get a license from a slug 

268 """ 

269 try: 

270 return License.objects.get(license_slug=slug) 

271 except License.DoesNotExist: 

272 return None