def signup(): if request.method == "POST": username = request.form["username"] password = request.form["password"] verify = request.form["verify"] email = request.form["email"] if valid_username(username) and valid_password(password)\ and valid_email(email) and password == verify: user = User(username=username, email=email) user.password = password for exist in User.objects(username=username): if user.username == exist.username: flash(u"Username or Email already exist!!") return redirect("/login") for exist in User.objects(email=email): if user.email == exist.email: flash(u"Username or Email already exist!!") return redirect("/login") user.save() login_user(user) return redirect("/wiki/home") else: flash(u"Invalid Username or Password or Email!!") return redirect("/signup") return render_template("/auth/signup.html")
def post(self): have_error = False self.username = self.request.get('username') self.password = self.request.get('password') self.verify = self.request.get('verify') self.email = self.request.get('email') params = dict(username=self.username, email=self.email) if not valid_username(self.username): params['error_username'] = "******" have_error = True if not valid_password(self.password): params['error_password'] = "******" have_error = True elif self.password != self.verify: params['error_verify'] = "Your passwords didn't match." have_error = True if not valid_email(self.email): params['error_email'] = "That's not a valid email." have_error = True if have_error: self.render('signup-form.html', **params) else: self.done()
def sigup(): if flask.request.method == "GET": return flask.render_template("signup.html") elif flask.request.method == "POST": have_error = False email = flask.request.form.get('email') password = flask.request.form.get('password') verify = flask.request.form.get('verify') secretcode = flask.request.form.get('secretcode') params = dict(email=email, password=password, verify=verify, secretcode=secretcode) if not util.valid_email(email): params['error_email'] = "That's not a valid email." have_error = True else: # Check if this email has been already been registered. #### This is ugly! There must be a better way, and maybe cached emails = fetch_registered_emails() if email in emails: params[ 'error_email'] = "This email has already been registered." have_error = True if not util.valid_password(password): params['error_password'] = "******" have_error = True elif password != verify: params['error_verify'] = "Your passwords didn't match." have_error = True if not util.valid_secretcode(secretcode): params['error_secretcode'] = "secret code is wrong." have_error = True if have_error: return flask.render_template("signup.html", **params) else: pw_hash = util.make_pw_hash(email, password) account = Account(email, pw_hash, datetime.datetime.now()) db.session.add(account) db.session.commit() # FYI, url_for("report") will be return "/report", seems I don't # need url_for very much # u = flask.url_for("report") response = set_account_cookie_and_redirect(account.id, "/report") return response
def login(): if flask.request.method == "GET": account_cookie = flask.request.cookies.get('account') if util.valid_cookie_val(account_cookie): return flask.redirect("/report") else: return flask.render_template("login.html") elif flask.request.method == "POST": email = flask.request.form.get('email') password = flask.request.form.get('password') # I think, based on some experiment, checked checkbox will return a # unicode empty string, else it's None rmbme = flask.request.form.get('rmbme') params = dict(email=email, password=password) if not util.valid_email(email): params['error_email'] = "That's not a valid email." return flask.render_template("login.html", **params) else: emails = fetch_registered_emails() if email not in emails: params[ 'error_email'] = "This email hasn't been registered yet." return flask.render_template("login.html", **params) else: account = db.session.query(Account).filter( Account.email == email).all() account = account[0] if not util.correct_password(email, password, account.password): params['error_password'] = "******" return flask.render_template("login.html", **params) else: # Note: account.password is the hashed value if rmbme is None: #### it works here, but too messy about cookie setting #### by different urls cookie_val = util.make_secure_cookie_val( str(account.id)) response = flask.make_response( flask.redirect("/report")) response.headers[ 'Set-Cookie'] = 'account={0};Path=/;'.format( cookie_val) return response else: response = set_account_cookie_and_redirect( account.id, "/report") return response
def post(self): username = self.request.get('login') password = self.request.get('password') email = self.request.get('email') name = self.request.get('name') error_messages = [] if not util.valid_username(username): error_messages.append( "Please enter a valid username (more than 3 characters).") if used_username(username): error_messages.append("This username is already used") if not util.valid_password(password): error_messages.append( "Please enter a valid password (more than 3 characters, less than 40)" ) if not util.valid_email(email): error_messages.append("Please enter a valid email address") if not util.valid_name(name): error_messages.append( "Please enter your name in the following format: \"lastname firstname\", like \"John Doe\"." ) if len(error_messages) > 0: self.render('signup.html', error_messages=error_messages, username=username, email=email, name=name) else: #Put the account in datastore passhash = password_hash(password) account = Accounts(login=username, password=passhash, email_address=email, is_admin=False, is_teacher=False, name=name) account.put() #Set a cookie for the login and redirect to home self.response.headers.add_header( 'Set-Cookie', "user_id=" + id_cookie_generation(account.key().id()) + "; Path='/'") self.redirect('/')
def sigup(): if flask.request.method == "GET": return flask.render_template("signup.html") elif flask.request.method == "POST": have_error = False email = flask.request.form.get('email') password = flask.request.form.get('password') verify = flask.request.form.get('verify') secretcode = flask.request.form.get('secretcode') params = dict(email = email, password=password, verify = verify, secretcode=secretcode) if not util.valid_email(email): params['error_email'] = "That's not a valid email." have_error = True else: # Check if this email has been already been registered. #### This is ugly! There must be a better way, and maybe cached emails = fetch_registered_emails() if email in emails: params['error_email'] = "This email has already been registered." have_error=True if not util.valid_password(password): params['error_password'] = "******" have_error = True elif password != verify: params['error_verify'] = "Your passwords didn't match." have_error = True if not util.valid_secretcode(secretcode): params['error_secretcode'] = "secret code is wrong." have_error = True if have_error: return flask.render_template("signup.html", **params) else: pw_hash = util.make_pw_hash(email, password) account = Account(email, pw_hash, datetime.datetime.now()) db.session.add(account) db.session.commit() # FYI, url_for("report") will be return "/report", seems I don't # need url_for very much # u = flask.url_for("report") response = set_account_cookie_and_redirect(account.id, "/report") return response
def login(): if flask.request.method == "GET": account_cookie = flask.request.cookies.get('account') if util.valid_cookie_val(account_cookie): return flask.redirect("/report") else: return flask.render_template("login.html") elif flask.request.method == "POST": email = flask.request.form.get('email') password = flask.request.form.get('password') # I think, based on some experiment, checked checkbox will return a # unicode empty string, else it's None rmbme = flask.request.form.get('rmbme') params = dict(email = email, password=password) if not util.valid_email(email): params['error_email'] = "That's not a valid email." return flask.render_template("login.html", **params) else: emails = fetch_registered_emails() if email not in emails: params['error_email'] = "This email hasn't been registered yet." return flask.render_template("login.html", **params) else: account = db.session.query(Account).filter(Account.email==email).all() account = account[0] if not util.correct_password(email, password, account.password): params['error_password'] = "******" return flask.render_template("login.html", **params) else: # Note: account.password is the hashed value if rmbme is None: #### it works here, but too messy about cookie setting #### by different urls cookie_val = util.make_secure_cookie_val(str(account.id)) response = flask.make_response(flask.redirect("/report")) response.headers['Set-Cookie'] = 'account={0};Path=/;'.format(cookie_val) return response else: response = set_account_cookie_and_redirect( account.id, "/report") return response
def post(self): has_error = False self.username = self.request.get("username") self.password = self.request.get("password") self.confirm = self.request.get("verify") self.email = self.request.get("email") params = dict(username = self.username, email = self.email) if not valid_name(self.username): params["error_username"]="******" has_error = True if not valid_pass(self.password): params['error_password']='******' has_error = True if self.email !="": if not valid_email(self.email): params["error_email"] = "Invalid email" has_error = True if self.password != self.confirm: params["error_confirm"] = "Password does not match" has_error = True if has_error: self.render("signup.html", **params) else: u = User.by_name(self.username) if u: error = "Username already exists! Please choose another username" self.render("signup.html",error_username=error) else: user = User.register(self.username, self.password, self.email) user.put() self.login(user) self.redirect("/")
def post(self): username = self.request.get('login') password = self.request.get('password') email = self.request.get('email') name = self.request.get('name') error_messages = [] if not util.valid_username(username): error_messages.append("Please enter a valid username (more than 3 characters).") if used_username(username): error_messages.append("This username is already used") if not util.valid_password(password): error_messages.append("Please enter a valid password (more than 3 characters, less than 40)") if not util.valid_email(email): error_messages.append("Please enter a valid email address") if not util.valid_name(name): error_messages.append("Please enter your name in the following format: \"lastname firstname\", like \"John Doe\".") if len(error_messages) > 0: self.render('signup.html', error_messages=error_messages, username=username, email=email, name=name) else: #Put the account in datastore passhash = password_hash(password) account = Accounts(login=username, password=passhash, email_address=email, is_admin=False, is_teacher=False, name=name) account.put() #Set a cookie for the login and redirect to home self.response.headers.add_header('Set-Cookie', "user_id=" + id_cookie_generation(account.key().id()) + "; Path='/'") self.redirect('/')
def post(self): errors = {} # Get all values from signup form username = self.request.get("username") password = self.request.get("password") verify = self.request.get("verify") email = self.request.get("email") # Validate form data and return errors if invalid if not util.valid_username(username): errors["userError"] = "That isn't a valid username." if not util.valid_password(password): errors["passwordError"] = "That isn't a valid password." if not verify == password: errors["verifyError"] = "Your passwords don't match." if not util.valid_email(email): errors["emailError"] = "That isn't a valid email." # If errors exist render the page with the errors # If no errors exist redirect to welcome page if errors: errors["userValue"] = username errors["emailValue"] = email self.render('signup.html', **errors) else: # Create the user entity with validated data if email: user = Users(name = username, password = password, email = email) else: user = Users(name = username, password = password) user.put() # Generate a cookie storing user_id self.response.headers.add_header('Set-Cookie', 'user_id=%s; Path=/' % str(user.key().id())) #self.render('/wikihome.html', home="active", logged_in=True) self.redirect('/')
def post(self): if self.is_connected(): self.current_user = get_connected_user(self.request.cookies['user_id']) username = self.request.get('login') old_password = self.request.get('old_password') email = self.request.get('email') name = self.request.get('name') password = self.request.get('password') password_confirmation = self.request.get('password_confirmation') error_messages = [] if not util.valid_username(username): error_messages.append("Please enter a valid username (more than 3 characters).") if used_username(username) and username != self.current_user.login: error_messages.append("This username is already used") if not util.valid_password(old_password): error_messages.append("Please enter a valid password (more than 3 characters, less than 40)") if password and password_confirmation: if not util.valid_password(old_password): error_messages.append("Please enter a valid new password (more than 3 characters, less than 40)") if password != password_confirmation: error_messages.append("The two new password you entered are not equals.") if not util.valid_email(email): error_messages.append("Please enter a valid email address") if not util.valid_name(name): error_messages.append("Please enter your name in the following format: \"lastname firstname\", like \"John Doe\".") # Check the password key_to_check = user_connexion(self.current_user.login, old_password) if key_to_check is not None: if key_to_check == self.current_user.key().id(): # We have the right user pass else: error_messages.append("An unexpected error occurred. Please try again.") else: error_messages.append("You entered the wrong password.") if len(error_messages) > 0: # Errors have been found self.render('profile.html', error_messages=error_messages, user=self.current_user, new_login=username, new_email_address=email, new_name=name) else: # No error! Update time # Update the user self.current_user.login = username self.current_user.email_address = email self.current_user.name = name if password != "" and password == password_confirmation: # The user is changing password passhash = password_hash(password) self.current_user.password = passhash else: # The user is not changing password pass self.current_user.put() self.render('profile.html', updated=True, user=self.current_user, new_login=username, new_email_address=email, new_name=name) else: self.redirect('/')
def post(self): if self.is_connected(): self.current_user = get_connected_user( self.request.cookies['user_id']) username = self.request.get('login') old_password = self.request.get('old_password') email = self.request.get('email') name = self.request.get('name') password = self.request.get('password') password_confirmation = self.request.get('password_confirmation') error_messages = [] if not util.valid_username(username): error_messages.append( "Please enter a valid username (more than 3 characters).") if used_username(username) and username != self.current_user.login: error_messages.append("This username is already used") if not util.valid_password(old_password): error_messages.append( "Please enter a valid password (more than 3 characters, less than 40)" ) if password and password_confirmation: if not util.valid_password(old_password): error_messages.append( "Please enter a valid new password (more than 3 characters, less than 40)" ) if password != password_confirmation: error_messages.append( "The two new password you entered are not equals.") if not util.valid_email(email): error_messages.append("Please enter a valid email address") if not util.valid_name(name): error_messages.append( "Please enter your name in the following format: \"lastname firstname\", like \"John Doe\"." ) # Check the password key_to_check = user_connexion(self.current_user.login, old_password) if key_to_check is not None: if key_to_check == self.current_user.key().id(): # We have the right user pass else: error_messages.append( "An unexpected error occurred. Please try again.") else: error_messages.append("You entered the wrong password.") if len(error_messages) > 0: # Errors have been found self.render('profile.html', error_messages=error_messages, user=self.current_user, new_login=username, new_email_address=email, new_name=name) else: # No error! Update time # Update the user self.current_user.login = username self.current_user.email_address = email self.current_user.name = name if password != "" and password == password_confirmation: # The user is changing password passhash = password_hash(password) self.current_user.password = passhash else: # The user is not changing password pass self.current_user.put() self.render('profile.html', updated=True, user=self.current_user, new_login=username, new_email_address=email, new_name=name) else: self.redirect('/')