Skip to content

Commit

Permalink
Fix the feedback not being reported through
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon committed Dec 21, 2024
1 parent 73f83b7 commit 6685c70
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
23 changes: 16 additions & 7 deletions tom_dataproducts/sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions tom_targets/persistent_sharing_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
)

return super().create(validated_data)
15 changes: 8 additions & 7 deletions tom_targets/sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down

0 comments on commit 6685c70

Please sign in to comment.