def profile_page(): user_id = current_user.user_data() if request.method == 'POST': user = dict(request.form) if '' in user.values() or ' ' in user.values(): flash('Поля мають бути заповненими') return redirect(url_for('profile_page')) elif not user['firstName'].isalpha() or not user['lastName'].isalpha(): flash("Поле ім\'я або фамілія має містити лише букви") return redirect(url_for('profile_page')) elif '@' not in user['email'] or '.' not in user['email']: flash('Невірно введений формат електронної пошти') return redirect(url_for('profile_page')) elif user['password'] != user['password2']: flash('Введені паролі не рівні') return redirect(url_for('profile_page')) for key, values in user.items(): user[key] = values.strip() user['id'] = user_id['id'] user['password'] = generate_password_hash(user['password']) user.pop('password2') user['date'] = str(datetime.datetime.today())[:16] user_edit = DataBase(user) db_user = user_edit.edit_user() if db_user: flash(f"{user['username']}, Ви успішно змінили особисті дані") return redirect(url_for('user_page')) flash(f"Користувач з {user['email']} вже існує") return redirect(url_for('new_user_page')) return render_template('profile_user.html', title='Профіль', username=user_id)
def user_search(): user_id = current_user.user_data() if user_id: user_name = user_id.get('username') else: user_name = str(user_id) if request.method == 'POST': stock = [dict(request.form)] with open('stocks.json', 'r') as file_stocks: list_stocks = json.load(file_stocks) stocks = list_stocks['securities']['security'] for i in stocks: if i['symbol'] == stock[0]['symbol']: my_stocks = [i] print(my_stocks) data_symbol = [symbol_stocks(my_stocks[0]['symbol'])] if data_symbol: if data_symbol[0]['quotes']['quote']['change_percentage'] >= 0: my_stocks[0]['positive_change'] = True else: my_stocks[0]['positive_change'] = False my_stocks[0]['quote'] = data_symbol[0]['quotes']['quote'] print(my_stocks) return render_template('user.html', title=f'{user_name}', stocks=my_stocks) my_stocks = [{ 'Дані відсутні': f'Символ {my_stocks[0]["symbol"]} не знайдено' }] return render_template('user.html', title=f'{user_name}', stocks=my_stocks) return redirect(url_for('user_page'))
def logout_page(): user_id = current_user.user_data() if user_id: username = user_id.get('username') flash(f'{username}, Ви вийшли з кабінету.') logout_user() return redirect(url_for('main_page'))
def user_profit_page(): user_id = current_user.user_data() if user_id: user_name = user_id.get('username') else: user_name = str(user_id) user_profit = DataBase(user_id).take_user_profit() if user_profit: sum_profit = sum([+i['prevclose'] for i in user_profit]) if sum_profit < 0: flash( f'Отриманий збиток за період з {user_id["oper_date"]} по {str(datetime.datetime.today())[:16]}' ) else: flash( f'Отриманий прибуток за період з {user_id["oper_date"]} по {str(datetime.datetime.today())[:16]}' ) for i in user_profit: if i['prevclose'] > 0: i['positive_profit'] = True else: i['positive_profit'] = False else: flash(f"Ви не здійнили, ще жодної операції.") sum_profit = 0 user_profit = [{'Дані відсутні': 'Список операцій порожній'}] return render_template('user_profit.html', title=f'{user_name}', stocks=user_profit, sum_profit=sum_profit)
def del_profile_page(): user_id = current_user.user_data() if user_id: user_name = user_id.get('username') else: user_name = str(user_id) if request.method == 'POST': logout_user() del_user = DataBase(user_id).del_user() flash(f'Нажаль {user_name} вас видалено.') return redirect(url_for('main_page')) flash(f'Ви впевненні {user_name}, що хочете себе видалити') return render_template('delete_profile.html', title='Видалення профілю !!!', username=user_id)
def user_list_search(): user_id = current_user.user_data() if user_id: user_name = user_id.get('username') else: user_name = str(user_id) if request.method == 'POST': my_stocks = dict(request.form) my_stocks['symbol'] = str(my_stocks['symbol']).upper() user_id['stock'] = my_stocks user_stocks = DataBase(user_id).take_user_views_symbol() if user_stocks: i = 0 while i != len(user_stocks): price = user_stocks[i]['ask'] data_symbol = symbol_stocks(user_stocks[i]['symbol']) price_new = data_symbol['quotes']['quote']['bid'] delta = round((price_new - price) * 100, 2) user_stocks[i]['profit'] = delta user_stocks[i]['bid'] = price user_stocks[i]['ask'] = price_new user_stocks[i]['change_percentage'] = round( (price_new - price) / price * 100, 2) print(user_stocks) if delta >= 0: user_stocks[i]['positive_profit'] = True else: user_stocks[i]['positive_profit'] = False i += 1 sum_profit = sum([+i['profit'] for i in user_stocks]) return render_template('user_list.html', title=f'{user_name}', stocks=user_stocks, sum_profit=sum_profit) user_stocks = [{ 'Дані відсутні': f'Символ {my_stocks["symbol"]} не знайдено' }] sum_profit = 0 return render_template('user_list.html', title=f'{user_name}', stocks=user_stocks, sum_profit=sum_profit) return user_list()
def user_page(): user_id = current_user.user_data() if user_id: user_name = user_id.get('username') else: user_name = str(user_id) with open('stocks.json', 'r') as file_stocks: list_stocks = json.load(file_stocks) my_stocks = list_stocks['securities']['security'] my_stocks = sorted(my_stocks, key=lambda symbol: symbol['symbol']) i = 0 while i != amount_stock: data_symbol = symbol_stocks(my_stocks[i]['symbol']) if data_symbol['quotes']['quote']['change_percentage'] >= 0: my_stocks[i]['positive_change'] = True else: my_stocks[i]['positive_change'] = False my_stocks[i]['quote'] = data_symbol['quotes']['quote'] i += 1 print(my_stocks[0]) return render_template('user.html', title=f'{user_name}', stocks=my_stocks)
def user_list_del(): user_id = current_user.user_data() if user_id: user_name = user_id.get('username') else: user_name = str(user_id) if request.method == 'POST': my_stocks = dict(request.form) my_stocks = eval(my_stocks['stock']) my_stocks['trade_date'] = str(datetime.datetime.today())[:16] user_id['stock'] = my_stocks user_stocks = DataBase(user_id).del_user_views() if user_stocks: if my_stocks['profit'] >= 0: flash_text = f'100 шт {my_stocks["symbol"]} продано з прибутком {my_stocks["profit"]}' else: flash_text = f'100 шт {my_stocks["symbol"]} продано з збитком {my_stocks["profit"]}' else: flash_text = f"Сталась помилка з'єдання з базою даних." flash(flash_text) return redirect(url_for('user_list')) return render_template('index.html', title=f'{user_name}')
def user_list(): user_id = current_user.user_data() if user_id: user_name = user_id.get('username') else: user_name = str(user_id) if request.method == 'POST': my_stocks = dict(request.form) my_stocks = eval(my_stocks['stock']) my_stocks['trade_date'] = str(datetime.datetime.today())[:16] user_id['stock'] = my_stocks user_stocks = DataBase(user_id).add_user_views() if user_stocks: text_flash = 'Дані збережено' else: text_flash = f"Символ паперу {my_stocks['symbol']}: {my_stocks['description']} вже відслідковується" user_stocks = DataBase(user_id).take_user_views() i = 0 while i != len(user_stocks): price = user_stocks[i]['ask'] data_symbol = symbol_stocks(user_stocks[i]['symbol']) price_new = data_symbol['quotes']['quote']['bid'] delta = round((price_new - price) * 100, 2) user_stocks[i]['profit'] = delta user_stocks[i]['bid'] = price user_stocks[i]['ask'] = price_new user_stocks[i]['change_percentage'] = round( (price_new - price) / price * 100, 2) if delta >= 0: user_stocks[i]['positive_profit'] = True else: user_stocks[i]['positive_profit'] = False i += 1 user_stocks = sorted(user_stocks, key=lambda symbol: symbol['trade_date'], reverse=True) sum_profit = sum([+i['profit'] for i in user_stocks]) flash(f'{text_flash}') return render_template('user_list.html', title=f'{user_name}', stocks=user_stocks, sum_profit=sum_profit) user_stocks = DataBase(user_id).take_user_views() if not user_stocks: sum_profit = 0 user_stocks = [{'Дані відсутні': 'Список спостереження порожній'}] return render_template('user_list.html', title=f'{user_name}', stocks=user_stocks, sum_profit=sum_profit) i = 0 while i != len(user_stocks): price = user_stocks[i]['ask'] data_symbol = symbol_stocks(user_stocks[i]['symbol']) price_new = data_symbol['quotes']['quote']['bid'] delta = round((price_new - price) * 100, 2) user_stocks[i]['profit'] = delta user_stocks[i]['bid'] = price user_stocks[i]['ask'] = price_new user_stocks[i]['change_percentage'] = round( (price_new - price) / price * 100, 2) if delta >= 0: user_stocks[i]['positive_profit'] = True else: user_stocks[i]['positive_profit'] = False i += 1 user_stocks = sorted(user_stocks, key=lambda symbol: symbol['trade_date'], reverse=True) sum_profit = sum([+i['profit'] for i in user_stocks]) return render_template('user_list.html', title=f'{user_name}', stocks=user_stocks, sum_profit=sum_profit)