Adds environment variable option for loading sponsors via Docker. Prevents duplicate sponsors from being loaded.

This commit is contained in:
Aaron Louie 2020-08-14 09:39:05 -04:00
parent 0e4cd7d130
commit 45a99d296d
4 changed files with 25 additions and 3 deletions

View File

@ -13,6 +13,16 @@ if [ "$UPGRADE_DB" = "true" ]; then
pipenv run flask db upgrade
fi
if [ "$RESET_DB" = "true" ]; then
echo 'Clearing database and loading example data...'
pipenv run flask load-example-data
fi
if [ "$LOAD_EXAMPLE_SPONSORS" = "true" ]; then
echo 'Loading example data...'
pipenv run flask load-example-sponsors
fi
if [ "$APPLICATION_ROOT" = "/" ]; then
pipenv run gunicorn --bind 0.0.0.0:$PORT0 wsgi:app
else

View File

@ -34,9 +34,14 @@ class ExampleDataLoader:
# row[3]: SP_TYPE
first_line = False
elif int(row[0] or -1) != -1:
new_sponsor = Sponsor(SPONSOR_ID=int(row[0]), SP_NAME=row[1], SP_MAILING_ADDRESS=row[2], SP_TYPE=row[3])
new_sponsor.SP_TYPE_GROUP_NAME = Sponsor.get_type_group_name(new_sponsor.SP_TYPE)
sponsors.append(new_sponsor)
sponsor_id = int(row[0])
is_duplicate = session.query(Sponsor).filter(Sponsor.SPONSOR_ID == sponsor_id).count() > 0
# Make sure we're not creating duplicates
if not is_duplicate:
new_sponsor = Sponsor(SPONSOR_ID=sponsor_id, SP_NAME=row[1], SP_MAILING_ADDRESS=row[2], SP_TYPE=row[3])
new_sponsor.SP_TYPE_GROUP_NAME = Sponsor.get_type_group_name(new_sponsor.SP_TYPE)
sponsors.append(new_sponsor)
session.add_all(sponsors)
session.commit()

View File

@ -403,6 +403,7 @@ def _update_study(study, form):
study.STUDYID = max_id + 1
else:
# Delete existing required documents for this study.
db.session.query(RequiredDocument).filter(RequiredDocument.STUDYID == study.STUDYID).delete()
study.TITLE = form.TITLE.data

View File

@ -85,6 +85,12 @@ class Sanity_Check_Test(unittest.TestCase):
self.assertEqual(num_docs_before, num_docs_after)
self.assertEqual(num_studies_before, num_studies_after)
def test_add_sponsors(self):
"""Load sponsors twice in a row to make sure duplicates aren't created."""
self.load_sponsors()
self.load_sponsors()
def test_add_and_edit_study_sponsor(self):
"""Add and edit a study sponsor"""
num_sponsors = 5