Skip to content

Commit

Permalink
Do not update share during version creation
Browse files Browse the repository at this point in the history
  • Loading branch information
cslzchen committed Jan 13, 2025
1 parent e49b64c commit 42f2d32
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions api/preprints/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def get_preprint_doi_url(self, obj):
doi = client.build_doi(preprint=obj) if client else None
return f'https://doi.org/{doi}' if doi else None

def update(self, preprint, validated_data):
def update(self, preprint, validated_data, update_search=True):
assert isinstance(preprint, Preprint), 'You must specify a valid preprint to be updated'

auth = get_user_auth(self.context['request'])
Expand Down Expand Up @@ -475,7 +475,7 @@ def require_admin_permission():

if 'subjects' in validated_data:
subjects = validated_data.pop('subjects', None)
self.update_subjects(preprint, subjects, auth)
self.update_subjects(preprint, subjects, auth, update_search=update_search)
save_preprint = True

if 'title' in validated_data:
Expand Down Expand Up @@ -598,7 +598,8 @@ def create(self, validated_data):
if not preprint:
raise NotFound(detail='Failed to create a new preprint version due to source preprint not found.')
if data_to_update:
return self.update(preprint, data_to_update)
# Share should not be updated during version creation
return self.update(preprint, data_to_update, update_search=False)
return preprint


Expand Down
10 changes: 5 additions & 5 deletions api/subjects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@


class UpdateSubjectsMixin:
def update_subjects_method(self, resource, subjects, auth):
def update_subjects_method(self, resource, subjects, auth, update_search=True):
# Method to update subjects on resource
raise NotImplementedError()

def update_subjects(self, resource, subjects, auth):
def update_subjects(self, resource, subjects, auth, update_search=True):
"""Updates subjects on resource and handles errors.
:param object resource: Object for which you want to update subjects
:param list subjects: Subjects array (or array of arrays)
:param object Auth object
"""
try:
self.update_subjects_method(resource, subjects, auth)
self.update_subjects_method(resource, subjects, auth, update_search=update_search)
except PermissionsError as e:
raise exceptions.PermissionDenied(detail=str(e))
except ValueError as e:
Expand Down Expand Up @@ -106,9 +106,9 @@ def make_instance_obj(self, obj):
def format_subjects(self, subjects):
return [subj['_id'] for subj in subjects]

def update_subjects_method(self, resource, subjects, auth):
def update_subjects_method(self, resource, subjects, auth, update_search=True):
# Overrides UpdateSubjectsMixin
return resource.set_subjects_from_relationships(subjects, auth)
return resource.set_subjects_from_relationships(subjects, auth, update_search=update_search)

def update(self, instance, validated_data):
resource = instance['self']
Expand Down
6 changes: 3 additions & 3 deletions api/taxonomies/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_subjects(self, obj):
]

# Overrides UpdateSubjectsMixin
def update_subjects_method(self, resource, subjects, auth):
def update_subjects_method(self, resource, subjects, auth, update_search=True):
"""Depending on the request's version, runs a different method
to update the resource's subjects. Will expect request to be formatted
differently, depending on the version.
Expand All @@ -108,8 +108,8 @@ def update_subjects_method(self, resource, subjects, auth):
:param object Auth object
"""
if self.expect_subjects_as_relationships(self.context['request']):
return resource.set_subjects_from_relationships(subjects, auth)
return resource.set_subjects(subjects, auth)
return resource.set_subjects_from_relationships(subjects, auth, update_search=update_search)
return resource.set_subjects(subjects, auth, update_search=update_search)

def expect_subjects_as_relationships(self, request):
"""Determines whether subjects should be serialized as a relationship.
Expand Down
8 changes: 4 additions & 4 deletions osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ def assert_subject_format(self, subj_list, expect_list, error_msg):
if (expect_list and not is_list) or (not expect_list and is_list):
raise ValidationValueError(f'Subjects are improperly formatted. {error_msg}')

def set_subjects(self, new_subjects, auth, add_log=True):
def set_subjects(self, new_subjects, auth, add_log=True, update_search=True):
""" Helper for setting M2M subjects field from list of hierarchies received from UI.
Only authorized admins may set subjects.
Expand Down Expand Up @@ -1134,10 +1134,10 @@ def set_subjects(self, new_subjects, auth, add_log=True):
self.add_subjects_log(old_subjects, auth)

self.save()
if hasattr(self, 'update_search'):
if update_search and hasattr(self, 'update_search'):
self.update_search()

def set_subjects_from_relationships(self, subjects_list, auth, add_log=True):
def set_subjects_from_relationships(self, subjects_list, auth, add_log=True, update_search=True):
""" Helper for setting M2M subjects field from list of flattened subjects received from UI.
Only authorized admins may set subjects.
Expand All @@ -1161,7 +1161,7 @@ def set_subjects_from_relationships(self, subjects_list, auth, add_log=True):
self.add_subjects_log(old_subjects, auth)

self.save()
if hasattr(self, 'update_search'):
if update_search and hasattr(self, 'update_search'):
self.update_search()

def map_subjects_between_providers(self, old_provider, new_provider, auth=None):
Expand Down

0 comments on commit 42f2d32

Please sign in to comment.