def days_until_goal(username): goal = user_get.get(username, "date_goal") difference = parser.parse(goal).replace( tzinfo=pytz.timezone(user_get.get(username, "timezone"))).astimezone( pytz.timezone("UTC")) - datetime.datetime.utcnow().replace( tzinfo=datetime.timezone.utc) if difference.days > 0: return difference.days else: return 0
def home(): error = request.args.get("error") return render_template( "Home.html", username=session["username"], running=user_get.get(session["username"], "activeDrive"), startTime=user_get.get_start_time(session["username"]), date_goal=user_get.get(session["username"], "date_goal"), settings_prefill=user_get.get_settings_prefill(session["username"]), error=error, stats=user_get.get_stats(session["username"]))
def stop_drive(username, time_mode): if user_check.check_username(username): users.update_one({"username": username}, {"$set": { "activeDrive": False }}) users.update_one({ "username": username, "drives.active": True }, {"$set": { "drives.$.stopTime": time_manage.iso_utc_now() }}) if time_mode == "night": users.update_one({ "username": username, "drives.active": True }, {"$set": { "drives.$.conditions": ["night"] }}) users.update_one({ "username": username, "drives.active": True }, {"$set": { "drives.$.active": False }}) return not user_get.get(username, "activeDrive") else: return False
def change_password(username, token): if user_get.get(username, "password_token") == token: return render_template("ChangePassword.html", token=token, username=username) else: return redirect("/login?error=reset_error")
def iso_utc_to_time(username, iso_utc): if user_check.check_username(username): date_object = parser.parse(iso_utc) timezone = user_get.get(username, "timezone") localized_time = date_object.astimezone(pytz.timezone(timezone)) return datetime.datetime.strftime(localized_time, "%I:%M %p") else: return "Error"
def verify_user(username, token): if user_check.check_username(username): if user_get.get(username, "verify_token") == token: users.update_one({"username": username}, {"$set": { "verified": True }}) return True else: return False else: return False
def date_start_stop_to_iso_utc(username, date, start_time, stop_time): if user_check.check_username(username): user_timezone = pytz.timezone(user_get.get(username, "timezone")) start_datetime = datetime.datetime.strptime(f"{date} {start_time}", "%b %d, %Y %I:%M %p") stop_datetime = datetime.datetime.strptime(f"{date} {stop_time}", "%b %d, %Y %I:%M %p") if start_datetime > stop_datetime: stop_datetime += datetime.timedelta(days=1) start_datetime = start_datetime.replace(tzinfo=user_timezone) stop_datetime = stop_datetime.replace(tzinfo=user_timezone) start_datetime = start_datetime.astimezone( pytz.timezone("UTC")).isoformat() stop_datetime = stop_datetime.astimezone( pytz.timezone("UTC")).isoformat() return {"start_date": start_datetime, "stop_date": stop_datetime}
def do_change_password(): if request.method == "POST": token = request.form["token"] username = request.form["username"] password = request.form["password"] repeat_password = request.form["password_repeat"] if not password == repeat_password: return redirect( user_get.get_change_password_error_link( username, token, "password_repeat")) elif not user_get.get(username, "password_token") == token: return redirect( user_get.get_change_password_error_link( username, token, "unknown")) else: user_manage.change_password(username, password) return redirect("/login?error=change_success")
def start_drive(username): if user_check.check_username(username): users.update_one({"username": username}, {"$set": { "activeDrive": True }}) id = len(users.find_one({"username": username})["drives"]) users.update_one({"username": username}, { "$push": { "drives": { "startTime": time_manage.iso_utc_now(), "stopTime": 0, "active": True, "conditions": [] } } }) return user_get.get(username, "activeDrive") else: return False
def send_email_confirmation(username): msg = EmailMessage() receiver = user_get.get(username, "email") msg["From"] = credentials.get_smtp_verify_address() msg["To"] = receiver msg["Subject"] = "Thanks for signing up!" random_bytes = urandom(64) token = b64encode(random_bytes).decode('utf-8') token = token.replace("+", "") token = token.replace("/", "") token = token.replace("=", "") verify_address = f"{credentials.get_site_url()}/verify/{username}/{token}" html = f"<html><head></head><body>Thanks for signing up!<br>To verify your account, click <a href=" \ f"'{verify_address}'>here</a>.<br>Thanks!<br>If you didn't request this email you can ignore it</body></html> " msg.set_content(html, "html") server = smtplib.SMTP(credentials.get_smtp_address(), 587) server.starttls() server.login(credentials.get_smtp_username(), credentials.get_smtp_password()) server.sendmail(credentials.get_smtp_verify_address(), receiver, msg.as_string()) server.quit() user_manage.associate_token(username, token)
def iso_utc_to_unix(username, iso_utc): if user_check.check_username(username): date_object = parser.parse(iso_utc) timezone = user_get.get(username, "timezone") localized_time = date_object.astimezone(pytz.timezone(timezone)) return localized_time.timestamp()
def date_local_to_iso_utc(username, date_local): if user_check.check_username(username): user_timezone = pytz.timezone(user_get.get(username, "timezone")) datetime_local = datetime.datetime.strptime(date_local, "%b %d, %Y") datetime_local = datetime_local.replace(tzinfo=user_timezone) return datetime_local