Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 2.2 support #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions django_celery_monitor/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from celery import states
from celery.events.state import Task
from celery.utils.time import maybe_timedelta
import django
from django.db import models, router, transaction

from .utils import Now
Expand All @@ -26,13 +27,30 @@ def select_for_update_or_create(self, defaults=None, **kwargs):
select_for_update when getting the object.
"""
defaults = defaults or {}
lookup, params = self._extract_model_params(defaults, **kwargs)
self._for_write = True

if django.VERSION < (2,2):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Django 2.2 check could be eleminated

lookup, params = self._extract_model_params(defaults, **kwargs)
with transaction.atomic(using=self.db):
try:
obj = self.select_for_update().get(**lookup)
except self.model.DoesNotExist:
obj, created = self._create_object_from_params(lookup, params)
if created:
return obj, created
for k, v in defaults.items():
setattr(obj, k, v() if callable(v) else v)
obj.save(using=self.db)
return obj, False

with transaction.atomic(using=self.db):
try:
obj = self.select_for_update().get(**lookup)
obj = self.select_for_update().get(**kwargs)
except self.model.DoesNotExist:
obj, created = self._create_object_from_params(lookup, params)
params = self._extract_model_params(defaults, **kwargs)
# Lock the row so that a concurrent update is blocked until
# after update_or_create() has performed its save.
obj, created = self._create_object_from_params(kwargs, params, lock=True)
if created:
return obj, created
for k, v in defaults.items():
Expand Down