def sell(self, ticker_symbol, trade_volume, username): user = User() holding = Holding() user_balance = user.get_current_balance(username) holding_volume = holding.get_holding_volume(username, ticker_symbol) last_price = Asset().get_last_price(ticker_symbol) transaction_value = (last_price * float(trade_volume)) - Holding.BROKERAGE_FEE if holding_volume >= trade_volume: # TODO Make inserts/updates in tables be part of the same DB transaction. # Inserts order OrderMapper().insert_order(username, ticker_symbol, datetime.now(), 'S', last_price, trade_volume, Holding.BROKERAGE_FEE) # Inserts/Updates holdings unit_price = transaction_value / float(trade_volume) holding.update_holdings(username, ticker_symbol, trade_volume, unit_price, 'S') # When selling, new balance must sum up transaction value, excluding fees new_balance = user_balance + \ (transaction_value - Holding.BROKERAGE_FEE) user.update_balance(username, new_balance) return 'SUCCESS' else: # TODO Improve return so could show current balance and transaction cost. return 'NO_FUNDS'
def buy(self, ticker_symbol, trade_volume, username): user = User() user_balance = user.get_current_balance(username) last_price = Asset().get_last_price(ticker_symbol) transaction_cost = (last_price * float(trade_volume)) + Holding.BROKERAGE_FEE if transaction_cost <= user_balance: # TODO Make inserts/updates in both tables be part of the same DB transaction. # Inserts order OrderMapper().insert_order(username, ticker_symbol, datetime.now(), 'B', last_price, trade_volume, Holding.BROKERAGE_FEE) # Inserts/Updates holdings unit_cost = transaction_cost / float(trade_volume) Holding().update_holdings(username, ticker_symbol, trade_volume, unit_cost, 'B') # Updates balance # When buying, new balance must subtract transaction cost (including brokerage fee) new_balance = user_balance - transaction_cost user.update_balance(username, new_balance) return 'SUCCESS' else: # TODO Improve return so could show current balance and transaction cost. return 'NO_FUNDS'
def home(): from core.model.user import User, Role if not session.get('logged_in'): return redirect(url_for('login')) else: email = session.get('user') query = "SELECT * FROM USER WHERE email='" + email + "'" conn = sqlite3.connect('app.db') c = conn.cursor() c.execute(query) result = c.fetchone() current_user = User() current_user.id = result[0] current_user.email = result[1] current_user.active = result[3] current_user.confirmed_at = result[4] query = "SELECT * FROM role WHERE id IN (SELECT role_id FROM roles_users WHERE user_id='" + str( current_user.id) + "');" c.execute(query) result = c.fetchone() current_user.roles = [] while result is not None: role = Role() role.name = result[1] role.description = result[2] current_user.roles.append(role) result = c.fetchone() return render_template('index.html', current_user=current_user) conn.close()
def get_accounts_data(self): users = User().get_user_list() user_accounts = [] for user in users: if user['profile'] == 'U': username = user['username'] user_accounts.append(self.get_account_data_by_user(username)) return user_accounts
def get_realized_pl(self, username): initial, current = User().get_balance_for_pl(username) if initial > 0: pl = {} pl['username'] = username pl['initial_balance'] = initial pl['cur_balance'] = current pl['pl_value'] = current - initial pl['pl_percent'] = (current - initial) / initial * 100 return pl else: return None
def read(row): entity = User() entity.id = row[0] entity.first_name = row[1] entity.last_name = row[2] entity.email = row[3] entities.append(entity)
def create_user(): user = User() user.id = str(uuid.uuid4()).upper() user.email = '*****@*****.**' user.first_name = 'peter' user.last_name = 'johnson' return user
def route_login(): form = ui.forms.auth.LoginForm() if form.validate_on_submit(): user = User.check_login(form.username.data, form.password.data) if user: flask_login.login_user(user) flask.flash('Logged in successfully.', 'success') next = flask.request.args.get('next') return flask.redirect( '/admin' ) #TODO update to use `next` (but must be filtered correctly) flask.flash('Invalid username/password.', 'danger') return flask.render_template('auth/login.html', form=form)
def __buy(symbol, volume, username): status = None error_detail = None try: # Executes the order and stores the status (SUCCESS or NO_FUNDS) status = Order().buy(symbol, int(volume), username) # TODO Make user see details about the transaction cost. # If user doesn't have enought funds, sets error_details with available balance. if status == 'NO_FUNDS': error_detail = User().get_current_balance(username) except Exception as e: status = 'EXCEPTION' error_detail = e.args[0] return render_template(html_filename, status=status, error_detail=error_detail)
def load_user(username): return User.get(username)
def route_users(): add_form = ui.forms.admin.users.AddForm(prefix='add_user') edit_form = ui.forms.admin.users.EditForm(prefix='edit') reset_pw_form = ui.forms.admin.users.ResetPasswordForm(prefix='reset_pw') delete_form = ui.forms.admin.users.DeleteForm(prefix='delete_user') # Add user form if add_form.submit.data and add_form.validate_on_submit(): try: user = User.add_and_notify(add_form.username.data, add_form.display_name.data, add_form.email.data, add_form.password.data, add_form.force_reset.data) flask.flash('Added user {} successfully.'.format(user.username), 'success') return flask.jsonify({'status': 'success'}) except UserError as e: add_form.username.errors.append(str(e)) if add_form.submit.data: return flask.jsonify({'status': 'errors', 'errors': add_form.errors}) # Edit user form if edit_form.submit.data and edit_form.validate_on_submit(): user = User.get(edit_form.username.data) if user: user.display_name = edit_form.display_name.data user.email = edit_form.email.data if edit_form.force_reset.data: user.reset_password = True user.update() flask.flash('Saved changes to {}.'.format(user.username), 'success') return flask.jsonify({'status': 'success'}) else: return flask.jsonify({ 'status': 'failed', 'reason': "That user doesn't exist" }) if edit_form.submit.data: return flask.jsonify({'status': 'errors', 'errors': edit_form.errors}) # Reset password form if reset_pw_form.submit.data and reset_pw_form.validate_on_submit(): print(reset_pw_form.username.data) user = User.get(reset_pw_form.username.data) if user: user.set_password(reset_pw_form.password.data) return flask.jsonify({'status': 'success'}) else: return flask.jsonify({ 'status': 'failed', 'reason': "That user doesn't exist" }) if reset_pw_form.submit.data: return flask.jsonify({ 'status': 'errors', 'errors': reset_pw_form.errors }) # Delete user form if delete_form.delete.data and delete_form.validate_on_submit(): user = User.get(delete_form.username.data) if user and user == flask_login.current_user: flask.flash("You can't delete your own user.", 'danger') elif user: user.delete() flask.flash('Deleted user {} successfully.'.format(user.username), 'success') else: flask.abort(400) return flask.render_template('admin/users.html', users=User.query.all(), add_form=add_form, edit_form=edit_form, reset_pw_form=reset_pw_form, delete_form=delete_form)
def try_login(username, password): user = User().login(username, password) return user != None, user