diff --git a/docker_run.sh b/docker_run.sh index d34a3e8..72a6539 100755 --- a/docker_run.sh +++ b/docker_run.sh @@ -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 diff --git a/example_data.py b/example_data.py index f9b4664..b81cf4f 100644 --- a/example_data.py +++ b/example_data.py @@ -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() diff --git a/pb/__init__.py b/pb/__init__.py index 130b432..34f2685 100644 --- a/pb/__init__.py +++ b/pb/__init__.py @@ -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 diff --git a/tests/test_sanity.py b/tests/test_sanity.py index f0602d9..c5e51f5 100644 --- a/tests/test_sanity.py +++ b/tests/test_sanity.py @@ -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