def StockEditView(request, symbol): stock = Stock.objects.filter(portfolio=Portfolio.objects.get( user=request.user)).get(symbol=symbol) print(stock) form = StockForm(request.POST or None, instance=stock) if request.method == "POST": if form.is_valid(): stock = form.save() stock.save() return redirect('dashboard') jdata1 = requests.get( "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={}&apikey=Q3XZZ2CPZ3JM3VFY" .format(symbol)) data = jdata1.json() opened = data["Global Quote"]["02. open"] high = data["Global Quote"]["03. high"] low = data["Global Quote"]["04. low"] price = data["Global Quote"]["05. price"] change_percent = data["Global Quote"]["10. change percent"] return render( request, 'app/app_templates/stock_detail.html', { 'form': form, 'symbol': symbol, "opened": opened, "high": high, "low": low, "change_percent": change_percent, "price": price })
def add_stock(): if not current_user.is_authenticated: return redirect(url_for('login')) # add stock to database # declare the product form form = StockForm(request.form) form.product.choices = [(x.id, x.name) for x in Product.query.all()] form.stocktype.choices = [(x.id, x.name) for x in Stocktype.query.all()] msg = None if request.method == 'GET': form.process() return render_template('layouts/default.html', content=render_template('pages/add-stock.html', form=form, message=msg)) # check if both http method is POST and form is valid on submit if form.validate_on_submit(): # assign form data to variables cost_price = request.form.get('cost_price', '', type=float) sell_price = request.form.get('sell_price', '', type=float) quantity = request.form.get('quantity', '', type=int) product_id = request.form.get('product', '', type=int) stocktype_id = request.form.get('stocktype', '', type=int) # see if stock entry already exists stock = Stock.query.filter_by(product_id=product_id, quantity=quantity, stocktype_id=stocktype_id).first() # if stock: flash( f'Error: A stock entry for {stock.quantity} {stock.product} already exists!' ) else: stock = Stock(cost_price, sell_price, quantity, product_id, stocktype_id) stock.save() flash( f'Stock for {stock.product.name} successfully created! Return to stock page or add another stock.' ) else: flash('I am sorry but the details you entered cannot be saved :(') # print (msg) return render_template('layouts/default.html', content=render_template('pages/add-stock.html', message=msg, form=form))
def edit_stock(id): if not current_user.is_authenticated: return redirect(url_for('login')) # declare the Stock Form form = StockForm(request.form) form.product.choices = [(x.id, x.name) for x in Product.query.all()] form.stocktype.choices = [(x.id, x.name) for x in Stocktype.query.all()] msg = None # check if stock already exists stock = Stock.query.filter_by(id=id).first() form.product.default = stock.product_id form.stocktype.default = stock.stocktype_id if request.method == 'GET': form.process() return render_template('layouts/default.html', content=render_template('pages/edit-stock.html', form=form, stock=stock, message=msg)) # check if both http method is POST and form is valid on submit if form.validate_on_submit(): # assign form data to variables cost_price = request.form.get('cost_price', '', type=float) sell_price = request.form.get('sell_price', '', type=float) quantity = request.form.get('quantity', '', type=int) product_id = request.form.get('product', '', type=int) # if the requested product exists if stock: stock.cost_price = cost_price stock.sell_price = sell_price stock.quantity = quantity stock.product_id = product_id db.session().commit() flash( 'Stock item successfully created! Return to stock page or make further changes.' ) else: flash( f'Error: A stock entry for {stock.quantity} {stock.product} already exists' ) else: flash('I am sorry but the details you entered cannot be saved') return redirect('/psalm2vs8/stock')
def StockAddView(request, symbol): portfolio = get_object_or_404(Portfolio, user=request.user) form = StockForm(request.POST or None) if request.method == "POST": if form.is_valid(): stock = form.save(commit=False) jdata = requests.get( "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={}&apikey=Q3XZZ2CPZ3JM3VFY" .format(symbol)) data = jdata.json() stock.portfolio = portfolio stock.symbol = symbol stock.day_open = float(data['Global Quote']['02. open']) stock.day_high = float(data['Global Quote']['03. high']) stock.day_low = float(data['Global Quote']['04. low']) stock.price = float(data['Global Quote']['05. price']) stock.previous_close = float( data['Global Quote']['08. previous close']) stock.change = float(data['Global Quote']['09. change']) stock.change_percent = ( data['Global Quote']['10. change percent'][0:6]) stock.roc = stock.change_percent stock.save() return redirect('dashboard') jdata1 = requests.get( "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={}&apikey=Q3XZZ2CPZ3JM3VFY" .format(symbol)) data = jdata1.json() opened = data["Global Quote"]["02. open"] high = data["Global Quote"]["03. high"] low = data["Global Quote"]["04. low"] price = data["Global Quote"]["05. price"] change_percent = data["Global Quote"]["10. change percent"] return render( request, 'app/app_templates/stock_detail.html', { 'form': form, 'symbol': symbol, "opened": opened, "high": high, "low": low, "change_percent": change_percent, "price": price })
def index(): form = StockForm() if form.validate_on_submit(): ''' if not mp.check_valid_dates(form.stock_name1.data, form.start_date.data): flash('Invalid start date') return redirect(url_for('index')) if not mp.check_valid_dates(form.stock_name1.data, form.end_date.data): flash('Invalid end date') return redirect(url_for('index')) if not mp.check_valid_dates(form.stock_name2.data, form.start_date.data): flash('Invalid start date') return redirect(url_for('index')) if not mp.check_valid_dates(form.stock_name2.data, form.end_date.data): flash('Invalid end date') return redirect(url_for('index')) if not mp.check_valid_dates(form.stock_name3.data, form.start_date.data): flash('Invalid start date') return redirect(url_for('index')) if not mp.check_valid_dates(form.stock_name3.data, form.end_date.data): flash('Invalid end date') return redirect(url_for('index')) ''' #stock 1 checks if not mp.check_valid_stock(form.stock_name1.data): flash('Invalid stock entered.') return redirect(url_for('index')) if not mp.check_valid_investment_amount(form.init_invest1.data): flash('Invalid initial investment amount for stock 1') return redirect(url_for('index')) if not mp.check_valid_recurring_amount(form.recurring_invest1.data): flash('Invalid recurring investment amount for stock 1') return redirect(url_for('index')) #stock 2 checks if not mp.check_valid_stock(form.stock_name2.data): flash('Invalid stock entered') return redirect(url_for('index')) if not mp.check_valid_investment_amount(form.init_invest2.data): flash('Invalid initial investment amount for stock 2') return redirect(url_for('index')) if not mp.check_valid_recurring_amount(form.recurring_invest2.data): flash('Invalid recurring investment amount for stock 2') return redirect(url_for('index')) #stock 3 checks if not mp.check_valid_stock(form.stock_name3.data): flash('Invalid stock entered.') return redirect(url_for('index')) if not mp.check_valid_investment_amount(form.init_invest3.data): flash('Invalid initial investment amount for stock 3') return redirect(url_for('index')) if not mp.check_valid_recurring_amount(form.recurring_invest3.data): flash('Invalid recurring investment amount for stock 3') return redirect(url_for('index')) session['start_date'] = str(form.start_date.data) session['end_date'] = str(form.end_date.data) session['stock_name1'] = form.stock_name1.data session['init_invest1'] = str(form.init_invest1.data) session['recurring_invest1'] = str(form.recurring_invest1.data) session['frequency_of_recurring_invest1'] = form.frequency_of_recurring_invest1.data session['stock_name2'] = form.stock_name2.data session['init_invest2'] = str(form.init_invest2.data) session['recurring_invest2'] = str(form.recurring_invest2.data) session['frequency_of_recurring_invest2'] = form.frequency_of_recurring_invest2.data session['stock_name3'] = form.stock_name3.data session['init_invest3'] = str(form.init_invest3.data) session['recurring_invest3'] = str(form.recurring_invest3.data) session['frequency_of_recurring_invest3'] = form.frequency_of_recurring_invest3.data return redirect(url_for('plot')) return render_template('index.html', form=form)