Reputation: 2080
models.py
@python_2_unicode_compatible
class UserAuthToken(models.Model):
email = models.ForeignKey(UserSubEmail)
token = models.CharField(max_length=34, unique=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "AuthToken for %s" % self.email
I want to check elapsed time between token's created time and now So if token created before more than 10 minutes, I can recognize this token is invalid.
views.py
def create_email_confirm_key(request, uid, token):
try:
user_authtoken = UserAuthToken.objects.get(uid=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user_authtoken = None
if not 'user_authtoken is created before more than 10minutes' :
This token is valid and do something
How can I check whether time between the token created
and now
is more than 10mins?
Upvotes: 0
Views: 28
Reputation: 73460
You can always calculate the delta from the current time using django.utils.timezone
:
from django.utils.timezone import now, timedelta
if now() - user_authtoken.created <= timedelta(seconds=10*60):
# token valid
But I recommend you take a look at django's Cryptographic signing. It lets you verify timestamped signatures and handles all the delta calculations for you.
Upvotes: 2