Skip to content

Commit

Permalink
Merge pull request #7 from razortheory/celery5-support
Browse files Browse the repository at this point in the history
celery 5 support
  • Loading branch information
maximlomakin authored Jan 11, 2021
2 parents e497df6 + a902ff0 commit 3593e0a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 19 deletions.
18 changes: 17 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,20 @@ Quick start

TOKEN_AGE = 60*10 # 10 min

6. Run `python manage.py migrate` to create the drf_secure_token models.
6. (Optional) To cleanup dead tokens celery can be used. Way to enable depends from celery version

6.1 Celery 4, just enable it with settings::

REMOVE_TOKENS_THROUGH_CELERY = True

6.2 Celery 5, add periodic task manually::

@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
from drf_secure_token.tasks import DELETE_OLD_TOKENS

app.conf.beat_schedule.update({
'drf_secure_token.tasks.delete_old_tokens': DELETE_OLD_TOKENS,
})

7. Run `python manage.py migrate` to create the drf_secure_token models.
2 changes: 0 additions & 2 deletions drf_secure_token/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.utils.six import python_2_unicode_compatible

from rest_framework import exceptions

from drf_secure_token import checkers
from drf_secure_token.settings import settings as token_settings


@python_2_unicode_compatible
class BaseToken(models.Model):
@staticmethod
def generate_key():
Expand Down
2 changes: 1 addition & 1 deletion drf_secure_token/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

DEFAULT_SETTINGS = {
'TOKEN_AGE': 0,
'UPDATE_TOKEN': not django_settings.DEBUG,
'UPDATE_TOKEN': not getattr(django_settings, 'DEBUG', False),

'MUTABLE_PERIOD': 60 * 60 * 24 * 7, # One week

Expand Down
34 changes: 26 additions & 8 deletions drf_secure_token/tasks.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
from django.utils import timezone

from celery import VERSION as CELERY_VERSION
from celery.schedules import crontab
from celery.task import periodic_task

from drf_secure_token.models import Token
from drf_secure_token.settings import settings as token_settings

if token_settings.REMOVE_TOKENS_THROUGH_CELERY:
@periodic_task(run_every=crontab(minute=0))
def delete_old_tokens():
now = timezone.now()

qs = Token.objects.all()
qs = qs.filter(dead_in__lt=now)
def delete_old_tokens():
now = timezone.now()

qs.delete()
qs = Token.objects.all()
qs = qs.filter(dead_in__lt=now)

qs.delete()


# if setting enabled and default app exists (celery<5), use it. else just register task to be available for scheduler
if CELERY_VERSION < (5, 0, 0):
if token_settings.REMOVE_TOKENS_THROUGH_CELERY:
from celery.task import periodic_task
delete_old_tokens = periodic_task(run_every=crontab(minute=0))(delete_old_tokens)
else:
from celery.task import task
delete_old_tokens = task(delete_old_tokens)
else:
from celery import shared_task
delete_old_tokens = shared_task(delete_old_tokens)

DELETE_OLD_TOKENS = {
'task': 'drf_secure_token.tasks.delete_old_tokens',
'schedule': crontab(minute=0),
'args': ()
}
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='drf-secure-token',
version='1.0.4',
version='1.1.0',
packages=['drf_secure_token', 'drf_secure_token/migrations'],
include_package_data=True,
license='BSD License',
Expand All @@ -27,7 +27,6 @@
'License :: OSI Approved :: BSD License', # example license
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
Expand Down
11 changes: 6 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[tox]
envlist =
{py27,py36,py37}-django111
{py36,py37}-django{21,22}
{py36,py38}-django{21,22}-celery{4,5}
{py38}-django{30}-celery5
[testenv]
deps =
django111: django>=1.11,<2.0
django21: django>=2.1,<2.2
django22: django>=2.2,<2.3
django111: djangorestframework>=3.7,<3.11
django30: django>=3.0,<3.1
django{21,22}: djangorestframework>=3.9,<3.11
celery
django{30}: djangorestframework
celery4: celery>=4,<5
celery5: celery>=5,<6
mock
coverage
commands =
Expand Down

0 comments on commit 3593e0a

Please sign in to comment.