-
Notifications
You must be signed in to change notification settings - Fork 16
/
application.py
412 lines (342 loc) · 13.1 KB
/
application.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from copy import deepcopy
from portality import constants
from portality.core import app
from portality.lib import es_data_mapping
from portality.models.v2 import shared_structs
from portality.models.v2.journal import JournalLikeObject, Journal
from portality.lib.coerce import COERCE_MAP
from portality.dao import DomainObject
APPLICATION_STRUCT = {
"objects": [
"admin", "index"
],
"structs": {
"admin": {
"fields": {
"current_journal": {"coerce": "unicode"},
"related_journal": {"coerce": "unicode"},
"application_status": {"coerce": "unicode"},
"date_applied": {"coerce": "utcdatetime"},
"application_type": {
"coerce": "unicode",
"allowed_values": [
constants.APPLICATION_TYPE_NEW_APPLICATION,
constants.APPLICATION_TYPE_UPDATE_REQUEST
]
}
}
},
"index": {
"fields": {
"application_type": {"coerce": "unicode"}
}
}
}
}
# ~~Application:Model~~
class Application(JournalLikeObject):
__type__ = "application"
__SEAMLESS_STRUCT__ = [
shared_structs.JOURNAL_BIBJSON,
shared_structs.SHARED_JOURNAL_LIKE,
APPLICATION_STRUCT
]
__SEAMLESS_COERCE__ = COERCE_MAP
def __init__(self, **kwargs):
# FIXME: hack, to deal with ES integration layer being improperly abstracted
if "_source" in kwargs:
kwargs = kwargs["_source"]
super(Application, self).__init__(raw=kwargs)
if self.application_type is None:
if self.current_journal:
self.set_is_update_request(True)
else:
self.set_is_update_request(False)
@classmethod
def get_by_owner(cls, owner):
q = SuggestionQuery(owner=owner)
result = cls.query(q=q.query())
records = [cls(**r.get("_source")) for r in result.get("hits", {}).get("hits", [])]
return records
@classmethod
def delete_selected(cls, email=None, statuses=None):
q = SuggestionQuery(email=email, statuses=statuses)
r = cls.delete_by_query(q.query())
@classmethod
def list_by_status(cls, status):
q = StatusQuery(status)
return cls.iterate(q=q.query())
@classmethod
def find_latest_by_current_journal(cls, journal_id):
q = CurrentJournalQuery(journal_id)
results = cls.q2obj(q=q.query())
if len(results) > 0:
return results[0]
return None
@classmethod
def find_all_by_related_journal(cls, journal_id):
q = RelatedJournalQuery(journal_id, size=1000)
return cls.q2obj(q=q.query())
@classmethod
def assignment_to_editor_groups(cls, egs):
q = AssignedEditorGroupsQuery([eg.name for eg in egs])
res = cls.query(q.query())
buckets = res.get("aggregations", {}).get("editor_groups", {}).get("buckets", [])
assignments = {}
for b in buckets:
assignments[b.get("key")] = b.get("doc_count")
return assignments
def mappings(self):
return es_data_mapping.create_mapping(self.__seamless_struct__.raw, MAPPING_OPTS)
@property
def application_type(self):
return self.__seamless__.get_single("admin.application_type")
@application_type.setter
def application_type(self, val):
self.__seamless__.set_with_struct("admin.application_type", val)
def set_is_update_request(self, val):
application_type = constants.APPLICATION_TYPE_UPDATE_REQUEST if val is True else constants.APPLICATION_TYPE_NEW_APPLICATION
self.application_type = application_type
@property
def current_journal(self):
return self.__seamless__.get_single("admin.current_journal")
def set_current_journal(self, journal_id):
# anything that has a current journal is, by definition, an update request
self.set_is_update_request(True)
self.__seamless__.set_with_struct("admin.current_journal", journal_id)
def remove_current_journal(self):
self.__seamless__.delete("admin.current_journal")
@property
def related_journal(self):
return self.__seamless__.get_single("admin.related_journal")
def set_related_journal(self, journal_id):
self.__seamless__.set_with_struct("admin.related_journal", journal_id)
def remove_related_journal(self):
self.__seamless__.delete("admin.related_journal")
@property
def related_journal_object(self):
if self.related_journal:
return Journal.pull(self.related_journal)
return None
@property
def application_status(self):
return self.__seamless__.get_single("admin.application_status")
def set_application_status(self, val):
self.__seamless__.set_with_struct("admin.application_status", val)
@property
def date_applied(self):
return self.__seamless__.get_single("admin.date_applied")
@date_applied.setter
def date_applied(self, val):
self.__seamless__.set_with_struct("admin.date_applied", val)
def _sync_owner_to_journal(self):
if self.current_journal is None:
return
from portality.models.v2.journal import Journal
cj = Journal.pull(self.current_journal)
if cj is not None and cj.owner != self.owner:
cj.set_owner(self.owner)
cj.save(sync_owner=False)
def _generate_index(self):
super(Application, self)._generate_index()
# index_record_type = None
# if self.application_type == constants.APPLICATION_TYPE_NEW_APPLICATION:
# if self.application_status in [constants.APPLICATION_STATUS_REJECTED, constants.APPLICATION_STATUS_ACCEPTED]:
# index_record_type = constants.INDEX_RECORD_TYPE_NEW_APPLICATION_FINISHED
# else:
# index_record_type = constants.INDEX_RECORD_TYPE_NEW_APPLICATION_UNFINISHED
# elif self.application_type == constants.APPLICATION_TYPE_UPDATE_REQUEST:
# if self.application_status in [constants.APPLICATION_STATUS_REJECTED, constants.APPLICATION_STATUS_ACCEPTED]:
# index_record_type = constants.INDEX_RECORD_TYPE_UPDATE_REQUEST_FINISHED
# else:
# index_record_type = constants.INDEX_RECORD_TYPE_UPDATE_REQUEST_UNFINISHED
# if index_record_type is not None:
# self.__seamless__.set_with_struct("index.application_type", index_record_type)
# FIXME: Temporary partial reversion of an indexing change (this index.application_type data is still being used
# in applications search)
if self.current_journal is not None:
self.__seamless__.set_with_struct("index.application_type", "update request")
elif self.application_status in [constants.APPLICATION_STATUS_ACCEPTED, constants.APPLICATION_STATUS_REJECTED]:
self.__seamless__.set_with_struct("index.application_type", "finished application/update")
else:
self.__seamless__.set_with_struct("index.application_type", "new application")
def prep(self, is_update=True):
self._generate_index()
if is_update:
self.set_last_updated()
def save(self, sync_owner=True, **kwargs):
self.prep()
self.verify_against_struct()
if sync_owner:
self._sync_owner_to_journal()
return super(Application, self).save(**kwargs)
#########################################################
## DEPRECATED METHODS
@property
def suggested_on(self):
return self.date_applied
@suggested_on.setter
def suggested_on(self, val):
self.date_applied = val
@property
def suggester(self):
owner = self.owner
if owner is None:
return None
from portality.models import Account
oacc = Account.pull(owner)
if oacc is None:
return None
return {
"name": owner,
"email": oacc.email
}
class DraftApplication(Application):
__type__ = "draft_application"
__SEAMLESS_APPLY_STRUCT_ON_INIT__ = False
__SEAMLESS_CHECK_REQUIRED_ON_INIT__ = False
class AllPublisherApplications(DomainObject):
__type__ = "draft_application,application"
MAPPING_OPTS = {
"dynamic": None,
"coerces": app.config["DATAOBJ_TO_MAPPING_DEFAULTS"],
"exceptions": {
"admin.notes.note": {
"type": "text",
"index": False,
# "include_in_all": False # Removed in es6 fixme: do we need to look at copy_to for the mapping?
}
}
}
class SuggestionQuery(object):
_base_query = {"track_total_hits": True, "query": {"bool": {"must": []}}}
_email_term = {"term": {"admin.applicant.email.exact": "<email address>"}}
_status_terms = {"terms": {"admin.application_status.exact": ["<list of statuses>"]}}
_owner_term = {"term": {"admin.owner.exact": "<the owner id>"}}
def __init__(self, email=None, statuses=None, owner=None):
self.email = email
self.owner = owner
if statuses:
self.statuses = statuses
else:
self.statuses = []
def query(self):
q = deepcopy(self._base_query)
if self.email:
et = deepcopy(self._email_term)
et["term"]["admin.applicant.email.exact"] = self.email
q["query"]["bool"]["must"].append(et)
if self.statuses and len(self.statuses) > 0:
st = deepcopy(self._status_terms)
st["terms"]["admin.application_status.exact"] = self.statuses
q["query"]["bool"]["must"].append(st)
if self.owner is not None:
ot = deepcopy(self._owner_term)
ot["term"]["admin.owner.exact"] = self.owner
q["query"]["bool"]["must"].append(ot)
return q
class OwnerStatusQuery(object):
base_query = {
"track_total_hits": True,
"query": {
"bool": {
"must": []
}
},
"sort": [
{"created_date": "desc"}
],
"size": 10
}
def __init__(self, owner, statuses, size=10):
self._query = deepcopy(self.base_query)
owner_term = {"match": {"owner": owner}}
self._query["query"]["bool"]["must"].append(owner_term)
status_term = {"terms": {"admin.application_status.exact": statuses}}
self._query["query"]["bool"]["must"].append(status_term)
self._query["size"] = size
def query(self):
return self._query
class StatusQuery(object):
def __init__(self, statuses):
if not isinstance(statuses, list):
statuses = [statuses]
self.statuses = statuses
def query(self):
return {
"track_total_hits": True,
"query": {
"bool": {
"must": [
{"terms": {"admin.application_status.exact": self.statuses}}
]
}
}
}
class CurrentJournalQuery(object):
def __init__(self, journal_id, size=1):
self.journal_id = journal_id
self.size = size
def query(self):
return {
"track_total_hits": True,
"query": {
"bool": {
"must": [
{"term": {"admin.current_journal.exact": self.journal_id}}
]
}
},
"sort": [
{"created_date": {"order": "desc"}}
],
"size": self.size
}
class RelatedJournalQuery(object):
def __init__(self, journal_id, size=1):
self.journal_id = journal_id
self.size = size
def query(self):
return {
"track_total_hits": True,
"query": {
"bool": {
"must": [
{"term": {"admin.related_journal.exact": self.journal_id}}
]
}
},
"sort": [
{"created_date": {"order": "asc"}}
],
"size": self.size
}
class AssignedEditorGroupsQuery(object):
"""
~~->$AssignedEditorGroups:Query~~
~~^->Elasticsearch:Technology~~
"""
def __init__(self, editor_groups, application_type_name="new application"):
self.editor_groups = editor_groups
self.application_type_name = application_type_name
def query(self):
return {
"query": {
"bool": {
"must": [
{"terms": {"admin.editor_group.exact": self.editor_groups}},
{"term": {"index.application_type.exact": self.application_type_name}}
]
}
},
"size": 0,
"aggs": {
"editor_groups": {
"terms": {
"field": "admin.editor_group.exact",
"size": len(self.editor_groups) or 1,
"order": {"_term": "asc"}
}
}
}
}