Simplify the race condition fix using F()

This commit is contained in:
Tanner Collin 2021-04-15 22:58:56 +00:00
parent 9a3a9db4ee
commit 3f34a3aaf8

View File

@ -4,7 +4,7 @@ logger = logging.getLogger(__name__)
from django.contrib.auth.models import User, Group
from django.shortcuts import get_object_or_404, redirect
from django.db import transaction
from django.db.models import Max
from django.db.models import Max, F
from django.db.utils import OperationalError
from django.http import HttpResponse, Http404, FileResponse
from django.core.files.base import File
@ -505,8 +505,6 @@ class StatsViewSet(viewsets.ViewSet, List):
@action(detail=False, methods=['post'])
def track(self, request):
try:
with transaction.atomic():
if 'name' not in request.data:
raise exceptions.ValidationError(dict(name='This field is required.'))
@ -539,15 +537,11 @@ class StatsViewSet(viewsets.ViewSet, List):
)
logging.info('New ' + devicename + ' session created for: ' + username)
else:
last_session.num_seconds += 10
last_session.save()
last_session.num_seconds = F('num_seconds') + 10
last_session.save(update_fields=['num_seconds'])
return Response(200)
# keep trying if we hit a "database locked" error
except OperationalError:
return self.track(request)
class MemberCountViewSet(Base, List):
pagination_class = None