def send_token(): if request.method == 'POST': form_value = request.form mail = form_value["email"] number = form_value["token"] result = tokens.give_token(mail, number) number_ = conn.connect_to_tokens().count({"isUsed": False}) if result is True: return render_template( 'send_token.html', token=tokens.get_token(), message="You have successfully sent the token.", tokens=number_) else: return render_template( 'send_token.html', token=tokens.get_token(), message= "The email is already registered or the token is incorrect.", tokens=number_) else: number_ = conn.connect_to_tokens().count({"isUsed": False}) return render_template('send_token.html', token=tokens.get_token(), tokens=number_)
def get_existing_tokens(): the_list = [] token_list = list(connect_to_tokens().find({}, {'_id': 0, 'isUsed': 0})) for el in token_list: if el not in the_list: the_list.append(el['token']) return the_list
def give_token(email, number): result_ = False user = connect_to_users().find_one({'email': email}) if number is not None and user is None: lock.acquire() the_token = connect_to_tokens().find_one({'token': int(number)}) if the_token['isUsed'] is True: return False msg = "This is your token. It is important! Remember to save it. Use it to create the account: " + str( number) changed = connect_to_tokens().update_one(the_token, {"$set": { 'isUsed': True }}) lock.release() if changed.modified_count > 0: result_ = True send_mail(email, msg) return result_
def insert_1000_tokens(): _result = False existing_list = get_existing_tokens() the_list = [] for i in range(1000): number = round( random.randint(1, 600000) * datetime.datetime.now().microsecond + 0.121 / 55 / 0.02) if not any(d['token'] == number for d in the_list) and number not in existing_list: the_list.append({'token': number, 'isUsed': False}) _result = True else: _result = False break if _result is True: connect_to_tokens().insert_many(the_list) else: insert_1000_tokens() return _result
def get_token(): token = connect_to_tokens().find_one({'isUsed': False}) number = token['token'] return number