From bd79946ca9a7709085fc8e98ffee67a3be2b4be2 Mon Sep 17 00:00:00 2001 From: Vilem Ded Date: Mon, 29 Mar 2021 11:11:49 +0200 Subject: [PATCH] cohort import - if exists -> update --- core/importer/datasets_importer.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/core/importer/datasets_importer.py b/core/importer/datasets_importer.py index 252a9e0a..e019c521 100644 --- a/core/importer/datasets_importer.py +++ b/core/importer/datasets_importer.py @@ -498,17 +498,29 @@ def _process_study(study): ethics_approval_notes = study.get('ethics_approval_notes', '') url = study.get('url', '') - cohort, _ = Cohort.objects.get_or_create( - ethics_confirmation=has_ethics_approval, - comments=description, - title=name, - ) + try: + cohort = Cohort.objects.get(title=name) + except Cohort.DoesNotExist: + cohort = None + + if cohort: + msg = f"Cohort with title '{safe_name}' already found. All fields are going to be updated." + self.logger.warning(msg) + else: + cohort = Cohort.objects.create(title=name) + + cohort.description = description + cohort.ethics_confirmation = has_ethics_approval + cohort.ethics_notes = ethics_approval_notes + cohort.cohort_web_page = url cohort.save() + cohort.updated = True local_custodians, local_personnel, external_contacts = self.process_contacts(study.get("contacts", [])) cohort.owners.set(external_contacts) cohort.save() + cohort.updated = True msg = f"Cohort '{safe_name}' imported successfully. Will try to link it to the data declaration..." self.logger.info(msg)