def order(): order_form = OrderForm() if request.method == 'POST': #manually input correct order_type here form_data = json.loads(request.data) order_form.order_type.data = form_data['order_type'] if order_form.validate_on_submit(): ticker_name = format_ticker_name(order_form.order_ticker_name.data) order_type = order_form.order_type.data #just add entry here item = create_new_order_entry(ticker_name, order_form, form_type=order_type) ### update position entry here update_position(ticker_name, item, mode=order_type) current_position = Position.query.filter_by( user=current_user).all() #update summary row - instead of creating new one, just subtract current values from deleted row and current sum row return jsonify({'main_url': url_for('portfolio.main')}) else: return form_errors_400(order_form) else: redirect_next_page()
def change_settings(): changeSettingsForm = ChangeSettingsForm() changeSettingsForm.validate() errors = changeSettingsForm.errors if len(errors) == 0: current_user.currency = changeSettingsForm.currency.data db.session.commit() flash('Your settings has been changed!', 'success') return redirect_json(route='accounts.settings') else: return form_errors_400(changeSettingsForm)
def change_password(): #change password changePasswordForm = ChangePasswordForm() changePasswordForm.validate() errors = changePasswordForm.errors if len(errors) == 0: #change user password hashed_password = bcrypt.generate_password_hash( changePasswordForm.new_password.data).decode('utf-8') current_user.password = hashed_password db.session.commit() flash('Your password has been changed!', 'success') return redirect_json(route='accounts.settings') else: return form_errors_400(changePasswordForm)
def add(): add_form = AddForm() if request.method == 'POST' and confirm_post_request_form( request, add_form): if add_form.validate_on_submit(): #need to add the watchlist item into the db ticker_name = format_ticker_name(add_form.ticker_name.data) watchlist_add_item(ticker_name, user_id=current_user.get_id()) return new_item_json(TickerItem_Watchlist(ticker_name), table_class=WatchlistTable, include_id=False) else: return form_errors_400(add_form) else: return redirect_next_page()
def request_reset(): request_reset_form = RequestResetForm() if current_user.is_authenticated: return redirect(url_for('main.home')) if request.method == "POST" and confirm_post_request_form( request, request_reset_form): #need to pass in forms to _render_template if request_reset_form.validate_on_submit(): user = user_query(request_reset_form.email.data, return_user=True) if user: send_reset_email(user) return jsonify({'message': 'done!'}) else: return form_errors_400(request_reset_form) return redirect_next_page()
def login(): if current_user.is_authenticated: return redirect(url_for('main.home')) login_form = LoginForm() if request.method == "POST" and confirm_post_request_form( request, login_form): if login_form.validate_on_submit() and account_is_activated( login_form): print('login validated') user = user_query(login_form.email_username.data, return_user=True) login_user(user, remember=login_form.remember.data) flash('You have logged in successfully!', 'success') return redirect_json(route="main.home") else: return form_errors_400(login_form) return redirect_next_page()
def reset_password(token): if current_user.is_authenticated: return redirect(url_for('main.home')) user = User.verify_token(token, timed=True) if user is None: flash('The token is invalid or has expired.', 'warning') return redirect_next_page() reset_password_form = ResetPasswordForm() if request.method == "POST": if reset_password_form.validate_on_submit(): print('reset form submitted') hashed_password = bcrypt.generate_password_hash( reset_password_form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Password has been updated!', 'success') #redirect does not work with ajax, so instead return json then use js to switch url return redirect_json(route="main.home") else: return form_errors_400(reset_password_form) return _render_template('accounts/reset_password.html')
def register(): register_form = RegisterationForm() if request.method == "POST" and confirm_post_request_form( request, register_form): if register_form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( register_form.password.data).decode('utf-8') user = User(username=register_form.username.data, password=hashed_password, email=register_form.email.data) #add email verification here db.session.add(user) db.session.commit() send_verification_email(user) #clear form fields flash( 'Your account has been created! Please activate your account before logging in.', 'success') return redirect_json(route="main.home") else: print(register_form.errors) return form_errors_400(register_form) return redirect_next_page()