Skip to content

Commit

Permalink
Skip SHARE only but still update legacy OSF search
Browse files Browse the repository at this point in the history
  • Loading branch information
cslzchen committed Jan 14, 2025
1 parent 42f2d32 commit 7e4e125
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 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, update_search=True):
def update(self, preprint, validated_data, skip_share=False):
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, update_search=update_search)
self.update_subjects(preprint, subjects, auth, skip_share=skip_share)
save_preprint = True

if 'title' in validated_data:
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, update_search=True):
def update_subjects_method(self, resource, subjects, auth, skip_share=False):
# Method to update subjects on resource
raise NotImplementedError()

def update_subjects(self, resource, subjects, auth, update_search=True):
def update_subjects(self, resource, subjects, auth, skip_share=False):
"""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, update_search=update_search)
self.update_subjects_method(resource, subjects, auth, skip_share=skip_share)
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, update_search=True):
def update_subjects_method(self, resource, subjects, auth, skip_share=False):
# Overrides UpdateSubjectsMixin
return resource.set_subjects_from_relationships(subjects, auth, update_search=update_search)
return resource.set_subjects_from_relationships(subjects, auth, skip_share=skip_share)

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, update_search=True):
def update_subjects_method(self, resource, subjects, auth, skip_share=False):
"""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, update_search=True):
:param object Auth object
"""
if self.expect_subjects_as_relationships(self.context['request']):
return resource.set_subjects_from_relationships(subjects, auth, update_search=update_search)
return resource.set_subjects(subjects, auth, update_search=update_search)
return resource.set_subjects_from_relationships(subjects, auth, skip_share=skip_share)
return resource.set_subjects(subjects, auth, skip_share=skip_share)

def expect_subjects_as_relationships(self, request):
"""Determines whether subjects should be serialized as a relationship.
Expand Down
12 changes: 6 additions & 6 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, update_search=True):
def set_subjects(self, new_subjects, auth, add_log=True, skip_share=False):
""" 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, update_search=True):
self.add_subjects_log(old_subjects, auth)

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

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

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

def map_subjects_between_providers(self, old_provider, new_provider, auth=None):
"""
Expand Down
8 changes: 6 additions & 2 deletions osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,12 @@ def bulk_update_search(cls, preprints, index=None):
logger.exception(e)
log_exception(e)

def update_search(self):
update_share(self)
def update_search(self, skip_share=False):
"""Update SHARE and OSF search.
"""
if not skip_share:
update_share(self)

from website import search
try:
search.search.update_preprint(self, bulk=False, async_update=True)
Expand Down

0 comments on commit 7e4e125

Please sign in to comment.