diff --git a/api/registrations/serializers.py b/api/registrations/serializers.py index 99991356be6..e45c74a22d1 100644 --- a/api/registrations/serializers.py +++ b/api/registrations/serializers.py @@ -23,6 +23,7 @@ from framework.auth.core import Auth from osf.exceptions import ValidationValueError, NodeStateError from osf.models import Node, RegistrationSchema +from osf.utils.registrations import strip_registered_meta_comments from website.settings import ANONYMIZED_TITLES from framework.sentry import log_exception @@ -387,7 +388,8 @@ def anonymize_registered_meta(self, obj): matching ANONYMIZED_TITLES. If present, deletes that question's response from meta_values. """ - meta_values = obj.registered_meta.values()[0] + meta_values = strip_registered_meta_comments(obj.registered_meta.values()[0]) + if is_anonymized(self.context['request']): registration_schema = RegistrationSchema.objects.get(_id=obj.registered_schema_id) for page in registration_schema.schema['pages']: @@ -395,7 +397,6 @@ def anonymize_registered_meta(self, obj): if question['title'] in ANONYMIZED_TITLES and meta_values.get(question.get('qid')): del meta_values[question['qid']] - strip_registered_meta_comments(meta_values) return meta_values def check_admin_perms(self, registration, user, validated_data): @@ -621,33 +622,3 @@ class RegistrationStorageProviderSerializer(NodeStorageProviderSerializer): kind='folder', never_embed=True, ) - -def strip_registered_meta_comments(messy_dict_or_list): - """Removes Prereg Challenge comments from a given `registered_meta` dict. - - Nothing that uses APIv2 needs these comments: - ``` - { - "registered_meta": { - "q20": { - "comments": [ ... ], <~~~ THIS - "value": "foo", - "extra": [] - }, - } - } - ``` - """ - if isinstance(messy_dict_or_list, list): - for obj in messy_dict_or_list: - strip_registered_meta_comments(obj) - elif isinstance(messy_dict_or_list, dict): - comments = messy_dict_or_list.get('comments', None) - - # some schemas have a question named "comments" -- those will have a dict value - if isinstance(comments, list): - del messy_dict_or_list['comments'] - - # dig into the deeply nested structure - for nested_obj in messy_dict_or_list.values(): - strip_registered_meta_comments(nested_obj) diff --git a/osf/utils/registrations.py b/osf/utils/registrations.py new file mode 100644 index 00000000000..a346ad5dc2b --- /dev/null +++ b/osf/utils/registrations.py @@ -0,0 +1,37 @@ +import copy + +def strip_registered_meta_comments(messy_dict_or_list, in_place=False): + """Removes Prereg Challenge comments from a given `registered_meta` dict. + + Nothing publicly exposed needs these comments: + ``` + { + "registered_meta": { + "q20": { + "comments": [ ... ], <~~~ THIS + "value": "foo", + "extra": [] + }, + } + } + ``` + + If `in_place` is truthy, modifies `messy_dict_or_list` and returns it. + Else, returns a deep copy without modifying the given `messy_dict_or_list` + """ + obj = messy_dict_or_list if in_place else copy.deepcopy(messy_dict_or_list) + + if isinstance(obj, list): + for nested_obj in obj: + strip_registered_meta_comments(nested_obj, in_place=True) + elif isinstance(obj, dict): + comments = obj.get('comments', None) + + # some schemas have a question named "comments" -- those will have a dict value + if isinstance(comments, list): + del obj['comments'] + + # dig into the deeply nested structure + for nested_obj in obj.values(): + strip_registered_meta_comments(nested_obj, in_place=True) + return obj diff --git a/website/project/views/node.py b/website/project/views/node.py index 6aadef2398d..d5944de5f94 100644 --- a/website/project/views/node.py +++ b/website/project/views/node.py @@ -20,6 +20,7 @@ from framework.exceptions import HTTPError from osf.models.nodelog import NodeLog from osf.utils.functional import rapply +from osf.utils.registrations import strip_registered_meta_comments from osf.utils import sanitize from osf import features @@ -762,7 +763,7 @@ def _view_project(node, auth, primary=False, 'registered_from_url': node.registered_from.url if is_registration else '', 'registered_date': iso8601format(node.registered_date) if is_registration else '', 'root_id': node.root._id if node.root else None, - 'registered_meta': node.registered_meta, + 'registered_meta': strip_registered_meta_comments(node.registered_meta), 'registered_schemas': serialize_meta_schemas(list(node.registered_schema.all())) if is_registration else False, 'is_fork': node.is_fork, 'is_collected': node.is_collected,