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

188 statements  

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

1import os 

2 

3from langcodes import Language 

4 

5from django import template 

6from django.contrib.staticfiles.finders import find 

7from django.http import QueryDict 

8from django.template.defaultfilters import stringfilter 

9from django.urls import reverse 

10from django.utils.translation import gettext as _ 

11 

12from ptf import models 

13from ptf.display import resolver 

14from ptf.model_helpers import get_resource as get_resource_helper 

15from ptf.solr.search_helpers import CleanSearchURL 

16 

17register = template.Library() 

18 

19 

20@register.filter 

21@stringfilter 

22def get_resource(value): 

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

24 resource = get_resource_helper(value) 

25 return resource 

26 

27 

28@register.filter 

29@stringfilter 

30def cleanURL_encode(value, arg): 

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

32 query = QueryDict(value) 

33 return CleanSearchURL.encode(query, arg) 

34 

35 

36@register.simple_tag 

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

38 """ 

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

40 @param path: the url name 

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

42 @param args: one or many criteria for search 

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

44 """ 

45 path = reverse(url) 

46 clean_url = CleanSearchURL(path) 

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

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

49 url = clean_url.to_href() 

50 return url 

51 

52 

53@register.filter 

54@stringfilter 

55def cleanURL_params_paginate(value, page): 

56 """ 

57 création du lien pour la pagination 

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

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

60 """ 

61 

62 _, query = CleanSearchURL.decode(value) 

63 queryDict = query.copy() 

64 if "page" in queryDict: 

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

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

67 

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

69 return result 

70 

71 

72@register.filter 

73@stringfilter 

74def replace_basename_binary_file(value): 

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

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

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

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

79 # else: 

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

81 return value 

82 

83 

84@register.filter 

85@stringfilter 

86def basename(value): 

87 return os.path.basename(value) 

88 

89 

90@register.filter 

91def sort_by(queryset, order): 

92 return queryset.order_by(order) 

93 

94 

95@register.filter 

96def sort_by_date(queryset, container): 

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

98 return queryset.order_by("-date_published") 

99 return queryset 

100 

101 

102@register.simple_tag 

103def get_flag_icon(language): 

104 flag = "flag-icon-" 

105 

106 if language == "en": 

107 flag += "gb" 

108 else: 

109 flag += language 

110 

111 return flag 

112 

113 

114@register.filter 

115def get_flag_unicode(lang): 

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

117 if lang == "en": 

118 territory = "GB" 

119 elif lang == "pt": 

120 territory = "PT" 

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

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

123 

124 

125@register.filter 

126@stringfilter 

127def short_doi(value): 

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

129 value = value[8:] 

130 

131 return value 

132 

133 

134@register.filter 

135@stringfilter 

136def get_doi_url(value): 

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

138 value = resolver.get_doi_url(value) 

139 else: 

140 value = "" 

141 

142 return value 

143 

144 

145@register.filter 

146@stringfilter 

147def get_type_value(value): 

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

149 return value 

150 

151 

152@register.filter 

153def exclude_do_not_publish_filter(queryset, export_to_website): 

154 """ 

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

156 """ 

157 qs = queryset 

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

159 qs = queryset.exclude(do_not_publish=True) 

160 return qs 

161 

162 

163@register.filter 

164def are_all_equal_contrib(contributions): 

165 return models.are_all_equal_contrib(contributions) 

166 

167 

168@register.filter 

169def filter_by_role(contributions, role): 

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

171 return [] 

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

173 

174 

175@register.filter 

176def get_addresses(contributions): 

177 addresses = [] 

178 for contribution in contributions: 

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

180 if contrib_address.address not in addresses: 

181 addresses.append(contrib_address.address) 

182 return addresses 

183 

184 

185@register.filter 

186def contributions_with_index(contributions, addresses): 

187 results = [] 

188 for contribution in contributions: 

189 indexes = [] 

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

191 address = contrib_address.address 

192 index = addresses.index(address) + 1 

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

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

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

196 results.append((contribution, indexes)) 

197 return results 

198 

199 

200@register.filter 

201def address_index_value(index, with_letter): 

202 value = index 

203 if with_letter: 

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

205 return value 

206 

207 

208@register.filter 

209def affiliation_id(with_letter): 

210 return "affilition_a" if with_letter else "affiliation" 

211 

212 

213@register.filter 

214def contributions_with_letter_index(contributions, addresses): 

215 results = [] 

216 for contribution in contributions: 

217 indexes = [] 

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

219 address = contrib_address.address 

220 index = addresses.index(address) + 1 

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

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

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

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

225 results.append((contribution, indexes)) 

226 return results 

227 

228 

229@register.filter 

230def is_author_of_original_article(contribution, original_contributions): 

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

232 return False 

233 

234 is_original_author = False 

235 i = 0 

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

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

238 is_original_author = True 

239 i += 1 

240 

241 return is_original_author 

242 

243 

244@register.filter 

245def to_str(value): 

246 return str(value) 

247 

248 

249@register.filter 

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

251 """ 

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

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

254 Usage `my_dict|get_dict_item:key` 

255 """ 

256 return my_dict.get(key) 

257 

258 

259@register.filter 

260def get_article_heading(article): 

261 heading = article.get_subj_text() 

262 collection = article.get_top_collection() 

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

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

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

266 if atype is not None: 

267 if heading: 

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

269 else: 

270 heading = _(atype) 

271 

272 return heading 

273 

274 

275@register.filter 

276def get_image_path(journal: models.Collection, logo: bool = False): 

277 """ 

278 Usage: `journal|get_image_path` 

279 @param journal: La revue dont l'on veut récupérer l'image 

280 @param logo: Booléen indiquant si l'on veut récupérer le logo ou non 

281 @return un string contenant le chemin vers l'image, par exemple : "pid/img/PID.jpg" ou "pid/img/pid.png" dans le cas d'un logo 

282 """ 

283 if journal is None or type(journal) is not models.Collection: 

284 return "" 

285 

286 pid = journal.pid 

287 # Si le pid final est en minuscule, on récupère le logo 

288 link = pid.lower() + "/img/" + (pid if not logo else pid.lower()) 

289 for extension in [".jpg", ".png"]: 

290 # Si find renvoie None, c'est que l'image n'a pas été trouvé pour cette extension 

291 abs_path = find(link + extension) 

292 if abs_path is not None: 292 ↛ 293line 292 didn't jump to line 293, because the condition on line 292 was never true

293 break 

294 else: 

295 # Dans le cas où on a rien trouvé 

296 return "" 

297 

298 return link + extension