Skip to content

Commit

Permalink
Merge pull request #710 from TOMToolkit/fix/tom_sharing_tweaks
Browse files Browse the repository at this point in the history
Fix/tom sharing tweaks
  • Loading branch information
jchate6 authored Oct 23, 2023
2 parents 4cb3e50 + f068566 commit 727ef98
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 8 deletions.
3 changes: 2 additions & 1 deletion tom_dataproducts/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin

from tom_dataproducts.models import DataProduct, DataProductGroup
from tom_dataproducts.models import DataProduct, DataProductGroup, ReducedDatum

admin.site.register(DataProduct)
admin.site.register(DataProductGroup)
admin.site.register(ReducedDatum)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.1 on 2023-10-23 22:03

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('tom_dataproducts', '0011_reduceddatum_message'),
]

operations = [
migrations.AlterField(
model_name='reduceddatum',
name='data_product',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='tom_dataproducts.dataproduct'),
),
migrations.AlterField(
model_name='reduceddatum',
name='source_location',
field=models.CharField(blank=True, default='', max_length=200),
),
migrations.AlterField(
model_name='reduceddatum',
name='source_name',
field=models.CharField(blank=True, default='', max_length=100),
),
]
4 changes: 2 additions & 2 deletions tom_dataproducts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ class ReducedDatum(models.Model):
max_length=100,
default=''
)
source_name = models.CharField(max_length=100, default='')
source_location = models.CharField(max_length=200, default='')
source_name = models.CharField(max_length=100, default='', blank=True)
source_location = models.CharField(max_length=200, default='', blank=True)
timestamp = models.DateTimeField(null=False, blank=False, default=datetime.now, db_index=True)
value = models.JSONField(null=False, blank=False)
message = models.ManyToManyField(AlertStreamMessage)
Expand Down
5 changes: 3 additions & 2 deletions tom_dataproducts/sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ def share_data_with_tom(share_destination, form_data, product_id=None, target_id
serialized_data['data_product'] = ''
if not serialized_data['source_name']:
serialized_data['source_name'] = settings.TOM_NAME
serialized_data['source_location'] = "TOM-TOM Direct Sharing"
serialized_data['source_location'] = f"ReducedDatum shared from " \
f"<{settings.TOM_NAME}.url>/api/reduceddatums/{datum.id}/"
response = requests.post(reduced_datums_url, json=serialized_data, headers=headers, auth=auth)
response_codes.append(response.status_code)
failed_data_count = response_codes.count(500)
failed_data_count = len([rc for rc in response_codes if rc >= 300])
if failed_data_count < len(response_codes):
return {'message': f'{len(response_codes)-failed_data_count} of {len(response_codes)} '
'datums successfully saved.'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h4>Data</h4>
</td>
<td>
{% if sharing_destinations %}
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#share-{{ forloop.counter }}">Publish</button>
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#share-{{ forloop.counter }}">Share</button>
{% else %}
<p>Not Configured</p>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% load tom_common_extras %}
<div class="card" style="margin-top:1rem; margin-bottom:1rem;">
<div class="card-header">
Publish Data for {{ target.name }}:
Share Data for {{ target.name }}:
</div>
{% if sharing_destinations %}
<form method="POST" action="{% url 'tom_dataproducts:share_all' tg_pk=target.id %}" enctype="multipart/form-data">
Expand Down
2 changes: 1 addition & 1 deletion tom_dataproducts/templatetags/dataproduct_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def upload_dataproduct(context, obj):
@register.inclusion_tag('tom_dataproducts/partials/share_target_data.html', takes_context=True)
def share_data(context, target):
"""
Publish data to Hermes
Share data to Hermes or another TOM
"""

initial = {'submitter': context['request'].user,
Expand Down
16 changes: 16 additions & 0 deletions tom_dataproducts/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,19 @@ def test_upload_same_reduced_datum_twice(self):
self.client.post(reverse('api:reduceddatums-list'), self.rd_data, format='json')
rd_queryset = ReducedDatum.objects.all()
self.assertEqual(rd_queryset.count(), 2)

def test_upload_reduced_datum_no_sharing_location(self):
"""
Test that a reduced datum can be uploaded without a source_location.
"""
del self.rd_data['source_location']
response = self.client.post(reverse('api:reduceddatums-list'), self.rd_data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_upload_reduced_datum_no_sharing_name(self):
"""
Test that a reduced datum can be uploaded without a source_name.
"""
del self.rd_data['source_name']
response = self.client.post(reverse('api:reduceddatums-list'), self.rd_data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
70 changes: 70 additions & 0 deletions tom_dataproducts/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,73 @@ def test_share_reduced_datums_valid_responses(self):
follow=True
)
self.assertContains(response, '2 of 2 datums successfully saved.')

@responses.activate
def test_share_reduced_datums_invalid_responses(self):
share_destination = 'local_host'
destination_tom_base_url = settings.DATA_SHARING[share_destination]['BASE_URL']

rsp1 = responses.Response(
method="GET",
url=destination_tom_base_url + 'api/targets/',
json={"results": [{'id': 1}]},
status=200
)
responses.add(rsp1)
responses.add(
responses.GET,
"http://hermes-dev.lco.global/api/v0/profile/",
json={"error": "not found"},
status=404,
)

sharing_dict = {
'share_authors': ['test_author'],
'target': self.target.id,
'submitter': ['test_submitter'],
'share_destination': [share_destination],
'share_title': ['Updated data for thingy.'],
'share_message': ['test_message'],
'share-box': [1, 2]
}
# Check 500 error
responses.add(
responses.POST,
destination_tom_base_url + 'api/reduceddatums/',
json={},
status=500,
)
response = self.client.post(
reverse('dataproducts:share_all', kwargs={'tg_pk': self.target.id}),
sharing_dict,
follow=True
)
self.assertContains(response, 'No valid data shared. These data may already exist in target TOM.')

# Check 400 error
responses.add(
responses.POST,
destination_tom_base_url + 'api/reduceddatums/',
json={},
status=400,
)
response = self.client.post(
reverse('dataproducts:share_all', kwargs={'tg_pk': self.target.id}),
sharing_dict,
follow=True
)
self.assertContains(response, 'No valid data shared. These data may already exist in target TOM.')

# Check 300 error
responses.add(
responses.POST,
destination_tom_base_url + 'api/reduceddatums/',
json={},
status=300,
)
response = self.client.post(
reverse('dataproducts:share_all', kwargs={'tg_pk': self.target.id}),
sharing_dict,
follow=True
)
self.assertContains(response, 'No valid data shared. These data may already exist in target TOM.')

0 comments on commit 727ef98

Please sign in to comment.