0

我写了一个django,它使用了django rest框架的TokenAuthentication验证。注销时,删除之前的令牌并重新创建令牌。现在想用django_cron来实现token过期和注销。如何在Django Cron中获取当前用户信息?

4

1 回答 1

0

你可以按照这个 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()

这只是想法如何与您合作

于 2021-11-24T09:23:52.690 回答