-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in persistentshare model and views and hook it into the target sh…
…are page.
- Loading branch information
Jon
committed
Dec 10, 2024
1 parent
22403b7
commit 595ba2f
Showing
21 changed files
with
516 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from guardian.shortcuts import get_objects_for_user | ||
from rest_framework import serializers | ||
|
||
|
||
class TargetFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField): | ||
# This PrimaryKeyRelatedField subclass is used to implement get_queryset based on the permissions of the user | ||
# submitting the request. The pattern was taken from this StackOverflow answer: https://stackoverflow.com/a/32683066 | ||
|
||
def get_queryset(self): | ||
request = self.context.get('request', None) | ||
queryset = super().get_queryset() | ||
if not (request and queryset): | ||
return None | ||
return get_objects_for_user(request.user, 'tom_targets.change_target') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Generated by Django 4.2.13 on 2024-11-22 22:29 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('tom_targets', '0021_rename_target_basetarget_alter_basetarget_options'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='PersistentShare', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('destination', models.CharField(help_text='The sharing destination, as it appears in your DATA_SHARING settings dict', max_length=200)), | ||
('created', models.DateTimeField(auto_now_add=True, help_text='The time which this PersistentShare was created in the TOM database.')), | ||
('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tom_targets.basetarget')), | ||
('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'ordering': ('-created',), | ||
'unique_together': {('target', 'destination')}, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.13 on 2024-12-05 01:06 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('tom_targets', '0022_persistentshare'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name='basetarget', | ||
options={'permissions': (('view_target', 'View Target'), ('add_target', 'Add Target'), ('share_target', 'Share Target'), ('change_target', 'Change Target'), ('delete_target', 'Delete Target')), 'verbose_name': 'target'}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.dispatch import receiver | ||
from django.db.models.signals import post_save | ||
|
||
from tom_dataproducts.models import ReducedDatum | ||
from tom_dataproducts.sharing import share_data_with_destination | ||
from tom_targets.models import PersistentShare | ||
|
||
|
||
@receiver(post_save, sender=ReducedDatum) | ||
def cb_dataproduct_post_save(sender, instance, *args, **kwargs): | ||
# When a new dataproduct is created or updated, check for any persistentshare instances on that target | ||
# and if they exist, attempt to share the new data | ||
target = instance.target | ||
persistentshares = PersistentShare.objects.filter(target=target) | ||
for persistentshare in persistentshares: | ||
share_destination = persistentshare.destination | ||
share_data_with_destination(share_destination, instance) |
73 changes: 73 additions & 0 deletions
73
tom_targets/templates/tom_targets/partials/create_persistent_share.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{% load bootstrap4 targets_extras static %} | ||
|
||
<form method="post" class="form" id='target-persistent-share-create-form'> | ||
{% csrf_token %} | ||
<div class="form-row" style="padding-inline:1rem"> | ||
<div class="col-sm-3"> | ||
{% bootstrap_field form.destination %} | ||
</div> | ||
<div class="col-sm-5"> | ||
{% bootstrap_field form.target %} | ||
</div> | ||
<div class="col-sm-1"> | ||
{% if target %} | ||
<input type="button" class="btn btn-primary" value="Create" onclick="createPersistentShare('{% url 'targets:persistent-share' %}', '{% url 'targets:target-persistent-share-manage-table' target.pk %}')" style="position:absolute; bottom:1rem"/> | ||
{% else %} | ||
<input type="button" class="btn btn-primary" value="Create" onclick="createPersistentShare('{% url 'targets:persistent-share' %}', '{% url 'targets:persistent-share-manage-table' %}')" style="position:absolute; bottom:1rem"/> | ||
{% endif %} | ||
</div> | ||
</div> | ||
<div class="form-row"> | ||
<div class="alert alert-danger" role="alert" id="create_persistent_share_error" style="display:none"> | ||
<div class="row"> | ||
<div class="col-sm-11"> | ||
<p id="create_persistent_share_error_msg"></p> | ||
</div> | ||
<div class="col-sm-1"> | ||
<button type="button" class="close" aria-label="Close" onclick="hidePSErrorAlert()"> | ||
<span aria-hidden="true">×</span> | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
{% buttons %} | ||
{% endbuttons %} | ||
</form> | ||
<script> | ||
async function createPersistentShare(createUrl, updateUrl) { | ||
var target_id = document.getElementById('id_target').value; | ||
var destination = document.getElementById('id_destination').value; | ||
var payload = { | ||
"destination": destination, | ||
"target": target_id | ||
} | ||
const response = await fetch(createUrl, { | ||
method: 'POST', | ||
body: JSON.stringify(payload), | ||
headers: { | ||
'X-CSRFToken': "{{ csrf_token }}", | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
|
||
if (response.ok) { | ||
updatePersistentShareTable(updateUrl); | ||
} | ||
else{ | ||
const responseJson = await response.json() | ||
var error_msg = document.getElementById('create_persistent_share_error_msg'); | ||
error_msg.innerText = 'Failed to create Persisten Share: ' + JSON.stringify(responseJson); | ||
var error_msg_alert = document.getElementById('create_persistent_share_error'); | ||
error_msg_alert.style.display = "block"; | ||
} | ||
} | ||
|
||
function hidePSErrorAlert() { | ||
var error_msg = document.getElementById('create_persistent_share_error_msg'); | ||
error_msg.innerText = ''; | ||
var error_msg_alert = document.getElementById('create_persistent_share_error'); | ||
error_msg_alert.style.display = "none"; | ||
} | ||
</script> |
Oops, something went wrong.