Skip to content

Commit

Permalink
handle invalid date
Browse files Browse the repository at this point in the history
  • Loading branch information
everreau authored and mauromsl committed Apr 22, 2024
1 parent 072d067 commit ac081f4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
21 changes: 20 additions & 1 deletion src/repository/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,23 @@ def test_accept_preprint(self):
},
SERVER_NAME=self.server_name,)
p = rm.Preprint.objects.get(pk=self.preprint_one.pk)
self.assertEqual(p.date_published, p.date_accepted)
self.assertEqual(p.date_published, FROZEN_DATETIME)
self.assertEqual(p.date_accepted, FROZEN_DATETIME)

@override_settings(URL_CONFIG='domain')
@freeze_time(FROZEN_DATETIME, tz_offset=5)
def test_accept_preprint_bad_date(self):
self.preprint_one.make_new_version(self.preprint_one.submission_file)
path = reverse('repository_manager_article',
kwargs={'preprint_id': self.preprint_one.pk,})
self.client.force_login(self.repo_manager)
self.client.post(path,
data={
'accept': '',
'datetime': "2024-35-35 10:00",
'timezone': "America/Chicago"
},
SERVER_NAME=self.server_name,)
p = rm.Preprint.objects.get(pk=self.preprint_one.pk)
self.assertIsNone(p.date_published)
self.assertIsNone(p.date_accepted)
58 changes: 36 additions & 22 deletions src/repository/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,32 +882,46 @@ def repository_manager_article(request, preprint_id):
messages.WARNING,
'You must assign at least one galley file.',
)
redirect_request = False
else:
d = datetime.fromisoformat(request.POST.get('datetime', timezone.now().strftime("%Y-%m-%d %H:%M")))
t = tz.gettz(request.POST.get('timezone', str(timezone.get_current_timezone())))
date_published = datetime(d.year, d.month, d.day, d.hour, d.minute, tzinfo=t)
date_kwargs = {
'date_published': date_published
}
if preprint.date_published:
preprint.update_date_published(**date_kwargs)
else:
preprint.accept(**date_kwargs)
event_logic.Events.raise_event(
event_logic.Events.ON_PREPRINT_PUBLICATION,
**{
'request': request,
'preprint': preprint,
},
)
return redirect(
reverse(
'repository_notification',
kwargs={'preprint_id': preprint.pk},
try:
d = datetime.fromisoformat(request.POST.get('datetime', timezone.now().strftime("%Y-%m-%d %H:%M")))
t = tz.gettz(request.POST.get('timezone', str(timezone.get_current_timezone())))

date_published = datetime(d.year, d.month, d.day, d.hour, d.minute, tzinfo=t)
date_kwargs = {
'date_published': date_published
}
if preprint.date_published:
preprint.update_date_published(**date_kwargs)
else:
preprint.accept(**date_kwargs)
event_logic.Events.raise_event(
event_logic.Events.ON_PREPRINT_PUBLICATION,
**{
'request': request,
'preprint': preprint,
},
)
return redirect(
reverse(
'repository_notification',
kwargs={'preprint_id': preprint.pk},
)
)
redirect_request = True
except ValueError:
# This is unlikely to happen because the form widget
# does not accept invalid dates. If we somehow get a bad
# date just send the user back to the accept_preprint modal
redirect_request = False
modal = "accept_preprint"
messages.add_message(
request,
messages.ERROR,
'Invalid publication date selected',
)

redirect_request = True

if 'decline' in request.POST:
note = request.POST.get('decline_note')
Expand Down

0 comments on commit ac081f4

Please sign in to comment.