|
| 1 | +from tom_targets.base_models import get_target_model_app_label, BaseTarget |
1 | 2 | import requests |
2 | 3 |
|
3 | 4 | from django.conf import settings |
4 | 5 | from django.core.exceptions import ImproperlyConfigured |
5 | 6 |
|
6 | 7 | from tom_targets.serializers import TargetSerializer |
7 | | -from tom_targets.models import PersistentShare |
| 8 | +from tom_targets.models import PersistentShare, get_target_model_class |
8 | 9 | from tom_dataproducts.sharing import (share_data_with_tom, |
9 | 10 | get_destination_target, sharing_feedback_converter) |
10 | 11 |
|
@@ -37,6 +38,20 @@ def continuous_share_data(target, reduced_datums): |
37 | 38 | share_data_with_tom(share_destination, None, None, None, selected_data=reduced_datum_pks) |
38 | 39 |
|
39 | 40 |
|
| 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 | + |
40 | 55 | def share_target_with_tom(share_destination, form_data, target_lists=()): |
41 | 56 | """ |
42 | 57 | Share a target with a remote TOM. |
@@ -76,6 +91,11 @@ def share_target_with_tom(share_destination, form_data, target_lists=()): |
76 | 91 | if destination_target_id is None: |
77 | 92 | # If target is not in Destination, serialize and create new target. |
78 | 93 | 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) |
79 | 99 | # Remove local User Groups |
80 | 100 | serialized_target['groups'] = [] |
81 | 101 | # Add target lists |
|
0 commit comments