diff --git a/api/registrations/serializers.py b/api/registrations/serializers.py index dc4795dc132..99991356be6 100644 --- a/api/registrations/serializers.py +++ b/api/registrations/serializers.py @@ -394,6 +394,8 @@ def anonymize_registered_meta(self, obj): for question in page['questions']: 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): @@ -619,3 +621,33 @@ 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)