From e6e7e9567a503e181dfd95f91d44c8cbbd8cbf69 Mon Sep 17 00:00:00 2001 From: Joseph Chatelain Date: Wed, 5 Jul 2023 17:05:31 -0700 Subject: [PATCH 01/25] set up ui buttons for sharing --- tom_dataproducts/views.py | 10 ---- .../tom_targets/partials/target_buttons.html | 5 +- tom_targets/urls.py | 1 + tom_targets/views.py | 58 +++++++++++++++++++ 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/tom_dataproducts/views.py b/tom_dataproducts/views.py index b9d489795..314519b07 100644 --- a/tom_dataproducts/views.py +++ b/tom_dataproducts/views.py @@ -338,16 +338,6 @@ def post(self, request, *args, **kwargs): messages.success(self.request, publish_feedback) return redirect(reverse('tom_targets:detail', kwargs={'pk': request.POST.get('target')})) - # if data_share_form.is_valid(): - # form_data = data_share_form.cleaned_data - # - # return redirect(reverse('tom_targets:detail', kwargs={'pk': request.POST.get('target')})) - # else: - # # messages.error(self.request, 'TOM-TOM sharing is not yet supported.') - # response = self.share_with_tom(share_destination, product) - # return redirect(reverse('tom_targets:detail', kwargs={'pk': request.POST.get('target')})) - # # response = self.share_with_tom(share_destination, product) - class DataProductGroupDetailView(DetailView): """ diff --git a/tom_targets/templates/tom_targets/partials/target_buttons.html b/tom_targets/templates/tom_targets/partials/target_buttons.html index 5cd86e4f4..3f5216c37 100644 --- a/tom_targets/templates/tom_targets/partials/target_buttons.html +++ b/tom_targets/templates/tom_targets/partials/target_buttons.html @@ -1,2 +1,3 @@ -Update Target -Delete Target \ No newline at end of file +Update +Delete +Share \ No newline at end of file diff --git a/tom_targets/urls.py b/tom_targets/urls.py index 33e98aa81..09bb8228f 100644 --- a/tom_targets/urls.py +++ b/tom_targets/urls.py @@ -24,6 +24,7 @@ path('name/', TargetNameSearchView.as_view(), name='name-search'), path('/update/', TargetUpdateView.as_view(), name='update'), path('/delete/', TargetDeleteView.as_view(), name='delete'), + path('/share/', TargetUpdateView.as_view(), name='share'), path('/', TargetDetailView.as_view(), name='detail'), path('targetgrouping//delete/', TargetGroupingDeleteView.as_view(), name='delete-group'), path('targetgrouping/create/', TargetGroupingCreateView.as_view(), name='create-group') diff --git a/tom_targets/views.py b/tom_targets/views.py index 500c1e472..d762e7e47 100644 --- a/tom_targets/views.py +++ b/tom_targets/views.py @@ -321,6 +321,64 @@ def get_form(self, *args, **kwargs): return form +# class TargetShareView(FormView): +# """ +# View that handles the sharing of data either through HERMES or with another TOM. +# """ +# +# form_class = DataShareForm +# +# def get_form(self, *args, **kwargs): +# # TODO: Add permissions +# form = super().get_form(*args, **kwargs) +# return form +# +# def form_invalid(self, form): +# """ +# Adds errors to Django messaging framework in the case of an invalid form and redirects to the previous page. +# """ +# # TODO: Format error messages in a more human-readable way +# messages.error(self.request, 'There was a problem sharing your Data: {}'.format(form.errors.as_json())) +# return redirect(form.cleaned_data.get('referrer', '/')) +# +# def post(self, request, *args, **kwargs): +# """ +# Method that handles the POST requests for sharing data. +# Handles Data Products and All the data of a type for a target as well as individual Reduced Datums. +# Submit to Hermes, or Share with TOM (soon). +# """ +# data_share_form = DataShareForm(request.POST, request.FILES) +# +# if data_share_form.is_valid(): +# form_data = data_share_form.cleaned_data +# share_destination = form_data['share_destination'] +# product_id = kwargs.get('dp_pk', None) +# target_id = kwargs.get('tg_pk', None) +# +# # Check if data points have been selected. +# selected_data = request.POST.getlist("share-box") +# +# # Check Destination +# if 'HERMES' in share_destination.upper(): +# response = share_data_with_hermes(share_destination, form_data, product_id, target_id, selected_data) +# else: +# response = share_data_with_tom(share_destination, form_data, product_id, target_id, selected_data) +# try: +# if 'message' in response.json(): +# publish_feedback = response.json()['message'] +# else: +# publish_feedback = f"ERROR: {response.text}" +# except AttributeError: +# publish_feedback = response['message'] +# except ValueError: +# publish_feedback = f"ERROR: Returned Response code {response.status_code}" +# if "ERROR" in publish_feedback.upper(): +# messages.error(self.request, publish_feedback) +# else: +# messages.success(self.request, publish_feedback) +# return redirect(reverse('tom_targets:detail', kwargs={'pk': request.POST.get('target')})) + + class TargetDeleteView(Raise403PermissionRequiredMixin, DeleteView): """ View for deleting a target. Requires authorization. From d342af2b0f946af2e9ed7cae99437d03b0ce4bb4 Mon Sep 17 00:00:00 2001 From: Joseph Chatelain Date: Thu, 13 Jul 2023 16:42:06 -0700 Subject: [PATCH 02/25] Build Target sharing form page --- tom_dataproducts/forms.py | 31 +----- tom_dataproducts/sharing.py | 30 +++++- .../photometry_datalist_for_target.html | 36 +++---- .../templatetags/dataproduct_extras.py | 5 +- tom_targets/forms.py | 16 +++ .../tom_targets/partials/target_buttons.html | 4 +- tom_targets/urls.py | 4 +- tom_targets/views.py | 101 +++++++----------- 8 files changed, 111 insertions(+), 116 deletions(-) diff --git a/tom_dataproducts/forms.py b/tom_dataproducts/forms.py index e0b615a96..43fbbf309 100644 --- a/tom_dataproducts/forms.py +++ b/tom_dataproducts/forms.py @@ -5,7 +5,7 @@ from tom_dataproducts.models import DataProductGroup, DataProduct from tom_observations.models import ObservationRecord from tom_targets.models import Target -from tom_dataproducts.alertstreams.hermes import get_hermes_topics +from tom_dataproducts.sharing import get_sharing_destination_options DATA_TYPE_OPTIONS = (('photometry', 'Photometry'), @@ -65,33 +65,6 @@ class DataShareForm(forms.Form): widget=forms.HiddenInput() ) - def get_sharing_destination_options(self): - """ - Build the Display options and headers for the dropdown form for choosing sharing topics. - Customize for a different selection experience. - :return: Tuple: Possible Destinations and their Display Names - """ - choices = [] - try: - for destination, details in settings.DATA_SHARING.items(): - new_destination = [details.get('DISPLAY_NAME', destination)] - if details.get('USER_TOPICS', None): - # If topics exist for a destination (Such as HERMES) give topics as sub-choices - # for non-selectable Destination - if destination == "hermes": - destination_topics = get_hermes_topics() - else: - destination_topics = details['USER_TOPICS'] - topic_list = [(f'{destination}:{topic}', topic) for topic in destination_topics] - new_destination.append(tuple(topic_list)) - else: - # Otherwise just use destination as option - new_destination.insert(0, destination) - choices.append(tuple(new_destination)) - except AttributeError: - pass - return tuple(choices) - def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.fields['share_destination'].choices = self.get_sharing_destination_options() + self.fields['share_destination'].choices = get_sharing_destination_options() diff --git a/tom_dataproducts/sharing.py b/tom_dataproducts/sharing.py index 8995bd39e..f6a7cda1e 100644 --- a/tom_dataproducts/sharing.py +++ b/tom_dataproducts/sharing.py @@ -6,7 +6,7 @@ from tom_targets.models import Target from tom_dataproducts.models import DataProduct, ReducedDatum -from tom_dataproducts.alertstreams.hermes import publish_photometry_to_hermes, BuildHermesMessage +from tom_dataproducts.alertstreams.hermes import publish_photometry_to_hermes, BuildHermesMessage, get_hermes_topics from tom_dataproducts.serializers import DataProductSerializer, ReducedDatumSerializer @@ -170,3 +170,31 @@ def check_for_share_safe_datums(destination, reduced_datums, **kwargs): def check_for_save_safe_datums(): return + + +def get_sharing_destination_options(): + """ + Build the Display options and headers for the dropdown form for choosing sharing topics. + Customize for a different selection experience. + :return: Tuple: Possible Destinations and their Display Names + """ + choices = [] + try: + for destination, details in settings.DATA_SHARING.items(): + new_destination = [details.get('DISPLAY_NAME', destination)] + if details.get('USER_TOPICS', None): + # If topics exist for a destination (Such as HERMES) give topics as sub-choices + # for non-selectable Destination + if destination == "hermes": + destination_topics = get_hermes_topics() + else: + destination_topics = details['USER_TOPICS'] + topic_list = [(f'{destination}:{topic}', topic) for topic in destination_topics] + new_destination.append(tuple(topic_list)) + else: + # Otherwise just use destination as option + new_destination.insert(0, destination) + choices.append(tuple(new_destination)) + except AttributeError: + pass + return tuple(choices) diff --git a/tom_dataproducts/templates/tom_dataproducts/partials/photometry_datalist_for_target.html b/tom_dataproducts/templates/tom_dataproducts/partials/photometry_datalist_for_target.html index 953af3b88..1a2e499f5 100644 --- a/tom_dataproducts/templates/tom_dataproducts/partials/photometry_datalist_for_target.html +++ b/tom_dataproducts/templates/tom_dataproducts/partials/photometry_datalist_for_target.html @@ -46,25 +46,25 @@ {% endfor %} -
-
- Share Selected Data -
- {% if sharing_destinations %} -
-
- {% bootstrap_field target_data_share_form.share_destination %} -
-
- -
-
- {% else %} - Not Configured - {% endif %} - - + {% if not target_share %} +
+
+ Share Selected Data +
+ {% if sharing_destinations %} +
+
+ {% bootstrap_field target_data_share_form.share_destination %} +
+
+ +
+
+ {% else %} + Not Configured + {% endif %}
+ {% endif %}