susioma/smsauth/views.py

30 lines
No EOL
1 KiB
Python

from django.shortcuts import render
from django.utils import timezone
from datetime import timedelta
from smsauth.models import Token
from twilio.rest import Client
from dbsettings.views import getValue
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 Bestellung zu bestätigen: %s" % token)
def sendSMS(text):
client = Client(getValue("smsauth.twilio.sid"), getValue("smsauth.twilio.token"))
return client.messages.create(body=text, from_=getValue("smsauth.twilio.number"), to=getValue("smsauth.recipient"))