Skip to content

Commit

Permalink
Merge pull request #10899 from cslzchen/feature/fix-contributor-add
Browse files Browse the repository at this point in the history
[ENG-6845][ENG-6864] Fix contributors during preprint version creation
  • Loading branch information
cslzchen authored Jan 9, 2025
2 parents 79c911e + 0a93590 commit c818d8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
15 changes: 7 additions & 8 deletions osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,15 +1383,14 @@ def add_contributor(self, contributor, permissions=None, visible=True,
)
if save:
self.save()
# TODO: Remove this comment after `Preprint.save()` has been fixed. There is a bug for Preprint when the
# first `.save()` is called. It triggers a series of events which includes adding creator as one
# contributor. However, with versioned guid, there is a gap between when preprint os saved and when
# guid and versioned guid are saved. During this gap, `self._id` is unfortunately `None` and the
# signal can not be be sent.
if self._id and contrib_to_add:
project_signals.contributor_added.send(self,
contributor=contributor,
auth=auth, email_template=send_email, permissions=permissions)
project_signals.contributor_added.send(
self,
contributor=contributor,
auth=auth,
email_template=send_email,
permissions=permissions
)

# enqueue on_node_updated/on_preprint_updated to update DOI metadata when a contributor is added
if getattr(self, 'get_identifier_value', None) and self.get_identifier_value('doi'):
Expand Down
19 changes: 15 additions & 4 deletions osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
from osf.exceptions import (
PreprintStateError,
InvalidTagError,
TagNotFoundError
TagNotFoundError,
UserStateError,
ValidationValueError,
)
from django.contrib.postgres.fields import ArrayField
from api.share.utils import update_share
Expand Down Expand Up @@ -427,9 +429,18 @@ def create_version(cls, create_from_guid, auth):
preprint.save(guid_ready=True, first_save=True)

# Add contributors
for contributor_info in latest_version.contributor_set.exclude(user=latest_version.creator).values('visible', 'user_id', '_order'):
preprint.contributor_set.create(**{**contributor_info, 'preprint_id': preprint.id})

for contributor in latest_version.contributor_set.exclude(user=latest_version.creator):
try:
preprint.add_contributor(
contributor.user,
permissions=contributor.permission,
visible=contributor.visible,
save=True
)
except (ValidationValueError, UserStateError) as e:
sentry.log_exception(e)
sentry.log_message(f'Contributor was not added to new preprint version due to error: '
f'[preprint={preprint._id}, user={contributor.user._id}]')
# Add affiliated institutions
for institution in latest_version.affiliated_institutions.all():
preprint.add_affiliated_institution(institution, auth.user, ignore_user_affiliation=True)
Expand Down

0 comments on commit c818d8c

Please sign in to comment.