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 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)
|
2020-02-01 15:05:10 +00:00
|
|
|
return sendSMS("Bitte verwende diesen Code, um deine Bestellung 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):
|
2019-10-27 15:20:28 +00:00
|
|
|
client = Client(getValue("smsauth.twilio.sid"), getValue("smsauth.twilio.token"))
|
2020-02-01 15:05:10 +00:00
|
|
|
return client.messages.create(body=text, from_=getValue("smsauth.twilio.number"), to=recipient)
|