From 6685c709e535034771242d5f62c46c142b4ad8bc Mon Sep 17 00:00:00 2001 From: Jon Date: Fri, 20 Dec 2024 23:28:27 -0100 Subject: [PATCH] Fix the feedback not being reported through --- tom_dataproducts/sharing.py | 23 +++++++++++++------ tom_targets/persistent_sharing_serializers.py | 8 +++---- tom_targets/sharing.py | 15 ++++++------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/tom_dataproducts/sharing.py b/tom_dataproducts/sharing.py index 922e586f..ea550166 100644 --- a/tom_dataproducts/sharing.py +++ b/tom_dataproducts/sharing.py @@ -282,21 +282,30 @@ def get_sharing_destination_options(include_download=True): return tuple(choices) -def sharing_feedback_handler(response, request): +def sharing_feedback_converter(response): """ - Handle the response from a sharing request and prepare a message to the user - :return: + Takes a sharing feedback response and returns its error or success message """ try: response.raise_for_status() if 'message' in response.json(): - publish_feedback = response.json()['message'] + feedback_message = response.json()['message'] else: - publish_feedback = "Submitted message succesfully" + feedback_message = "Submitted message succesfully" except AttributeError: - publish_feedback = response['message'] + feedback_message = response['message'] except Exception: - publish_feedback = f"ERROR: Returned Response code {response.status_code} with content: {response.content}" + feedback_message = f"ERROR: Returned Response code {response.status_code} with content: {response.content}" + + return feedback_message + + +def sharing_feedback_handler(response, request): + """ + Handle the response from a sharing request and prepare a message to the user + :return: + """ + publish_feedback = sharing_feedback_converter(response) if "ERROR" in publish_feedback.upper(): messages.error(request, publish_feedback) else: diff --git a/tom_targets/persistent_sharing_serializers.py b/tom_targets/persistent_sharing_serializers.py index 1d787980..9a72b516 100644 --- a/tom_targets/persistent_sharing_serializers.py +++ b/tom_targets/persistent_sharing_serializers.py @@ -19,10 +19,10 @@ class Meta: def create(self, validated_data): shared_existing_data = validated_data.pop('share_existing_data', None) if shared_existing_data: - try: - share_target_and_all_data(validated_data['destination'], validated_data['target']) - except Exception as e: + sharing_feedback = share_target_and_all_data(validated_data['destination'], validated_data['target']) + if 'ERROR' in sharing_feedback.upper(): raise serializers.ValidationError( - f"Failed to share existing data of target {validated_data['target'].name}: {repr(e)}" + f"Failed to share existing data of target {validated_data['target'].name}: {sharing_feedback}" ) + return super().create(validated_data) diff --git a/tom_targets/sharing.py b/tom_targets/sharing.py index 4bbd5037..ebeba11f 100644 --- a/tom_targets/sharing.py +++ b/tom_targets/sharing.py @@ -5,7 +5,8 @@ from tom_targets.serializers import TargetSerializer from tom_targets.models import PersistentShare -from tom_dataproducts.sharing import check_for_share_safe_datums, share_data_with_tom, get_destination_target +from tom_dataproducts.sharing import (check_for_share_safe_datums, share_data_with_tom, + get_destination_target, sharing_feedback_converter) from tom_dataproducts.models import ReducedDatum from tom_dataproducts.alertstreams.hermes import publish_to_hermes, BuildHermesMessage @@ -27,17 +28,17 @@ def share_target_and_all_data(share_destination, target): message = BuildHermesMessage(title=f"Setting up continuous sharing for {target.name} from " f"{tom_name}.", authors=sharing.get('hermes', {}).get('DEFAULT_AUTHORS', None), - submitter=tom_name, + submitter='', message='', topic=hermes_topic ) - response = publish_to_hermes(message, filtered_reduced_datums) - response.raise_for_status() + return sharing_feedback_converter(publish_to_hermes(message, filtered_reduced_datums)) else: response = share_target_with_tom(share_destination, {'target': target}) - response.raise_for_status() - response = share_data_with_tom(share_destination, None, target_id=target.id) - response.raise_for_status() + response_feedback = sharing_feedback_converter(response) + if 'ERROR' in response_feedback.upper(): + return response_feedback + return sharing_feedback_converter(share_data_with_tom(share_destination, None, target_id=target.id)) def continuous_share_data(target, reduced_datums):