def update_user_account_fields(user_id, current_user, parent_trade): cursor = connection.cursor() converted_amount = Helper.convert_currency(parent_trade['amount'], parent_trade['amount_currency'], current_user['currency']) updated_amount = float(current_user['vcurrency']) - float(converted_amount) # update account balance update_user = f"UPDATE user_by_id SET vcurrency = {updated_amount} WHERE id = {user_id}" cursor.execute(update_user)
def save_stats(current_user, parent_trade, selected_level): converted_amount = Helper.convert_currency(parent_trade['amount'], parent_trade['amount_currency'], current_user['currency']) Helper.store_state_value(current_user['id'], StatKeys.BALANCE.value, converted_amount, 'subtract') Helper.store_state_value(current_user['id'], StatKeys.NUM_TRADES.value, 1, 'add') Helper.store_state_value(current_user['id'], StatKeys.LEVELS.value, 1, 'add') Trading.save_purchase_stats(current_user['id'], parent_trade['purchase_type']) Trading.save_levels_stats(current_user['id'], selected_level)
def persist_transactions_by_state(transaction_id, user_id, parent_trade): current_user = Helper.get_user_by_id(user_id) converted_amount = Helper.convert_currency(parent_trade['amount'], parent_trade['amount_currency'], current_user['currency']) fields = "(transaction_id,user_id,currency,purchase_type,outcome,status, created_date, amount, trade_type)" values = f"({transaction_id}, {user_id},'{parent_trade['currency']}','{parent_trade['purchase_type']}'," \ f"'{parent_trade['outcome']}','{parent_trade['status']}','{created_date}',{converted_amount}," \ f"'{parent_trade['trade_type']}')" transactions_by_state = f"INSERT INTO transactions_by_state {fields} VALUES {values}" cursor = connection.cursor() cursor.execute(transactions_by_state)
def persist_user_transactions(user_id, transaction_id, parent_trade, selected_level): current_user = Helper.get_user_by_id(user_id) converted_amount = Helper.convert_currency(parent_trade['amount'], parent_trade['amount_currency'], current_user['currency']) price_range = get_selected_level_price(parent_trade['levels_price'], selected_level) available_levels = get_available_levels(parent_trade['available_levels'], selected_level) level_owners = add_level_owners(parent_trade['level_owners'], user_id, selected_level) start_time = Helper.get_time_formatted(parent_trade['start_time']) end_time = Helper.get_time_formatted(parent_trade['end_time']) changes_allowed_time = Helper.get_time_formatted( parent_trade['changes_allowed_time']) fields = "(transaction_id,user_id,created_date,trade_type,purchase_type,currency,staring_price," \ "amount,amount_currency,start_time,end_time,changes_allowed_time,outcome,status,level_pips,levels_price,level_owners," \ "join_date,level_start_price,level_end_price,level_selected,created_by,parent_id,child,available_levels)" values = f"({transaction_id},{user_id},'{created_date}','{parent_trade['trade_type']}'," \ f"'{parent_trade['purchase_type']}','{parent_trade['currency']}',{parent_trade['staring_price']}," \ f"{converted_amount},'{current_user['currency']}','{start_time}','{end_time}'," \ f"'{changes_allowed_time}','{parent_trade['outcome']}','{parent_trade['status']}'," \ f"{parent_trade['level_pips']},'{parent_trade['levels_price']}'," \ f"'{level_owners}'," \ f"'{Helper.get_current_time_formatted()}',{price_range['range'][0]},{price_range['range'][1]}," \ f"{selected_level},{parent_trade['created_by']},{parent_trade['user_id']},{True}, {available_levels})" user_transactions = f"INSERT INTO user_transactions {fields} VALUES {values}" cursor = connection.cursor() cursor.execute(user_transactions) Trading.save_levels_general_stats(user_id, selected_level, converted_amount, parent_trade['purchase_type'])
def get_transaction(request): ac = Authentication(request) # if user is not logged in response user not exist if not ac.is_user_logged_in(): return JsonResponse( Helper.get_json_response(False, [], ['Please login'])) post = request.POST trade = Trading.get_transaction_by_id(post['transaction_ref'], post['trade_owner']) if not trade: return JsonResponse( Helper.get_json_response(False, [], ["Trade not available"])) user_id = ac.get_user_session() current_user = Helper.get_user_by_id(user_id) trade_data = dict() trade_data['contract_type'] = trade['trade_type'] trade_data['transaction_ref'] = trade['transaction_id'] trade_data['purchase_type'] = trade['purchase_type'] trade_data['start_time'] = trade['start_time'] trade_data['end_time'] = trade['end_time'] if trade['trade_type'] == Types.LEVELS.value: trade_data['amount'] = Helper.convert_currency( trade['amount'], trade['amount_currency'], current_user['currency']) trade_data['selected_level'] = trade['level_selected'] trade_data['user_count'] = 4 - len(trade['available_levels']) trade_data['available_levels'] = trade['available_levels'] else: trade_data['amount'] = trade['amount'] trade_data['user_currency'] = current_user['currency'] trade_data['staring_price'] = trade['staring_price'] trade_data['closing_price'] = trade['closing_price'] trade_data['outcome'] = trade['outcome'] trade_data['status'] = trade['status'] return JsonResponse(Helper.get_json_response(True, [trade_data], []))