fix: eliminate race condition when creating a review Document (#4876)

This commit is contained in:
Jennifer Richards 2022-12-13 16:45:42 -04:00 committed by GitHub
parent 0d2e264de4
commit 3d47a9069c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -711,8 +711,11 @@ def complete_review(request, name, assignment_id=None, acronym=None):
date_today().isoformat(),
]
review_name = "-".join(c for c in name_components if c).lower()
if not Document.objects.filter(name=review_name).exists():
review = Document.objects.create(name=review_name,type_id='review',group=team)
review, created = Document.objects.get_or_create(
name=review_name,
defaults={'type_id': 'review', 'group': team},
)
if created:
DocAlias.objects.create(name=review_name).docs.add(review)
else:
messages.warning(request, message='Attempt to save review failed: review document already exists. This most likely occurred because the review was submitted twice in quick succession. If you intended to submit a new review, rather than update an existing one, things are probably OK. Please verify that the shown review is what you expected.')