diff --git a/backend/composer/models.py b/backend/composer/models.py index 2b1fcd41..cd88c66d 100644 --- a/backend/composer/models.py +++ b/backend/composer/models.py @@ -634,7 +634,10 @@ def npo_approved(self, *args, **kwargs): @transition( field=state, - source=CSState.NPO_APPROVED, + source=[ + CSState.NPO_APPROVED, + CSState.INVALID + ], target=CSState.EXPORTED, conditions=[ConnectivityStatementStateService.is_valid], permission=ConnectivityStatementStateService.has_permission_to_transition_to_exported, diff --git a/backend/composer/services/cs_ingestion/helpers/notes_helper.py b/backend/composer/services/cs_ingestion/helpers/notes_helper.py index f89c2249..a8079e85 100644 --- a/backend/composer/services/cs_ingestion/helpers/notes_helper.py +++ b/backend/composer/services/cs_ingestion/helpers/notes_helper.py @@ -26,3 +26,9 @@ def create_invalid_note(connectivity_statement: ConnectivityStatement, note: str type=NoteType.ALERT, note=f"Invalidated due to the following reason(s): {note}" ) + + +def do_transition_to_exported(connectivity_statement: ConnectivityStatement): + system_user = User.objects.get(username="system") + connectivity_statement.exported(by=system_user) + connectivity_statement.save() diff --git a/backend/composer/services/cs_ingestion/helpers/statement_helper.py b/backend/composer/services/cs_ingestion/helpers/statement_helper.py index fafd734f..828b4ad5 100644 --- a/backend/composer/services/cs_ingestion/helpers/statement_helper.py +++ b/backend/composer/services/cs_ingestion/helpers/statement_helper.py @@ -12,7 +12,7 @@ from composer.services.cs_ingestion.helpers.getters import get_sex, get_circuit_type, get_functional_circuit_role, \ get_phenotype, get_projection_phenotype from composer.services.cs_ingestion.helpers.notes_helper import do_transition_to_invalid_with_note, create_invalid_note, \ - add_ingestion_system_note + add_ingestion_system_note, do_transition_to_exported from composer.services.cs_ingestion.models import ValidationErrors @@ -35,11 +35,11 @@ def create_or_update_connectivity_statement(statement: Dict, sentence: Sentence, reference_uri=reference_uri, defaults=defaults ) - if not created: + if not created: if has_changes(connectivity_statement, statement, defaults): - ConnectivityStatement.objects.filter(reference_uri=reference_uri).update(**defaults) - fields_to_refresh = [field for field in defaults.keys() if field != 'state'] - connectivity_statement.refresh_from_db(fields=fields_to_refresh) + defaults_without_state = {field: value for field, value in defaults.items() if field != 'state'} + ConnectivityStatement.objects.filter(reference_uri=reference_uri).update(**defaults_without_state) + connectivity_statement = ConnectivityStatement.objects.get(reference_uri=reference_uri) add_ingestion_system_note(connectivity_statement) validation_errors = statement.get(VALIDATION_ERRORS, ValidationErrors()) @@ -50,6 +50,9 @@ def create_or_update_connectivity_statement(statement: Dict, sentence: Sentence, do_transition_to_invalid_with_note(connectivity_statement, error_message) else: create_invalid_note(connectivity_statement, error_message) + else: + if connectivity_statement.state != CSState.EXPORTED: + do_transition_to_exported(connectivity_statement) update_many_to_many_fields(connectivity_statement, statement, update_anatomical_entities) statement[STATE] = connectivity_statement.state