Coverage for apps/ptf/templatetags/helpers.py: 62%

175 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-19 19:20 +0000

1import os 

2 

3from langcodes import Language 

4 

5from django import template 

6from django.http import QueryDict 

7from django.template.defaultfilters import stringfilter 

8from django.urls import reverse 

9from django.utils.translation import gettext as _ 

10 

11from ptf import models 

12from ptf.display import resolver 

13from ptf.model_helpers import get_resource as get_resource_helper 

14from ptf.solr.search_helpers import CleanSearchURL 

15 

16register = template.Library() 

17 

18 

19@register.filter 

20@stringfilter 

21def get_resource(value): 

22 """Usage, {% if pid|get_resource %}""" 

23 resource = get_resource_helper(value) 

24 return resource 

25 

26 

27@register.filter 

28@stringfilter 

29def cleanURL_encode(value, arg): 

30 """Usage : {{ 'q=test&qt=all'|cleanURL_encode:request.path }}""" 

31 query = QueryDict(value) 

32 return CleanSearchURL.encode(query, arg) 

33 

34 

35@register.simple_tag 

36def pretty_search(url, encoding, *args): 

37 """ 

38 Usage {% pretty_search 'path' 'q' criteria1 criteria2 ... %} 

39 @param path: the url name 

40 @param encoding: one or many encodings reference to CleanSearchURL 

41 @param args: one or many criteria for search 

42 @return: url with querystring like search?q=test+a 

43 """ 

44 path = reverse(url) 

45 clean_url = CleanSearchURL(path) 

46 for index in range(len(encoding)): 

47 clean_url.append(args[index], encoding[index]) 

48 url = clean_url.to_href() 

49 return url 

50 

51 

52@register.filter 

53@stringfilter 

54def cleanURL_params_paginate(value, page): 

55 """ 

56 création du lien pour la pagination 

57 exemple Usage : {{ '?q=test+all+qa'|cleanURL_encode:"3" }} 

58 @return: ?q=test+all+3+qat 

59 """ 

60 

61 _, query = CleanSearchURL.decode(value) 

62 queryDict = query.copy() 

63 if "page" in queryDict: 

64 del queryDict["page"] # supprime la page demandée 

65 queryDict.appendlist("page", str(page)) 

66 

67 result = CleanSearchURL.encode(queryDict, "") 

68 return result 

69 

70 

71@register.filter 

72@stringfilter 

73def replace_basename_binary_file(value): 

74 """Usage, {% binary_file.pdf| static_basename_binary_file %}""" 

75 # if hasattr(settings, 'SITE_URL_PREFIX'): 

76 # if len(value) > 0 and value[0] != '/': 

77 # value = '/' + settings.SITE_URL_PREFIX + '/' + value 

78 # else: 

79 # value = '/' + settings.SITE_URL_PREFIX + value 

80 return value 

81 

82 

83@register.filter 

84@stringfilter 

85def basename(value): 

86 return os.path.basename(value) 

87 

88 

89@register.filter 

90def sort_by(queryset, order): 

91 return queryset.order_by(order) 

92 

93 

94@register.filter 

95def sort_by_date(queryset, container): 

96 if container.my_collection.pid == "PCJ": 96 ↛ 97line 96 didn't jump to line 97, because the condition on line 96 was never true

97 return queryset.order_by("-date_published") 

98 return queryset 

99 

100 

101@register.simple_tag 

102def get_flag_icon(language): 

103 flag = "flag-icon-" 

104 

105 if language == "en": 

106 flag += "gb" 

107 else: 

108 flag += language 

109 

110 return flag 

111 

112 

113@register.filter 

114def get_flag_unicode(lang): 

115 territory = Language.get(lang).maximize().territory 

116 if lang == "en": 

117 territory = "GB" 

118 elif lang == "pt": 

119 territory = "PT" 

120 points = [ord(x) + 127397 for x in territory] 

121 return chr(points[0]) + chr(points[1]) 

122 

123 

124@register.filter 

125@stringfilter 

126def short_doi(value): 

127 if len(value) > 9: 127 ↛ 130line 127 didn't jump to line 130, because the condition on line 127 was never false

128 value = value[8:] 

129 

130 return value 

131 

132 

133@register.filter 

134@stringfilter 

135def get_doi_url(value): 

136 if value and value.find("10.") == 0: 

137 value = resolver.get_doi_url(value) 

138 else: 

139 value = "" 

140 

141 return value 

142 

143 

144@register.filter 

145@stringfilter 

146def get_type_value(value): 

147 value = resolver.ARTICLE_TYPES.get(value, "Article de recherche") 

148 return value 

149 

150 

151@register.filter 

152def exclude_do_not_publish_filter(queryset, export_to_website): 

153 """ 

154 if we export to website (production), we don't export article for which do_not_publish=True 

155 """ 

156 qs = queryset 

157 if export_to_website: 157 ↛ 158line 157 didn't jump to line 158, because the condition on line 157 was never true

158 qs = queryset.exclude(do_not_publish=True) 

159 return qs 

160 

161 

162@register.filter 

163def are_all_equal_contrib(contributions): 

164 return models.are_all_equal_contrib(contributions) 

165 

166 

167@register.filter 

168def filter_by_role(contributions, role): 

169 if contributions is None: 169 ↛ 170line 169 didn't jump to line 170, because the condition on line 169 was never true

170 return [] 

171 return [contribution for contribution in contributions if contribution.role.find(role) == 0] 

172 

173 

174@register.filter 

175def get_addresses(contributions): 

176 addresses = [] 

177 for contribution in contributions: 

178 for contrib_address in contribution.contribaddress_set.all(): 

179 if contrib_address.address not in addresses: 

180 addresses.append(contrib_address.address) 

181 return addresses 

182 

183 

184@register.filter 

185def contributions_with_index(contributions, addresses): 

186 results = [] 

187 for contribution in contributions: 

188 indexes = [] 

189 for contrib_address in contribution.contribaddress_set.all(): 

190 address = contrib_address.address 

191 index = addresses.index(address) + 1 

192 if index not in [item["index"] for item in indexes]: 

193 indexes.append({"index": index, "address": address}) 

194 # indexes.sort(key=lambda d: d['index']) 

195 results.append((contribution, indexes)) 

196 return results 

197 

198 

199@register.filter 

200def address_index_value(index, with_letter): 

201 value = index 

202 if with_letter: 

203 value = chr(ord("a") + index - 1) 

204 return value 

205 

206 

207@register.filter 

208def affiliation_id(with_letter): 

209 return "affilition_a" if with_letter else "affiliation" 

210 

211 

212@register.filter 

213def contributions_with_letter_index(contributions, addresses): 

214 results = [] 

215 for contribution in contributions: 

216 indexes = [] 

217 for contrib_address in contribution.contribaddress_set.all(): 

218 address = contrib_address.address 

219 index = addresses.index(address) + 1 

220 letter = chr(ord("a") + index) 

221 if index not in [item["index"] for item in indexes]: 

222 indexes.append({"index": letter, "address": address}) 

223 # indexes.sort(key=lambda d: d['index']) 

224 results.append((contribution, indexes)) 

225 return results 

226 

227 

228@register.filter 

229def is_author_of_original_article(contribution, original_contributions): 

230 if original_contributions is None or len(original_contributions) == 0: 

231 return False 

232 

233 is_original_author = False 

234 i = 0 

235 while not is_original_author and i < len(original_contributions): 

236 if contribution.is_equal(original_contributions[i]): 

237 is_original_author = True 

238 i += 1 

239 

240 return is_original_author 

241 

242 

243@register.filter 

244def to_str(value): 

245 return str(value) 

246 

247 

248@register.filter 

249def get_dict_item(my_dict: dict, key: str): 

250 """ 

251 Wrapper around dict.get() that can be used directly in templates. 

252 Its purpose is to the ability to pass a variable instead of a plain string. 

253 Usage `my_dict|get_dict_item:key` 

254 """ 

255 return my_dict.get(key) 

256 

257 

258@register.filter 

259def get_article_heading(article): 

260 heading = article.get_subj_text() 

261 collection = article.get_top_collection() 

262 if collection.pid in ["CRMATH", "CRMECA", "CRPHYS", "CRGEOS", "CRCHIM", "CRBIOL"]: 262 ↛ 271line 262 didn't jump to line 271, because the condition on line 262 was never false

263 if int(article.my_container.year) > 2023: 263 ↛ 264line 263 didn't jump to line 264, because the condition on line 263 was never true

264 atype = resolver.ARTICLE_TYPES.get(article.atype, None) 

265 if atype is not None: 

266 if heading: 

267 heading = _(atype) + " - " + heading 

268 else: 

269 heading = _(atype) 

270 

271 return heading