def registration(): if request.method == "POST": # Gets all the credentials from the form name = request.form.get("name") password = request.form.get("enter-password") confirmation = request.form.get("confirm-password") # Checks for any input errors if name == None: error = "PLEASE ENTER YOUR NAME" return render_template("error.html", v=0, error=error) elif password == None: error = "PLEASE ENTER YOUR PASSWORD" return render_template("error.html", v=0, error=error) elif confirmation == None: error = "PLEASE CONFIRM YOUR PASSWORD" return render_template("error.html", v=0, error=error) elif password != confirmation: error = "PASSWORDS DO NOT MATCH" return render_template("error.html", v=0, error=error) # Checks whether the user is already registered or not rows = db.execute("SELECT * FROM users WHERE name = :name", {"name":name}).fetchall() password = hash(password) if not len(rows) == 0: error = "USER ALREADY REGISTERED" return render_template("error.html", v=0, error=error) else : db.execute("INSERT INTO users (name, password) VALUES (:name, :password)", {"name":name, "password":(password)}) db.commit() return render_template("registered.html") else: # Renders the template of "registration.html" return render_template("registration.html")
def login(): if request.method == "POST": # Gets all the credentials from the form name = request.form.get("name") password = request.form.get("enter-password") # Checks for any input errors if name == None: error = "PLEASE ENTER YOUR NAME" return render_template("error.html", v=0, error=error) elif password == None: error = "PLEASE ENTER YOUR PASSWORD" return render_template("error.html", v=0, error=error) password = hash(password) # Checks whether the user is already registered or not rows = db.execute("SELECT * FROM users WHERE name = :name", {"name":name}).fetchall() if len(rows) == 0: error = "USER IS NOT REGISTERED" return render_template("error.html", v=0, error=error) elif not rows[0][2] == (password): error = "WRONG PASSWORD" return render_template("error.html", v=0, error=error) else : session["user_id"] = rows[0][0] return redirect("/") else: # Renders the template of "login.html" return render_template("login.html")
def login(cls, username, password, remember, session): user = User.query.filter_by(username=username.lower()).first() if user is None: return False, "Login failed, please try again." elif user.locked: return False, "Account locked, please contact your administrator." elif h.hash(password, PASSWORD_SALT) != user.password: user.failed_login_attempts += 1 if user.failed_login_attempts >= MAX_FAILED_LOGIN_ATTEMPTS: user.locked = True db.session.commit() return False, "Login failed, please try again." else: user.failed_login_attempts = 0 db.session.commit() # Stop the session from expiring when the browser closes session.permanent = True session["user_id"] = user.id session["last_activity"] = datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) session["remember"] = h.checkbox_to_boolean(remember) user.process_savings() return True, ""
def auth_reset2(): ''' Make sure that password is valid for password reset. ''' email = request.form.get('email', False) key = request.form.get('key', False) newPassword = request.form.get('password', False) if email and newPassword and helpers.isValidPassword(newPassword): models.changePassword(email, helpers.hash(newPassword)) return redirect(url_for('resetPassword2', succ=1)) return redirect(url_for('resetPassword2', e=email, k=key, r=1))
def env_user(): username = environ.get("USERNAME") if username is None: return password = environ.get("PASSWORD") if password is None: return existing_user = User.query.filter_by(username=username.lower()).first() if existing_user is None: db.session.add(User(username, password)) else: existing_user.password = h.hash(password, PASSWORD_SALT) db.session.commit()
def translate_struct(ref_lang, target_lang, ref_struct, target_struct, db, token, parent_key=None): for k, v in ref_struct.items(): kp = k.split("|") kr = kp[0] full_key = kr if parent_key is None else f'{parent_key}.{kr}' if isinstance(v, str): kh = hash(v) rh = db.get(target_lang, full_key) if rh == kh: continue # translation is still up-to-date print( f"Translating {full_key} from {ref_lang} to {target_lang}...") try: if target_lang == ref_lang: translation = deserialize_text(serialize_text(v)) else: translation = deserialize_text( translate(serialize_text(v), ref_lang, target_lang, token)) except: print("Cannot translate, skipping...") continue target_struct[k] = translation db.set(target_lang, full_key, kh) else: if not isinstance(target_struct.get(k), dict): target_struct[k] = {} translate_struct(ref_lang, target_lang, v, target_struct[k], db, token, parent_key=full_key) return target_struct
def translate_struct(ref_lang, target_lang, translations, db, token): if not target_lang in translations: translations[target_lang] = {} for k, v in translations.get('$' + ref_lang, {}).items(): kp = k.split("|") kr = kp[0] kh = hash(v) rh = db.get(target_lang, kr) if rh == kh: continue #translation is still good print(f"Translating {kr} from {ref_lang} to {target_lang}...") try: if target_lang == ref_lang: translation = deserialize_text(serialize_text(v)) else: translation = deserialize_text( translate(serialize_text(v), ref_lang, target_lang, token)) except KeyboardInterrupt: raise except: continue translations[target_lang][kr] = translation db.set(target_lang, kr, kh) return translations
def change_password(username, password): user = User.query.filter_by(username=username).first() user.password = hash(password, salt=PASSWORD_SALT) db.session.commit()
def add_user(username, password): password = hash(password, salt=PASSWORD_SALT) db.session.add(User(username=username, password=password)) db.session.commit()