Skip to content

Commit a99d60d

Browse files
committed
Merge branch 'hotfix/19.23.1'
2 parents 6c81f5c + 4fff006 commit a99d60d

File tree

2 files changed

+77
-30
lines changed

2 files changed

+77
-30
lines changed

osf_tests/test_elastic_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ def tearDownClass(cls):
13071307

13081308
def setUp(self):
13091309
super(TestSearchMigration, self).setUp()
1310-
populate_institutions('test')
1310+
populate_institutions(default_args=True)
13111311
self.es = search.search_engine.CLIENT
13121312
search.delete_index(settings.ELASTIC_INDEX)
13131313
search.create_index(settings.ELASTIC_INDEX)

scripts/populate_institutions.py

Lines changed: 76 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*-
33
"""Populate development database with Institution fixtures."""
44

5+
import argparse
56
import logging
67
import sys
78
import urllib
@@ -18,10 +19,19 @@
1819
logger = logging.getLogger(__name__)
1920
logging.basicConfig(level=logging.INFO)
2021

21-
ENVS = ['prod', 'stage', 'stage2', 'test']
22+
ENVS = ['prod', 'stage', 'stage2', 'stage3', 'test', ]
23+
24+
# TODO: Store only the Entity IDs in OSF DB and move the URL building process to CAS
2225
SHIBBOLETH_SP_LOGIN = '{}/Shibboleth.sso/Login?entityID={{}}'.format(settings.CAS_SERVER_URL)
2326
SHIBBOLETH_SP_LOGOUT = '{}/Shibboleth.sso/Logout?return={{}}'.format(settings.CAS_SERVER_URL)
2427

28+
# Using optional args instead of positional ones to explicitly set them
29+
parser = argparse.ArgumentParser()
30+
parser.add_argument('-e', '--env', help='select the server: prod, test, stage, stage2 or stage3')
31+
group = parser.add_mutually_exclusive_group()
32+
group.add_argument('-i', '--ids', nargs='+', help='select the institution(s) to add or update')
33+
group.add_argument('-a', '--all', action='store_true', help='add or update all institutions')
34+
2535

2636
def encode_uri_component(val):
2737
return urllib.quote(val, safe='~()*!.\'')
@@ -44,11 +54,45 @@ def update_or_create(inst_data):
4454
return inst, True
4555

4656

47-
def main(env):
48-
INSTITUTIONS = []
57+
def main(default_args=False):
58+
59+
if default_args:
60+
args = parser.parse_args(['--env', 'test', '--all'])
61+
else:
62+
args = parser.parse_args()
63+
64+
server_env = args.env
65+
update_ids = args.ids
66+
update_all = args.all
67+
68+
if not server_env or server_env not in ENVS:
69+
logger.error('A valid environment must be specified: {}'.format(ENVS))
70+
sys.exit(1)
71+
institutions = INSTITUTIONS[server_env]
72+
73+
if not update_all and not update_ids:
74+
logger.error('Nothing to update or create. Please either specify a list of institutions '
75+
'using --ids or run for all with --all')
76+
sys.exit(1)
77+
elif update_all:
78+
institutions_to_update = institutions
79+
else:
80+
institutions_to_update = [inst for inst in institutions if inst['_id'] in update_ids]
81+
diff_list = list(set(update_ids) - set([inst['_id'] for inst in institutions_to_update]))
82+
if diff_list:
83+
logger.error('One or more institution ID(s) provided via -i or --ids do not match any '
84+
'existing records: {}.'.format(diff_list))
85+
sys.exit(1)
86+
87+
with transaction.atomic():
88+
for inst_data in institutions_to_update:
89+
update_or_create(inst_data)
90+
for extra_inst in Institution.objects.exclude(_id__in=[x['_id'] for x in institutions]):
91+
logger.warn('Extra Institution : {} - {}'.format(extra_inst._id, extra_inst.name))
92+
4993

50-
if env == 'prod':
51-
INSTITUTIONS = [
94+
INSTITUTIONS = {
95+
'prod': [
5296
{
5397
'_id': 'a2jlab',
5498
'name': 'Access to Justice Lab',
@@ -529,7 +573,7 @@ def main(env):
529573
{
530574
'_id': 'ou',
531575
'name': 'The University of Oklahoma',
532-
'description': '<a href="https://www.ou.edu">The University of Oklahoma</a> | <a href="https://www.ou.edu/research-norman">Office of the Vice President for Research</a> | <a href="https://libraries.ou.edu">University Libraries</a>',
576+
'description': '<a href="https://www.ou.edu">The University of Oklahoma</a> | <a href="https://libraries.ou.edu">University Libraries</a>',
533577
'banner_name': 'ou-banner.png',
534578
'logo_name': 'ou-shield.png',
535579
'login_url': SHIBBOLETH_SP_LOGIN.format(encode_uri_component('https://shib.ou.edu/idp/shibboleth')),
@@ -792,9 +836,8 @@ def main(env):
792836
'email_domains': [],
793837
'delegation_protocol': 'saml-shib',
794838
},
795-
]
796-
if env == 'stage':
797-
INSTITUTIONS = [
839+
],
840+
'stage': [
798841
{
799842
'_id': 'cos',
800843
'name': 'Center For Open Science [Stage]',
@@ -842,9 +885,8 @@ def main(env):
842885
'email_domains': ['yahoo.com'],
843886
'delegation_protocol': '',
844887
},
845-
]
846-
if env == 'stage2':
847-
INSTITUTIONS = [
888+
],
889+
'stage2': [
848890
{
849891
'_id': 'cos',
850892
'name': 'Center For Open Science [Stage2]',
@@ -857,9 +899,22 @@ def main(env):
857899
'email_domains': ['cos.io'],
858900
'delegation_protocol': '',
859901
},
860-
]
861-
elif env == 'test':
862-
INSTITUTIONS = [
902+
],
903+
'stage3': [
904+
{
905+
'_id': 'cos',
906+
'name': 'Center For Open Science [Stage3]',
907+
'description': 'Center for Open Science [Stage3]',
908+
'banner_name': 'cos-banner.png',
909+
'logo_name': 'cos-shield.png',
910+
'login_url': None,
911+
'logout_url': None,
912+
'domains': ['staging3-osf.cos.io'],
913+
'email_domains': ['cos.io'],
914+
'delegation_protocol': '',
915+
},
916+
],
917+
'test': [
863918
{
864919
'_id': 'a2jlab',
865920
'name': 'Access to Justice Lab [Test]',
@@ -1340,7 +1395,7 @@ def main(env):
13401395
{
13411396
'_id': 'ou',
13421397
'name': 'The University of Oklahoma [Test]',
1343-
'description': '<a href="https://www.ou.edu">The University of Oklahoma</a> | <a href="https://www.ou.edu/research-norman">Office of the Vice President for Research</a> | <a href="https://libraries.ou.edu">University Libraries</a>',
1398+
'description': '<a href="https://www.ou.edu">The University of Oklahoma</a> | <a href="https://libraries.ou.edu">University Libraries</a>',
13441399
'banner_name': 'ou-banner.png',
13451400
'logo_name': 'ou-shield.png',
13461401
'login_url': SHIBBOLETH_SP_LOGIN.format(encode_uri_component('https://shib.ou.edu/idp/shibboleth')),
@@ -1603,19 +1658,11 @@ def main(env):
16031658
'email_domains': [],
16041659
'delegation_protocol': 'saml-shib',
16051660
},
1606-
]
1607-
1608-
init_app(routes=False)
1609-
with transaction.atomic():
1610-
for inst_data in INSTITUTIONS:
1611-
update_or_create(inst_data)
1612-
for extra_inst in Institution.objects.exclude(_id__in=[x['_id'] for x in INSTITUTIONS]):
1613-
logger.warn('Extra Institution : {} - {}'.format(extra_inst._id, extra_inst.name))
1661+
],
1662+
}
16141663

16151664

16161665
if __name__ == '__main__':
1617-
env = str(sys.argv[1]).lower() if len(sys.argv) == 2 else None
1618-
if env not in ENVS:
1619-
print('An environment must be specified : {}', ENVS)
1620-
sys.exit(1)
1621-
main(env)
1666+
1667+
init_app(routes=False)
1668+
main(default_args=False)

0 commit comments

Comments
 (0)