forked from ourresearch/jump-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepared_demo_publisher.py
53 lines (39 loc) · 1.89 KB
/
prepared_demo_publisher.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
from app import db
from app import logger
from institution import Institution
from package import Package, clone_demo_package
def get_or_create_placeholder_institution():
display_name = 'placeholder, see prepared_demo_publisher.py'
institution = Institution.query.filter(Institution.display_name == display_name).scalar()
if not institution:
institution = Institution(
display_name=display_name,
is_consortium=False,
is_demo_institution=True
)
db.session.add(institution)
return institution
def prepare_publishers():
institution = get_or_create_placeholder_institution()
prepared_publishers = Package.query.filter(Package.institution_id == institution.id).all()
num_prepared = len(prepared_publishers)
to_prepare = 10 - num_prepared
logger.info('Found {} prepared demo publishers. Creating {}.'.format(num_prepared, to_prepare))
if to_prepare > 0:
for i in range(0, to_prepare):
new_publisher = clone_demo_package(institution)
logger.info('Created demo publisher {} ({}).'.format(new_publisher.package_id, institution.id))
def get_demo_publisher(institution, use_prepared=True):
if use_prepared:
placeholder_institution = get_or_create_placeholder_institution()
prepared_publisher = Package.query.filter(Package.institution_id == placeholder_institution.id).first()
if prepared_publisher:
prepared_publisher.institution_id = institution.id
logger.info('Got prepared demo publisher {} ({}).'.format(prepared_publisher.package_id, institution.id))
return prepared_publisher
new_publisher = clone_demo_package(institution)
logger.info('Created demo publisher {} ({}).'.format(new_publisher.package_id, institution.id))
return new_publisher
if __name__ == "__main__":
prepare_publishers()
db.session.commit()