Coverage for portality/crosswalks/article_form.py: 95%

113 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-22 15:59 +0100

1from portality import models 

2 

3class ArticleFormXWalk(object): 

4 """ 

5 ~~ArticleForm:Crosswalk->Article:Form~~ 

6 ~~->Article:Model~~ 

7 """ 

8 format_name = "form" 

9 

10 @classmethod 

11 def form2obj(cls, form, add_journal_info=True): 

12 article = models.Article() 

13 bibjson = article.bibjson() 

14 

15 # title 

16 if form.title.data is not None and form.title.data != "": 

17 bibjson.title = form.title.data 

18 

19 # doi 

20 doi = form.doi.data 

21 if doi is not None and doi != "": 

22 bibjson.add_identifier(bibjson.DOI, doi) 

23 

24 # authors 

25 if bibjson.author: 

26 bibjson.author = [] 

27 for subfield in form.authors: 

28 if subfield.form.name.data != "": 

29 aff = "" 

30 orcid_id = "" 

31 author = subfield.form.name.data 

32 if subfield.form.affiliation.data != "": 

33 aff = subfield.form.affiliation.data 

34 if subfield.form.orcid_id.data != "": 

35 orcid_id = subfield.form.orcid_id.data 

36 if author is not None and author != "": 

37 bibjson.add_author(author, affiliation=aff, orcid_id=orcid_id) 

38 

39 # abstract 

40 abstract = form.abstract.data 

41 if abstract is not None and abstract != "": 

42 bibjson.abstract = abstract 

43 

44 # keywords 

45 keywords = form.keywords.data 

46 if keywords is not None and keywords is not []: 

47 if isinstance(keywords, str): 

48 keywords = keywords.split(",") 

49 ks = [k.strip() for k in keywords] 

50 bibjson.set_keywords(ks) 

51 

52 # fulltext 

53 ft = form.fulltext.data 

54 if ft is not None and ft != "": 

55 bibjson.add_url(ft, "fulltext") 

56 

57 # publication year/month 

58 py = form.publication_year.data 

59 pm = form.publication_month.data 

60 if pm is not None and pm != "": 

61 bibjson.month = pm 

62 if py is not None and py != "": 

63 bibjson.year = py 

64 

65 # pissn 

66 pissn = form.pissn.data 

67 if pissn is not None and pissn != "": 

68 bibjson.add_identifier(bibjson.P_ISSN, pissn) 

69 

70 # eissn 

71 eissn = form.eissn.data 

72 if eissn is not None and eissn != "": 

73 bibjson.add_identifier(bibjson.E_ISSN, eissn) 

74 

75 # volume 

76 volume = form.volume.data 

77 if volume is not None and volume != "": 

78 bibjson.volume = volume 

79 

80 # number 

81 number = form.number.data 

82 if number is not None and number != "": 

83 bibjson.number = number 

84 

85 # start date 

86 start = form.start.data 

87 if start is not None and start != "": 

88 bibjson.start_page = start 

89 

90 # end date 

91 end = form.end.data 

92 if end is not None and end != "": 

93 bibjson.end_page = end 

94 

95 # add the journal info if requested 

96 if add_journal_info: 

97 article.add_journal_metadata() 

98 

99 return article 

100 

101 @classmethod 

102 def obj2form(cls, form, article): 

103 from portality.forms.article_forms import AuthorForm 

104 

105 bibjson = article.bibjson() 

106 if bibjson.title: 

107 form.title.data = bibjson.title 

108 doi = bibjson.get_one_identifier("doi") 

109 if doi: 

110 form.doi.data = doi 

111 if bibjson.author: 

112 for i in range(len(form.authors)): 

113 form.authors.pop_entry() 

114 for a in bibjson.author: 

115 author = AuthorForm() 

116 if "name" in a: 

117 author.name = a["name"] 

118 else: 

119 author.name = "" 

120 if "affiliation" in a: 

121 author.affiliation = a["affiliation"] 

122 else: 

123 author.affiliation = "" 

124 if "orcid_id" in a: 

125 author.orcid_id = a["orcid_id"] 

126 else: 

127 author.orcid_id = "" 

128 form.authors.append_entry(author) 

129 

130 if bibjson.keywords: 

131 form.keywords.data = bibjson.keywords 

132 

133 url = bibjson.get_single_url("fulltext") 

134 if url: 

135 form.fulltext.data = url 

136 if bibjson.month: 

137 form.publication_month.data = bibjson.month 

138 if bibjson.year: 

139 form.publication_year.data = bibjson.year 

140 pissn = bibjson.get_identifiers(bibjson.P_ISSN) 

141 if len(pissn) > 0: 

142 form.pissn.data = pissn[0] 

143 eissn = bibjson.get_identifiers(bibjson.E_ISSN) 

144 if len(eissn) > 0: 

145 form.eissn.data = eissn[0] 

146 if bibjson.volume: 

147 form.volume.data = bibjson.volume 

148 if bibjson.number: 

149 form.number.data = bibjson.number 

150 if bibjson.start_page: 

151 form.start.data = bibjson.start_page 

152 if bibjson.end_page: 

153 form.end.data = bibjson.end_page 

154 if bibjson.abstract: 

155 form.abstract.data = bibjson.abstract 

156