susioma/smsauth/views.py

30 lines
975 B
Python

from django.shortcuts import render
from django.utils import timezone
from datetime import timedelta
from smsauth.models import Token
from dbsettings.views import getValue
from kumisms import KumiSMS
import random
def generateToken():
return str(random.SystemRandom().choice(range(999999))).zfill(6)
def storeToken(token):
return Token.objects.create(token=token) # pylint: disable=E1101
def useToken(token):
retval = False
for t in Token.objects.filter(token=str(token).zfill(6)): # pylint: disable=E1101
t.used = True
if timezone.now() - timedelta(seconds=300) < t.sent:
retval = True
return retval
def requestToken():
token = generateToken()
storeToken(token)
return sendSMS("Bitte verwende diesen Code, um deine Anfrage zu bestätigen: %s" % token, getValue("smsauth.recipient"))
def sendSMS(text, recipient):
gateway = KumiSMS(getValue("smsauth.kumisms.key"))
return gateway.send(recipient, text)