main.py
from funtions import *
mail = get_mail()
username = make_user(mail)
active_user = check_user_is_active(username)
functions.py
import time
from datetime import timedelta, datetime
from db import *
dt = datetime.now()
data_now = dt.year, dt.month, dt.day
def get_mail(attempts=3, sleep_duration=10):
mail = input()
i = 1
while mail.find("@") == -1:
print("Вы ввели неверный e-mail. Введите правильный e-mail: ", end="")
i += 1
mail = input()
if i % attempts == 0:
print("Вы переборщили с попытками, подождите " + str(sleep_duration) + " секунд и попробуйте снова")
time.sleep(sleep_duration)
return mail
def make_user(mail):
return mail.split("@")[0].lower()
def check_user_is_active(user):
registred_user = list_users()
i = 0
while i < len(registred_user):
if user == registred_user[i][0]:
last_seen = query_user_last_seen(user)
data = data_now - timedelta(last_seen)
if timedelta(data.days) < 180:
return print("Вам надо подтвердить логин!")
else:
pod_data = datetime.now() + timedelta(days = 180)
return print("Ваш аккаунт подтверждён до ", pod_data)
break
i += 1
return print("Вы с нами впервые. Добро пожаловать! ", user)
db.py
import sqlite3
import random
from datetime import datetime as dt
class UsersDB:
__instance = None
@staticmethod
def get_instance():
if UsersDB.__instance is None:
UsersDB()
return UsersDB.__instance
def __init__(self):
if UsersDB.__instance is not None:
raise Exception("You're doing it wrong, don't do it")
else:
self.con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
self.init_db()
UsersDB.__instance = self
def init_db(self):
with self.con:
cur = self.con.cursor()
cur.execute(
"CREATE TABLE Users(Id INTEGER PRIMARY_KEY, username TEXT, last_seen timestamp)"
)
n = dt.now()
for i, (u, t) in enumerate(
[
("sergei", n.replace(year = 2021, month=8, day=3)),
("valentin87", n.replace(year = 2021, month=8, day=4)),
("igor99", n.replace(year = 2021, month=7, day=1)),
("anton4", n.replace(year = 2021, month=1, day=10)),
("vera2000", n.replace(year = 2021, month=4, day=14)),
("sunnyside", n.replace(year = 2021, month=2, day=24)),
("sarah.connor", n.replace(year = 2021, month=1, day=2)),
("andrey3", n.replace(year = 2021, month=4, day=21)),
("alexei22", n.replace(year = 2021, month=2, day=17)),
],
start=100,
):
h = random.randrange(0, 24)
m = random.randrange(0, 60)
s = random.randrange(0, 60)
us = random.randrange(0, 1000000)
t = t.replace(hour=h, minute=m, second=s, microsecond=us)
cur.execute("INSERT INTO Users values(?, ?, ?)", (i, u, t))
def list_users():
users = UsersDB.get_instance()
known_users = []
with users.con:
cur = users.con.cursor()
cur.execute("SELECT username, last_seen FROM Users")
known_users = cur.fetchall()
return known_users #[(u, t) for _, u, t in known_users]
def query_user_last_seen(username):
users = UsersDB.get_instance()
with users.con:
cur = users.con.cursor()
cur.execute("SELECT last_seen FROM Users where username=?", (username,))
results = cur.fetchone()
return results[0] if (results and len(results) > 0) else dt.now()