def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() flash('Congratulations, you are now a registered user!') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data,password=hashed_password) db.session.add(user) db.session.commit() flash('Account created','success') return redirect(url_for('home')) return render_template('register.html', title='Register',form=form)
def register(): if current_user.is_authenticated: return redirect(url_for("main.home")) form = RegisterForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode("utf-8") user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash("You are now Registered!", "success") return redirect(url_for("users.login")) return render_template("register.html", title="Register", form=form)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for("main.home")) user = User.verify_reset_token(token) if user is None: flash("Invalid/Expired token","danger") return redirect(url_for('users.reset_password_request')) form = Reset_password_form() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data).decode("utf-8") user.password = hashed_password db.session.commit() flash("Your password has been changed!","success") return redirect(url_for('users.login')) return render_template("reset_token.html",title="Reset Password",form=form)
def test_follow(self): u1=User(username='******',email='*****@*****.**') u2=User(username='******',email='*****@*****.**') db.session.add(u1) db.session.add(u2) db.session.commit() self.assertEqual(u1.followed.all(),[]) self.assertEqual(u1.followers.all(),[]) u1.follow(u2) db.session.commit() self.assertTrue(u1.is_following(u2)) self.assertEqual(u1.followed.count(),1) self.assertEqual(u1.followed.first().username, 'susan') self.assertEqual(u2.followers.count(),1) self.assertEqual(u2.followers.first().username, 'john') u1.unfollow(u2) db.session.commit() self.assertFalse(u1.is_following(u2)) self.assertEqual(u1.followed.count(),0) self.assertEqual(u2.followers.count(),0)
def register(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password, dept='student', designation='student') db.session.add(user) db.session.commit() flash('Your account has been created! You are now able to log in', 'success') return redirect(url_for('users.login')) return render_template('register.html', title='Register', form=form)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('main.home')) user = User.verify_reset_token(token) if user is None: flash('That is an invalid or expired token', 'warning') return redirect(url_for('users.reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Your password has been updated! You are now able to log in', 'success') return redirect(url_for('users.login')) return render_template('reset_token.html', title='Reset Password', form=form)
def register(): try: username = request.json['username'] password = request.json['password'] if (db.session.query(User).filter_by(name=username).first()): raise Exception("Username is already taken") for char in app.config['FORBIDDEN_CHARACTERS']: if char in username: raise Exception("Forbidden character in username: "******"Invalid password due to length < 8 characters") if password != request.json['password_confirm']: raise Exception("Password confirm does not match") for char in app.config['FORBIDDEN_CHARACTERS']: if char in password: raise Exception("Forbidden character in username: '******'") # sterilize input? cryptedPassword = sha256_crypt.hash(password) newU = User(name=username, password=cryptedPassword) if 'address' in request.json: newU.address = request.json['address'] if 'color' in request.json: newU.color = request.json['color'] newU.timestamp = int(time.time()) if 'image_filename' in request.json and request.json[ 'image_filename'] != '': dt = request.json['image_filename'].rsplit('.', 1)[1].lower() if not dt in app.config['IMAGE_EXTENSIONS']: raise Exception('File type not accepted, must be ' + ', '.join(app.config['IMAGE_EXTENSIONS'])) newU.image_filename = request.json['image_filename'] newU.image_type = dt else: #apply default image? # image paths used /api/downloads, these are in static... ''' birdPath = app.config['APP_DIRECTORY'] + 'static\\gulls' fn = random.choice([ x for x in os.listdir(birdPath) if os.path.isfile(os.path.join(birdPath, x)) ]) newU.image_filename = ... #fn newU.image_type = fn.rsplit('.', 1)[1].lower() ''' #create profile thread (no timestamp_close/timestamp_delete) pt = Thread(title="their Pleepline", \ timestamp=int(time.time()), \ creator=newU, \ creator_id=newU.id, \ write_access=False, \ permission_list = newU.name) db.session.add(pt) db.session.commit() newU.profile_thread_id = pt.id db.session.add(newU) db.session.commit() #pass get_flashed_messages() to the page and reload #flash('Register request completed', 'success') return pleep_resp(status=200) except Exception as err: #if err is thrown must delete possible upload if 'image_filename' in request.json and request.json[ 'image_filename'] != '': delete_upload(request.json['image_filename']) return pleep_resp(status=400, error=str(err))
def test_follow_posts(self): #create four users u1=User(username='******', email='*****@*****.**') u2=User(username='******', email='*****@*****.**') u3=User(username='******', email='*****@*****.**') u4=User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) #create four posts now = datetime.utcnow() p1= Post(body="post from john", author=u1, timestamp=now+timedelta(seconds=1)) p2= Post(body="post from susan", author=u2, timestamp=now + timedelta(seconds=4)) p3= Post(body="post from mary", author=u3, timestamp=now + timedelta(seconds=3)) p4= Post(body="post from david", author=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([p1,p2,p3,p4]) db.session.commit() #setiing up followers u1.follow(u2) u1.follow(u4) u2.follow(u3) u3.follow(u4) db.session.commit() #check the followed posts of each user f1= u1.followed_posts().all() f2= u2.followed_posts().all() f3= u3.followed_posts().all() f4= u4.followed_posts().all() self.assertEqual(f1, [p2,p4,p1]) self.assertEqual(f2, [p2,p3]) self.assertEqual(f3, [p3,p4]) self.assertEqual(f4, [p4])
def test_avatar(self): u=User(username='******', email='*****@*****.**') self.assertEqual(u.avatar(128),('https://www.gravatar.com/avatar/' 'd4c74594d841139328695756648b6bd6' '?d=identicon&s=128'))
def test_password_hashing(self): u=User(username='******') u.set_password('cat') self.assertFalse(u.check_password('dog')) self.assertTrue(u.check_password('cat'))