Skip to content

Commit

Permalink
Resolve issue with updating preprint fields and validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Uditi Mehta committed Sep 20, 2024
1 parent df41bb4 commit c553066
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
8 changes: 8 additions & 0 deletions api/preprints/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,36 +362,42 @@ def update(self, preprint, validated_data):
if 'has_coi' in validated_data:
try:
preprint.update_has_coi(auth, validated_data['has_coi'])
save_preprint = True
except PreprintStateError as e:
raise exceptions.ValidationError(detail=str(e))

if 'conflict_of_interest_statement' in validated_data:
try:
preprint.update_conflict_of_interest_statement(auth, validated_data['conflict_of_interest_statement'])
save_preprint = True
except PreprintStateError as e:
raise exceptions.ValidationError(detail=str(e))

if 'has_data_links' in validated_data:
try:
preprint.update_has_data_links(auth, validated_data['has_data_links'])
save_preprint = True
except PreprintStateError as e:
raise exceptions.ValidationError(detail=str(e))

if 'why_no_data' in validated_data:
try:
preprint.update_why_no_data(auth, validated_data['why_no_data'])
save_preprint = True
except PreprintStateError as e:
raise exceptions.ValidationError(detail=str(e))

if 'data_links' in validated_data:
try:
preprint.update_data_links(auth, validated_data['data_links'])
save_preprint = True
except PreprintStateError as e:
raise exceptions.ValidationError(detail=str(e))

if 'has_prereg_links' in validated_data:
try:
preprint.update_has_prereg_links(auth, validated_data['has_prereg_links'])
save_preprint = True
except PreprintStateError as e:
raise exceptions.ValidationError(detail=str(e))

Expand All @@ -404,12 +410,14 @@ def update(self, preprint, validated_data):
if 'prereg_links' in validated_data:
try:
preprint.update_prereg_links(auth, validated_data['prereg_links'])
save_preprint = True
except PreprintStateError as e:
raise exceptions.ValidationError(detail=str(e))

if 'prereg_link_info' in validated_data:
try:
preprint.update_prereg_link_info(auth, validated_data['prereg_link_info'])
save_preprint = True
except PreprintStateError as e:
raise exceptions.ValidationError(detail=str(e))

Expand Down
43 changes: 19 additions & 24 deletions osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,9 @@ def update_has_coi(self, auth: Auth, has_coi: bool, log: bool = True, save: bool
This method brought to you via a grant from the Alfred P Sloan Foundation.
"""
if has_coi is None:
has_coi = False

if self.has_coi == has_coi:
return

Expand Down Expand Up @@ -1021,17 +1024,14 @@ def update_conflict_of_interest_statement(self, auth: Auth, coi_statement: str,
if self.conflict_of_interest_statement == coi_statement:
return

if not self.has_coi:
raise PreprintStateError('You do not have the ability to edit a conflict of interest while the has_coi field is '
'set to false or unanswered')

self.conflict_of_interest_statement = coi_statement
self.conflict_of_interest_statement = coi_statement or ''

if log:
self.add_log(
action=PreprintLog.UPDATE_COI_STATEMENT,
params={
'user': auth.user._id,
'value': self.conflict_of_interest_statement
},
auth=auth,
)
Expand Down Expand Up @@ -1065,7 +1065,7 @@ def update_has_data_links(self, auth: Auth, has_data_links: bool, log: bool = Tr
},
auth=auth
)
if has_data_links != 'available':
if not has_data_links:
self.update_data_links(auth, data_links=[], log=False)
if save:
self.save()
Expand All @@ -1086,9 +1086,8 @@ def update_data_links(self, auth: Auth, data_links: list, log: bool = True, save
if self.data_links == data_links:
return

if not self.has_data_links == 'available' and data_links:
raise PreprintStateError('You cannot edit this statement while your data links availability is set to false'
' or is unanswered.')
if not self.has_data_links and data_links:
self.data_links = []

self.data_links = data_links

Expand Down Expand Up @@ -1119,11 +1118,10 @@ def update_why_no_data(self, auth: Auth, why_no_data: str, log: bool = True, sav
if self.why_no_data == why_no_data:
return

if not self.has_data_links == 'no':
raise PreprintStateError('You cannot edit this statement while your data links availability is set to true or'
' is unanswered.')
else:
self.why_no_data = why_no_data
if self.has_data_links:
self.why_no_data = ''

self.why_no_data = why_no_data

if log:
self.add_log(
Expand Down Expand Up @@ -1163,7 +1161,7 @@ def update_has_prereg_links(self, auth: Auth, has_prereg_links: bool, log: bool
},
auth=auth
)
if has_prereg_links != 'available':
if not has_prereg_links:
self.update_prereg_links(auth, prereg_links=[], log=False)
self.update_prereg_link_info(auth, prereg_link_info=None, log=False)
if save:
Expand All @@ -1185,9 +1183,8 @@ def update_why_no_prereg(self, auth: Auth, why_no_prereg: str, log: bool = True,
if why_no_prereg == self.why_no_prereg:
return

if self.has_prereg_links == 'available' or self.has_prereg_links is None:
raise PreprintStateError('You cannot edit this statement while your prereg links '
'availability is set to true or is unanswered.')
if self.has_prereg_links or self.has_prereg_links is None:
self.why_no_prereg = ''

self.why_no_prereg = why_no_prereg

Expand Down Expand Up @@ -1218,9 +1215,8 @@ def update_prereg_links(self, auth: Auth, prereg_links: list, log: bool = True,
if prereg_links == self.prereg_links:
return

if not self.has_prereg_links == 'available' and prereg_links:
raise PreprintStateError('You cannot edit this field while your prereg links'
' availability is set to false or is unanswered.')
if not self.has_prereg_links and prereg_links:
self.prereg_links = []

self.prereg_links = prereg_links

Expand Down Expand Up @@ -1252,9 +1248,8 @@ def update_prereg_link_info(self, auth: Auth, prereg_link_info: str, log: bool =
if self.prereg_link_info == prereg_link_info:
return

if not self.has_prereg_links == 'available' and prereg_link_info:
raise PreprintStateError('You cannot edit this field while your prereg links'
' availability is set to false or is unanswered.')
if not self.has_prereg_links and prereg_link_info:
self.prereg_link_info = None

self.prereg_link_info = prereg_link_info

Expand Down

0 comments on commit c553066

Please sign in to comment.