Coverage for apps/ptf/cmds/dc_cmds.py: 0%

119 statements  

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

1import datetime 

2import re 

3 

4from ptf import exceptions 

5from ptf import model_helpers 

6from ptf.cmds import ptf_cmds 

7 

8from ..model_data import create_articledata 

9from ..model_data import create_issuedata 

10from ..model_data import create_publisherdata 

11from .xml.dublin_core import data 

12 

13# TODO: derive from addCollectionXmlCmd and addArticleXmlCmd instead of recreating a class 

14 

15 

16class addCollectionDc: 

17 def __init__(self, journal, provider): 

18 self.journal = journal 

19 self.provider = provider 

20 

21 def do(self): 

22 return self.internal_do() 

23 

24 def internal_do(self): 

25 # Create a collection 

26 col_params = { 

27 "coltype": "journal", 

28 "pid": self.journal, 

29 "lang": "en", 

30 "title_xml": self.journal, 

31 "title_tex": self.journal, 

32 "title_html": self.journal, 

33 "trans_title_html": self.journal, 

34 "trans_title_tex": self.journal, 

35 "solr_commit": False, 

36 "wall": 0, 

37 } 

38 

39 collection = model_helpers.get_collection(self.journal) 

40 cls = ptf_cmds.addCollectionPtfCmd if not collection else ptf_cmds.updateCollectionPtfCmd 

41 cmd = cls(col_params) 

42 cmd.set_provider(self.provider) 

43 return cmd.do() 

44 

45 

46class addArticleDc: 

47 journal = None 

48 xml_format = "data_dc" 

49 provider = None 

50 lang = "en" 

51 eprint = False 

52 thesis = False 

53 source = None 

54 

55 def __init__(self, item): 

56 self.item = item 

57 self.update = True 

58 

59 def set_xml_format(self, xml_format): 

60 self.xml_format = xml_format 

61 

62 def set_provider(self, provider): 

63 self.provider = provider 

64 

65 def set_lang(self, lang): 

66 self.lang = lang 

67 

68 def set_eprint(self, eprint): 

69 self.eprint = eprint 

70 

71 def set_source(self, source): 

72 self.source = source 

73 

74 def set_thesis(self, thesis): 

75 self.thesis = thesis 

76 

77 def do(self): 

78 return self.internal_do() 

79 

80 def internal_do(self): 

81 article = data.Article(self.item, self.lang) 

82 

83 article_identifier = article.get_identifier() 

84 reg_doi = re.compile("doi") 

85 if reg_doi.search(article_identifier): 

86 article_identifier = article_identifier.replace("doi:", "") 

87 article_db = model_helpers.get_article(article_identifier) 

88 

89 # c = Collection.objects.filter(resourceid__in=[r]) 

90 

91 # cmd = addCollectionDc(article.get_collection(), self.provider) 

92 if article_db is not None: 

93 container = article_db.my_container 

94 

95 self.journal = container.my_collection 

96 

97 if article_db is not None: 

98 if self.update: 

99 oai_article = None 

100 if hasattr(article_db, "oai_article"): 

101 oai_article = article_db.oai_article 

102 cmd = ptf_cmds.addArticlePtfCmd( 

103 { 

104 "pid": article_db.pid, 

105 "provider": article_db.provider, 

106 "container": article_db.my_container, 

107 } 

108 ) 

109 cmd.set_provider(article_db.provider) 

110 cmd.set_object_to_be_deleted(article_db) 

111 cmd.undo() 

112 if oai_article: 

113 oai_article.delete() 

114 else: 

115 raise exceptions.ResourceExists("Article %s already exists" % article_db.pid) 

116 

117 year = article.get_date() 

118 year = article.get_date().year if year else 0 

119 pid = f"{self.journal.pid}_{year}" 

120 issue = model_helpers.get_container(pid) 

121 if issue is None: 

122 xissue = create_issuedata() 

123 xissue.pid = pid 

124 xissue.ctype = "issue" 

125 xissue.abstracts = article.get_abstract() 

126 xissue.year = year 

127 xissue.volume = article.get_volume() 

128 xissue.number = article.get_volume_number() 

129 xissue.last_modified_iso_8601_date_str = datetime.datetime.now().strftime( 

130 "%Y-%m-%d %H:%M:%S" 

131 ) 

132 

133 cmd_container = ptf_cmds.addContainerPtfCmd({"xobj": xissue}) 

134 cmd_container.add_collection(self.journal) 

135 cmd_container.set_provider(self.provider) 

136 

137 if article.get_publisher(): 

138 publisher = model_helpers.get_publisher(article.get_publisher()) 

139 if not publisher: 

140 xpub = create_publisherdata() 

141 xpub.name = article.get_publisher() 

142 # publisher = cmd1a.do() 

143 cmd = ptf_cmds.addPublisherPtfCmd({"xobj": xpub}) 

144 publisher = cmd.do() 

145 cmd_container.set_publisher(publisher) 

146 issue = cmd_container.do() 

147 

148 fpage, lpage = article.get_pages() 

149 params = { 

150 "title_xml": article.get_title(), 

151 "title_html": article.get_title(), 

152 "title_tex": article.get_title(), 

153 "kwd_groups": [ 

154 {"content_type": "msc", "kwds": article.get_kwd(), "lang": article.get_lang()} 

155 ], 

156 "abstracts": article.get_abstract(), 

157 "seq": 0, 

158 "lang": article.get_lang(), 

159 "fpage": fpage, 

160 "lpage": lpage, 

161 } 

162 xarticle = create_articledata() 

163 xarticle.date_published_iso_8601_date_str = article.get_date_str() 

164 xarticle.title_xml = article.get_title() 

165 xarticle.title_html = article.get_title() 

166 xarticle.title_tex = article.get_title() 

167 xarticle.pid = article_identifier 

168 xarticle.kwd_groups = [ 

169 {"content_type": "msc", "kwds": article.get_kwd(), "lang": article.get_lang()} 

170 ] 

171 xarticle.seq = 0 

172 xarticle.lang = article.get_lang() 

173 xarticle.fpage = fpage 

174 xarticle.lpage = lpage 

175 cmd = ptf_cmds.addArticlePtfCmd({"xobj": xarticle}) 

176 cmd.set_eprint(self.eprint) 

177 cmd.set_source(self.source) 

178 cmd.set_thesis(self.thesis) 

179 cmd.set_container(issue) 

180 cmd.add_collection(self.journal) 

181 article_db = cmd.do() 

182 

183 params = { 

184 "rel": "link", 

185 "mimetype": "application/html", 

186 "location": article.get_link(), 

187 "seq": 1, 

188 "solr_commit": False, 

189 "metadata": article.get_link(), 

190 } 

191 cmd = ptf_cmds.addExtLinkPtfCmd(params) 

192 cmd.set_resource(article_db) 

193 cmd.do() 

194 

195 return article_db