from celery import shared_task
from django.db.models import F

from grandchallenge.serving.models import Download


@shared_task
def create_download(*_, **kwargs):
    try:
        d = Download.objects.get(**kwargs)
        d.count = F("count") + 1
        d.save()
    except Download.DoesNotExist:
        Download.objects.create(**kwargs)

from celery import shared_task
from django.db.models import F

from grandchallenge.serving.models import Download


@shared_task
def create_download(*_, **kwargs):
    d, created = Download.objects.get_or_create(**kwargs)

    if not created:
        d.count = F("count") + 1
        d.save()

Fix race condition in create_download
Fix race condition in create_download

  from celery import shared_task
  from django.db.models import F
  
  from grandchallenge.serving.models import Download
  
  
  @shared_task
  def create_download(*_, **kwargs):
-     try:
-         d = Download.objects.get(**kwargs)
+     d, created = Download.objects.get_or_create(**kwargs)
+ 
+     if not created:
          d.count = F("count") + 1
          d.save()
-     except Download.DoesNotExist:
-         Download.objects.create(**kwargs)
  
Fix race condition in create_download
## Code Before:
from celery import shared_task
from django.db.models import F

from grandchallenge.serving.models import Download


@shared_task
def create_download(*_, **kwargs):
    try:
        d = Download.objects.get(**kwargs)
        d.count = F("count") + 1
        d.save()
    except Download.DoesNotExist:
        Download.objects.create(**kwargs)

## Instruction:
Fix race condition in create_download
## Code After:
from celery import shared_task
from django.db.models import F

from grandchallenge.serving.models import Download


@shared_task
def create_download(*_, **kwargs):
    d, created = Download.objects.get_or_create(**kwargs)

    if not created:
        d.count = F("count") + 1
        d.save()
