Coverage for sites/numdam/numdam/views.py: 30%

97 statements  

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

1import random 

2 

3from extra_views import FormSetSuccessMessageMixin 

4from extra_views import ModelFormSetView 

5 

6from django.conf import settings 

7from django.forms import TextInput 

8from django.http import Http404 

9from django.shortcuts import redirect 

10from django.shortcuts import render 

11from django.urls import reverse 

12from django.views.decorators.http import require_http_methods 

13from django.views.generic import RedirectView 

14 

15from ptf import model_helpers 

16from ptf.models import Article 

17from ptf.models import Collection 

18from ptf.models import Stats 

19 

20from .forms import FormSetHelper 

21 

22 

23class CollectionLiveManagement(FormSetSuccessMessageMixin, ModelFormSetView): 

24 model = Collection 

25 fields = [ 

26 "pid", 

27 "alive", 

28 ] 

29 factory_kwargs = { 

30 "extra": 0, 

31 "widgets": { 

32 "pid": TextInput(attrs={"readonly": "true"}), 

33 }, 

34 } 

35 template_name = "collection_formset.html" 

36 success_message = "Statuts mis à jour avec succès" 

37 

38 def get_queryset(self): 

39 # First, we need to update all child collections alive field to false 

40 Collection.objects.filter(parent__isnull=False).update(alive=False) 

41 # Then, we can continue the normal process of the view 

42 return super().get_queryset().filter(parent__isnull=True) 

43 

44 def get_context_data(self, **kwargs): 

45 context = super().get_context_data(**kwargs) 

46 context["helper"] = FormSetHelper() 

47 return context 

48 

49 

50def home(request): 

51 # new = models.SerialHistory.objects.select_related('serial').all() 

52 

53 page_count = 0 

54 stat = Stats.objects.first() 

55 if stat: 

56 page_count = stat.value 

57 

58 article_count = Article.objects.count() 

59 journal_count = model_helpers.get_journals().count() 

60 acta_count = model_helpers.get_actas().count() 

61 book_count = model_helpers.get_books().count() 

62 book_collection_count = model_helpers.get_collection_of_books(with_lectures=True).count() 

63 thesis_count = model_helpers.get_theses().count() 

64 

65 carousel = [] 

66 # latest_containers = model_helpers.get_random_containers() 

67 latest_pids = settings.CAROUSEL_NEW_PIDS 

68 random.shuffle(latest_pids) 

69 carousel_count = 0 

70 all_pids = [] 

71 

72 # for container in latest_containers: 

73 for pid in latest_pids: 

74 resource = model_helpers.get_resource(pid) 

75 if resource: 

76 obj = resource.cast() 

77 collection = obj.get_collection() 

78 icon = collection.small_icon() if collection.pid != "CJPS" else obj.small_icon() 

79 

80 item = { 

81 "title": obj.get_collection().title_tex, 

82 "icon": icon, 

83 "new": True, 

84 "volume": obj.get_volume(), 

85 "number": obj.get_number(), 

86 "pid": pid, 

87 } 

88 carousel.append(item) 

89 carousel_count += 1 

90 all_pids.append(pid) 

91 

92 if carousel_count == 6: 

93 break 

94 

95 pids = settings.CAROUSEL_PIDS 

96 random.shuffle(pids) 

97 

98 for pid in pids: 

99 if pid not in all_pids: 

100 resource = model_helpers.get_resource(pid) 

101 if resource: 

102 obj = resource.cast() 

103 

104 item = { 

105 "title": obj.title_tex, 

106 "icon": obj.small_icon(), 

107 "new": False, 

108 "volume": "", 

109 "number": "", 

110 "pid": pid, 

111 } 

112 

113 carousel.append(item) 

114 

115 carousel_count += 1 

116 

117 if carousel_count == 12: 

118 break 

119 

120 random.shuffle(carousel) 

121 context = { 

122 "page_count": page_count, 

123 "article_count": article_count, 

124 "journal_count": journal_count, 

125 "acta_count": acta_count, 

126 "book_count": book_count, 

127 "book_collection_count": book_collection_count, 

128 "thesis_count": thesis_count, 

129 "carousel": carousel, 

130 } 

131 

132 return render(request, "home.html", context) 

133 

134 

135# prise en compte d'une redirection pour des liens fournis sur GALLICA BNF 

136# -> lien fourni par le REPOX oai gallica 

137@require_http_methods(["GET"]) 

138def gallicaBnf(request): 

139 """ 

140 redirect to the collection page 

141 @param request: 

142 @return: 

143 """ 

144 colid = request.GET.get("j") 

145 journal = model_helpers.get_collection(colid) 

146 

147 if journal is None: 

148 raise Http404 

149 return redirect(reverse("journal-issues", kwargs={"jid": colid})) 

150 

151 

152@require_http_methods(["GET"]) 

153def complete_works(request, api=False): 

154 context = {} 

155 

156 return render(request, "complete-works.html", context) 

157 

158 

159class RedirectOldUrlView(RedirectView): 

160 pattern_name = "item_id" 

161 

162 def get_redirect_url(self, *args, **kwargs): 

163 if not (kwargs.get("pid", None) or self.request.GET.get("id", None)): 

164 raise Http404 

165 

166 the_pid = kwargs.get("pid", None) 

167 

168 if not the_pid: 

169 kwargs["pid"] = self.request.GET.get("id") 

170 kwargs["pid"] = kwargs["pid"].rstrip(".djvu").rstrip(".pdf") 

171 

172 if "extension" in kwargs: 

173 kwargs.pop("extension") 

174 

175 return super().get_redirect_url(*args, **kwargs) 

176 

177 

178def app_logout(request): 

179 pass