use django_cron to achieve token expiration and log out

831 views Asked by At

I wrote a django that uses the TokenAuthentication verification of the django rest framework. When logging out, delete the previous token and recreate the token. Now I want to use django_cron to achieve token expiration and log out.How do I get current user information in Django Cron?

1

There are 1 answers

6
Mohamed Beltagy On

you can follow this one https://django-cron.readthedocs.io/en/latest/installation.html and inside

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()

this just idea how to work with you