Skip to content

Commit a316da1

Browse files
committed
Sharing: convert custom target model fiends to target extras
1 parent de44131 commit a316da1

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

tom_targets/sharing.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
from tom_targets.base_models import get_target_model_app_label, BaseTarget
12
import requests
23

34
from django.conf import settings
45
from django.core.exceptions import ImproperlyConfigured
56

67
from tom_targets.serializers import TargetSerializer
7-
from tom_targets.models import PersistentShare
8+
from tom_targets.models import PersistentShare, get_target_model_class
89
from tom_dataproducts.sharing import (share_data_with_tom,
910
get_destination_target, sharing_feedback_converter)
1011

@@ -37,6 +38,20 @@ def continuous_share_data(target, reduced_datums):
3738
share_data_with_tom(share_destination, None, None, None, selected_data=reduced_datum_pks)
3839

3940

41+
def custom_target_to_extras(target_id) -> list[dict]:
42+
target_app_label = get_target_model_app_label()
43+
extra_fields = []
44+
if target_app_label != 'tom_targets':
45+
target = get_target_model_class().objects.get(pk=target_id)
46+
for field in target._meta.get_fields():
47+
if field not in BaseTarget._meta.get_fields() and field.name not in ['id', 'basetarget_ptr']:
48+
value = getattr(target, field.name, None)
49+
if value is not None:
50+
extra_fields.append({'key': field.name, 'value': value})
51+
52+
return extra_fields
53+
54+
4055
def share_target_with_tom(share_destination, form_data, target_lists=()):
4156
"""
4257
Share a target with a remote TOM.
@@ -76,6 +91,11 @@ def share_target_with_tom(share_destination, form_data, target_lists=()):
7691
if destination_target_id is None:
7792
# If target is not in Destination, serialize and create new target.
7893
serialized_target = TargetSerializer(form_data['target']).data
94+
# If the shared target is a custom model custom fields should still be shared.
95+
# Because the destination TOM might not have the same fields, we convert them to
96+
# target extras.
97+
extra_extras = custom_target_to_extras(serialized_target['id'])
98+
serialized_target['targetextra_set'].extend(extra_extras)
7999
# Remove local User Groups
80100
serialized_target['groups'] = []
81101
# Add target lists

0 commit comments

Comments
 (0)