-
Notifications
You must be signed in to change notification settings - Fork 16
/
bll_article.py
168 lines (128 loc) · 5.43 KB
/
bll_article.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from doajtest.fixtures import ArticleFixtureFactory
from portality.bll import exceptions
from portality.lib.dates import FMT_DATETIME_STD
from portality.models import Article
from datetime import datetime
class BLLArticleMockFactory(object):
@classmethod
def merge_mock(cls, article):
pass
@classmethod
def pull_mock(cls, id):
return Article
@classmethod
def doi_or_fulltext_updated(cls, doi_updated, ft_updated):
result = doi_updated and ft_updated
def mock(new_article, update_id):
return result
return mock
@classmethod
def discover_duplicates(cls, doi_duplicates=0, fulltext_duplicates=0, overlap=0):
if overlap > doi_duplicates or overlap > fulltext_duplicates:
raise Exception("overlap must be the same as or less than either of doi_duplicates or fulltext_duplicates")
idents = []
# first make duplicate records for the total number of desired dois
for i in range(doi_duplicates):
idents.append({"doi_domain" : True, "doi" : "10.1234/abc/1", "fulltext" : "http://example.com/unique/" + str(i)})
for i in range(overlap):
idents[i]["fulltext"] = "http://example.com/1"
idents[i]["fulltext_domain"] = True
remaining_fulltexts = fulltext_duplicates - overlap
for i in range(remaining_fulltexts):
idents.append({"fulltext_domain" : True, "doi" : "10.1234/unique/" + str(i), "fulltext" : "http://example.com/1"})
possible_duplicates = {"doi" : [], "fulltext" : []}
for i, ident in enumerate(idents):
source = ArticleFixtureFactory.make_article_source(eissn="1234-5678", pissn="9876-5432", doi=ident["doi"], fulltext=["fulltext"])
article = Article(**source)
article.set_id()
article.data["last_updated"] = datetime.fromtimestamp(i * 100000).strftime(FMT_DATETIME_STD)
if "doi_domain" in ident:
possible_duplicates["doi"].append(article)
if "fulltext_domain" in ident:
possible_duplicates["fulltext"].append(article)
if len(possible_duplicates["doi"]) == 0:
del possible_duplicates["doi"]
if len(possible_duplicates["fulltext"]) == 0:
del possible_duplicates["fulltext"]
def mock(article, owner=None, results_per_match_type=10):
return possible_duplicates
return mock
@classmethod
def is_legitimate_owner(cls, legit=None, legit_on_issn=None):
def mock(*arg, **kwarg):
if legit is not None:
return legit
if legit_on_issn is not None:
article = arg[0]
issns = article.bibjson().issns()
for issn in issns:
if issn in legit_on_issn:
return True
return False
return False
return mock
@classmethod
def issn_ownership_status(cls, owned, shared, unowned, unmatched):
def mock(*arg, **kwarg):
return owned, shared, unowned, unmatched
return mock
@classmethod
def get_duplicate(cls, given_article_id = None, return_none=False, eissn=None, pissn=None, doi=None, fulltext=None, merge_duplicate=False):
article = None
if not merge_duplicate:
if given_article_id == "exception":
raise exceptions.ArticleMergeConflict()
if not return_none:
source = ArticleFixtureFactory.make_article_source(eissn=eissn, pissn=pissn, doi=doi, fulltext=fulltext)
article = Article(**source)
article.set_id(given_article_id)
def mock(*args, **kwargs):
if merge_duplicate:
raise exceptions.ArticleMergeConflict()
supplied_article = args[0]
if given_article_id is not None:
if given_article_id == supplied_article.id:
return article
else:
return article
return mock
@classmethod
def batch_create(cls, *args, **kwargs):
raise RuntimeError("Batch create unsuccessful.")
@classmethod
def has_permissions(cls, has_permission):
def mock(*args, **kwargs):
return has_permission
return mock
@classmethod
def _prepare_update_admin(cls, duplicate_result, update_article_id):
if update_article_id is None:
result = -1
elif duplicate_result == "itself" or duplicate_result == "none":
result = 1
else:
result = 0
def mock(*args, **kwargs):
print(duplicate_result, update_article_id)
if result == -1:
raise exceptions.ConfigurationException
if result == 0:
raise exceptions.DuplicateArticleException("duplicate result is 'different'")
return 1
return mock
@classmethod
def _prepare_update_publisher(cls, duplicate_result, has_ft_doi_changed):
if duplicate_result is None:
result = 0
elif duplicate_result == "itself":
if has_ft_doi_changed:
result = -1
else:
result = 1
elif duplicate_result == "different":
result = -1
def mock(*args, **kwargs):
if result == -1:
raise exceptions.DuplicateArticleException()
return result
return mock