Skip to content

Commit

Permalink
Merge branch 'hotfix/19.29.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
mfraezz committed Oct 7, 2019
2 parents 9af2129 + 58d7d6e commit d22b269
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
35 changes: 3 additions & 32 deletions api/registrations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -387,15 +388,15 @@ 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']:
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):
Expand Down Expand Up @@ -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)
37 changes: 37 additions & 0 deletions osf/utils/registrations.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion website/project/views/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d22b269

Please sign in to comment.