susioma/smsauth/views.py

31 lines
975 B
Python
Raw Normal View History

2019-10-27 15:20:28 +00:00
from django.shortcuts import render
from django.utils import timezone
from datetime import timedelta
from smsauth.models import Token
from dbsettings.views import getValue
2020-10-24 14:50:04 +00:00
from kumisms import KumiSMS
2019-10-27 15:20:28 +00:00
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)
2020-10-24 14:50:04 +00:00
return sendSMS("Bitte verwende diesen Code, um deine Anfrage zu bestätigen: %s" % token, getValue("smsauth.recipient"))
2019-10-27 15:20:28 +00:00
2020-02-01 15:05:10 +00:00
def sendSMS(text, recipient):
2020-10-24 14:50:04 +00:00
gateway = KumiSMS(getValue("smsauth.kumisms.key"))
return gateway.send(recipient, text)