Coverage for apps/ptf/shortcuts.py: 23%

81 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-10-11 14:41 +0000

1from django.conf import settings 

2from django.http import Http404 

3 

4from .models import Article 

5from .models import Collection 

6from .models import Container 

7from .models import Resource 

8 

9try: 

10 USE_PID = settings.USE_NATURAL_ID 

11except AttributeError: 

12 USE_PID = False 

13 

14 

15def get_article(aid): 

16 manager = Article.objects 

17 if USE_PID: 17 ↛ 36line 17 didn't jump to line 36, because the condition on line 17 was never false

18 try: 

19 article = ( 

20 manager.select_related() 

21 .prefetch_related( 

22 "bibitem_set__bibitemid_set", 

23 "resourceid_set", 

24 "extid_set", 

25 "extlink_set", 

26 "datastream_set", 

27 "contribgroup_set__contrib_set", 

28 "subject_of", 

29 "abstract_set", 

30 ) 

31 .get(pid=aid, sites__id=settings.SITE_ID) 

32 ) 

33 except Article.DoesNotExist: 

34 raise Http404 

35 else: 

36 try: 

37 article = ( 

38 manager.select_related() 

39 .prefetch_related( 

40 "bibitem_set__bibitemid_set", 

41 "resourceid_set", 

42 "extid_set", 

43 "extlink_set", 

44 "datastream_set", 

45 "contribgroup_set__contrib_set", 

46 "subject_of", 

47 "abstract_set", 

48 ) 

49 .get(id=aid, sites__id=settings.SITE_ID) 

50 ) 

51 except Article.DoesNotExist: 

52 raise Http404 

53 return article 

54 

55 

56def get_issue(iid): 

57 qs = Container.objects.select_related("my_collection") 

58 if USE_PID: 

59 try: 

60 issue = qs.prefetch_related( 

61 "article_set__contribgroup_set__contrib_set", "article_set__subject_of" 

62 ).get(pid=iid, ctype="issue", sites__id=settings.SITE_ID) 

63 except Container.DoesNotExist: 

64 raise Http404 

65 else: 

66 try: 

67 issue = qs.prefetch_related( 

68 "article_set__contribgroup_set__contrib_set", "article_set__subject_of" 

69 ).get(id=iid, ctype="issue", sites__id=settings.SITE_ID) 

70 except Container.DoesNotExist: 

71 raise Http404 

72 return issue 

73 

74 

75# def get_issue(iid): 

76# qs = Container.objects.select_related('serial') 

77# if USE_PID: 

78# try: 

79# issue = qs.get(pid=iid, ctype='issue', 

80# sites__id=settings.SITE_ID) 

81# except Container.DoesNotExist: 

82# raise Http404 

83# else: 

84# try: 

85# issue = qs.get(id=iid, ctype='issue', 

86# sites__id=settings.SITE_ID) 

87# except Container.DoesNotExist: 

88# raise Http404 

89# return issue 

90 

91 

92def get_journal(jid): 

93 if USE_PID: 

94 try: 

95 journal = Collection.objects.get(pid=jid, sites__id=settings.SITE_ID) 

96 except Collection.DoesNotExist: 

97 raise Http404 

98 else: 

99 try: 

100 journal = Collection.objects.get(id=jid, sites__id=settings.SITE_ID) 

101 except Collection.DoesNotExist: 

102 raise Http404 

103 return journal 

104 

105 

106def get_book(bid): 

107 if USE_PID: 

108 try: 

109 book = Container.objects.get( 

110 pid=bid, ctype__startswith="book", sites__id=settings.SITE_ID 

111 ) 

112 except Container.DoesNotExist: 

113 raise Http404 

114 else: 

115 try: 

116 book = Container.objects.get( 

117 id=bid, ctype__startswith="book", sites__id=settings.SITE_ID 

118 ) 

119 except Container.DoesNotExist: 

120 raise Http404 

121 return book 

122 

123 

124# def get_book_serie(sid): 

125# """ 

126# 09/06/2021: Unused. Remove ? Miss lecture-notes 

127# :param sid: 

128# :return: 

129# """ 

130# if USE_PID: 

131# try: 

132# series = Collection.objects.get(pid=sid, coltype="book-series", sites__id=settings.SITE_ID) 

133# except Collection.DoesNotExist: 

134# raise Http404 

135# else: 

136# try: 

137# series = Collection.objects.get(id=sid, coltype="book-series", sites__id=settings.SITE_ID) 

138# except Collection.DoesNotExist: 

139# raise Http404 

140# return series 

141 

142 

143def get_collection(sid): 

144 if USE_PID: 

145 try: 

146 col = Collection.objects.get(pid=sid, sites__id=settings.SITE_ID) 

147 except Collection.DoesNotExist: 

148 raise Http404 

149 else: 

150 try: 

151 col = Collection.objects.get(id=sid, sites__id=settings.SITE_ID) 

152 except Collection.DoesNotExist: 

153 raise Http404 

154 return col 

155 

156 

157def get_resource(rid): 

158 if USE_PID: 

159 try: 

160 ressource = Resource.objects.get(pid=rid, sites__id=settings.SITE_ID) 

161 except Resource.DoesNotExist: 

162 raise Http404 

163 else: 

164 try: 

165 ressource = Resource.objects.get(id=rid, sites__id=settings.SITE_ID) 

166 except Resource.DoesNotExist: 

167 raise Http404 

168 return ressource 

169 

170 

171# def get_serial(sid): 

172# """ 

173# 09/06/2021: Unused. Remove ? Miss lecture-notes 

174# :param sid: 

175# :return: 

176# """ 

177# try: 

178# serial = get_journal(sid) 

179# except Http404: 

180# serial = get_book_serie(sid) 

181# return serial 

182 

183 

184def get_all_journals(): 

185 qs = Collection.objects.filter(sites__id=settings.SITE_ID) 

186 return qs