Search code examples
pythondjangodatetimetoken

Django timezone.now() remains unchanged


I'm looking for set token authentification in order to download server files with secure way and add after an expiration time.

Requirements :

  • Django 1.11
  • Ubuntu 18.04
  • Database PostgreSQL

Process :

User fills a form with some informations CustomerForm(email, ...) and he has to choose one or several document(s) with checkboxes.

When form is submitted, an email is sent with a generated token. This token has an expiration delay (1 minute in the first in order to make some tests).

Issue :

When user clicks on my link, it displays if token is in the list or not, datetime.now() and expiration_delay.

But, if I click one more time on the link (maybe 30s after the first one), datetime.now() still remains as the first one. It should change.

I suspect cookie to keep value inside, but I don't know if it's the right way.

My files :

I have a class in my view which let to fill the form, generate token and send the email.

Then, I have this new class which let to compare token with database and compare expiration_time with now() too.

class TokenDownloadView(TemplateView):
    template_name = 'app/token.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['token'] = self.kwargs['token']
        token = context['token']
        print(token)
        download = Download.objects.get(token__iexact=token)

        if download and download.expiration_date > now:
            print("token valide jusqu'à : " + str(download.expiration_date))
            print("il est actuellement : " + str(now))
            print(' ==> Token existe et valide <==')

        if download and download.expiration_date < now:
            print("token valide jusqu'à : " + str(download.expiration_date))
            print("il est actuellement : " + str(now))
            print('==> Token existe mais a expiré <==')

        return context

And this is what I obtain in my terminal in order to display what I said :

d0ce9328a53032d4484cccff4c0bdd92ad701567
token valide jusqu'à : 2018-09-12 07:46:30.082915+00:00
il est actuellement : 2018-09-12 07:45:30.082915+00:00
 ==> Token existe et valide <==
[12/Sep/2018 09:45:42] "GET /crud/download/token/d0ce9328a53032d4484cccff4c0bdd92ad701567/ HTTP/1.1" 200 7447
[12/Sep/2018 09:45:42] "GET /static/css/common-8073709e.css HTTP/1.1" 404 1682
d0ce9328a53032d4484cccff4c0bdd92ad701567
token valide jusqu'à : 2018-09-12 07:46:30.082915+00:00
il est actuellement : 2018-09-12 07:45:30.082915+00:00
 ==> Token existe et valide <==
[12/Sep/2018 09:46:10] "GET /crud/download/token/d0ce9328a53032d4484cccff4c0bdd92ad701567/ HTTP/1.1" 200 7447
[12/Sep/2018 09:46:10] "GET /static/css/common-8073709e.css HTTP/1.1" 404 1682
d0ce9328a53032d4484cccff4c0bdd92ad701567
token valide jusqu'à : 2018-09-12 07:46:30.082915+00:00
il est actuellement : 2018-09-12 07:45:30.082915+00:00
 ==> Token existe et valide <==
[12/Sep/2018 09:46:30] "GET /crud/download/token/d0ce9328a53032d4484cccff4c0bdd92ad701567/ HTTP/1.1" 200 7447
[12/Sep/2018 09:46:30] "GET /static/css/common-8073709e.css HTTP/1.1" 404 1682
d0ce9328a53032d4484cccff4c0bdd92ad701567
token valide jusqu'à : 2018-09-12 07:46:30.082915+00:00
il est actuellement : 2018-09-12 07:45:30.082915+00:00
 ==> Token existe et valide <==
[12/Sep/2018 09:46:41] "GET /crud/download/token/d0ce9328a53032d4484cccff4c0bdd92ad701567/ HTTP/1.1" 200 7447
[12/Sep/2018 09:46:41] "GET /static/css/common-8073709e.css HTTP/1.1" 404 1682

Do you have any idea ?


Solution

  • Any code at class or module level is executed when the module is first imported, so any values that are set in that code are set at that point. If you need values to be updated on every request, they need to be set inside a method. In this code moving your definition of now into the get_context_data method will do what you want.