try-catch for race-based integrity errors

This commit is contained in:
jsangmeister 2019-11-26 11:49:06 +01:00
parent 7454540b67
commit f2dd8ebdd9
1 changed files with 13 additions and 1 deletions

View File

@ -17,6 +17,7 @@ from django.contrib.sites.shortcuts import get_current_site
from django.core import mail
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db import transaction
from django.db.utils import IntegrityError
from django.http.request import QueryDict
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
@ -99,6 +100,13 @@ class UserViewSet(ModelViewSet):
result = False
return result
# catch IntegrityError, probably being caused by a race condition
def perform_create(self, serializer):
try:
super().perform_create(serializer)
except IntegrityError as e:
raise ValidationError({"detail": str(e)})
def update(self, request, *args, **kwargs):
"""
Customized view endpoint to update an user.
@ -360,7 +368,11 @@ class UserViewSet(ModelViewSet):
del data["groups_id"]
db_user = User(**data)
db_user.save(skip_autoupdate=True)
try:
db_user.save(skip_autoupdate=True)
except IntegrityError:
# race condition may happen, so skip double users here again
continue
db_user.groups.add(*groups)
created_users.append(db_user)
if "importTrackId" in user: