我写了一个django,它使用django rest框架的TokenAuthentication验证。注销时,删除上一个令牌并重新创建该令牌。现在我想使用django_cron来实现令牌过期和注销。如何在Django Cron中获取当前用户信息?
您可以遵循以下步骤https://django-cron.readthedocs.io/en/latest/installation.html和内部
from rest_framework.authtoken.models import Token
from datetime import timedelta, datetime
class MyCronJob(CronJobBase):
RUN_EVERY_MINS = 120 # every 2 hours
Expiration = timedelta(days=1) # whate ever you want or get from settings.py
now = datetime.now()
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'my_app.my_cron_job' # a unique code
def do(self):
tokens = Token.objects.filter(created__lt=now-Expiration)
# now to expires it you need to delete it
tokens.delete()
这就是如何与合作的想法