application / app.py
Dooratre's picture
Update app.py
8a420b0 verified
from flask import Flask, render_template, request, redirect, url_for, jsonify, flash
from db import signals as signals_module
from db import deeper as deeper_module
from db import paires as paires_module
import json
app = Flask(__name__)
app.secret_key = 'trading_signals_secret_key'
@app.route('/')
def index():
# Fetch signals from GitHub
signals_response = signals_module.fetch_json_from_github()
deeper_response = deeper_module.fetch_json_from_github()
paires_response = paires_module.fetch_json_from_github()
signals_data = []
system_status = {
'deeper': False,
'paires': False
}
if signals_response['success']:
signals_data = signals_response['data']
if deeper_response['success']:
system_status['deeper'] = deeper_response['data'].get('status', False)
if paires_response['success']:
system_status['paires'] = paires_response['data'].get('status', False)
return render_template('index.html',
signals=signals_data,
system_status=system_status)
@app.route('/signals', methods=['GET'])
def get_signals():
# Fetch signals from GitHub
response = signals_module.fetch_json_from_github()
if response['success']:
return jsonify(response['data'])
else:
return jsonify({"error": response['message']}), 500
@app.route('/signals/delete/<int:signal_id>', methods=['POST'])
def delete_signal(signal_id):
# Get current signals
response = signals_module.fetch_json_from_github()
if not response['success']:
flash('Failed to fetch signals data', 'error')
return redirect(url_for('index'))
signals_data = response['data']
# Check if signal_id is valid
if signal_id < 0 or signal_id >= len(signals_data):
flash('Invalid signal ID', 'error')
return redirect(url_for('index'))
# Remove the signal
signals_data.pop(signal_id)
# Update the signals.json file
auth_token, commit_oid = signals_module.fetch_authenticity_token_and_commit_oid()
if not auth_token or not commit_oid:
flash('Failed to get authentication tokens', 'error')
return redirect(url_for('index'))
# Convert the updated signals data to JSON string
new_content = json.dumps(signals_data, ensure_ascii=False)
# Update the signals.json file
update_response = signals_module.update_user_json_file(auth_token, commit_oid, new_content)
if update_response['success']:
flash('Signal deleted successfully', 'success')
else:
flash(f'Failed to delete signal: {update_response["message"]}', 'error')
return redirect(url_for('index'))
@app.route('/signals/delete_all', methods=['POST'])
def delete_all_signals():
# Get authentication tokens
auth_token, commit_oid = signals_module.fetch_authenticity_token_and_commit_oid()
if not auth_token or not commit_oid:
flash('Failed to get authentication tokens', 'error')
return redirect(url_for('index'))
# Create an empty array for signals
empty_signals = []
new_content = json.dumps(empty_signals, ensure_ascii=False)
# Update the signals.json file
update_response = signals_module.update_user_json_file(auth_token, commit_oid, new_content)
if update_response['success']:
flash('All signals deleted successfully', 'success')
else:
flash(f'Failed to delete all signals: {update_response["message"]}', 'error')
return redirect(url_for('index'))
@app.route('/signals/edit/<int:signal_id>', methods=['GET', 'POST'])
def edit_signal(signal_id):
# Get current signals
response = signals_module.fetch_json_from_github()
if not response['success']:
flash('Failed to fetch signals data', 'error')
return redirect(url_for('index'))
signals_data = response['data']
# Check if signal_id is valid
if signal_id < 0 or signal_id >= len(signals_data):
flash('Invalid signal ID', 'error')
return redirect(url_for('index'))
if request.method == 'POST':
# Update signal with form data
signals_data[signal_id]['pair'] = request.form['pair']
signals_data[signal_id]['timeframe'] = request.form['timeframe']
signals_data[signal_id]['type'] = request.form['type']
signals_data[signal_id]['entry'] = request.form['entry']
signals_data[signal_id]['stop_loss'] = request.form['stop_loss']
signals_data[signal_id]['take_profit'] = request.form['take_profit']
signals_data[signal_id]['duration'] = request.form['duration']
signals_data[signal_id]['reason'] = request.form['reason']
signals_data[signal_id]['status'] = request.form['status']
# Get authentication tokens
auth_token, commit_oid = signals_module.fetch_authenticity_token_and_commit_oid()
if not auth_token or not commit_oid:
flash('Failed to get authentication tokens', 'error')
return redirect(url_for('index'))
# Convert the updated signals data to JSON string
new_content = json.dumps(signals_data, ensure_ascii=False)
# Update the signals.json file
update_response = signals_module.update_user_json_file(auth_token, commit_oid, new_content)
if update_response['success']:
flash('Signal updated successfully', 'success')
return redirect(url_for('index'))
else:
flash(f'Failed to update signal: {update_response["message"]}', 'error')
return redirect(url_for('index'))
# GET request - display edit form with current signal data
return render_template('edit_signal.html', signal=signals_data[signal_id], signal_id=signal_id)
@app.route('/signals/add', methods=['GET', 'POST'])
def add_signal():
if request.method == 'POST':
# Get current signals
response = signals_module.fetch_json_from_github()
if not response['success']:
flash('Failed to fetch signals data', 'error')
return redirect(url_for('index'))
signals_data = response['data']
# Create new signal from form data
new_signal = {
'pair': request.form['pair'],
'timeframe': request.form['timeframe'],
'type': request.form['type'],
'entry': request.form['entry'],
'stop_loss': request.form['stop_loss'],
'take_profit': request.form['take_profit'],
'duration': request.form['duration'],
'reason': request.form['reason'],
'status': request.form['status']
}
# Add new signal to the beginning of the list
signals_data.insert(0, new_signal)
# Get authentication tokens
auth_token, commit_oid = signals_module.fetch_authenticity_token_and_commit_oid()
if not auth_token or not commit_oid:
flash('Failed to get authentication tokens', 'error')
return redirect(url_for('index'))
# Convert the updated signals data to JSON string
new_content = json.dumps(signals_data, ensure_ascii=False)
# Update the signals.json file
update_response = signals_module.update_user_json_file(auth_token, commit_oid, new_content)
if update_response['success']:
flash('Signal added successfully', 'success')
return redirect(url_for('index'))
else:
flash(f'Failed to add signal: {update_response["message"]}', 'error')
return redirect(url_for('index'))
# GET request - display add form
return render_template('add_signal.html')
@app.route('/system/toggle', methods=['POST'])
def toggle_system():
system_type = request.form.get('system_type')
new_status = request.form.get('status') == 'true'
if system_type == 'deeper':
# Update deeper.json
response = deeper_module.fetch_json_from_github()
if not response['success']:
return jsonify({"success": False, "message": "Failed to fetch deeper.json data"}), 500
deeper_data = response['data']
deeper_data['status'] = new_status
auth_token, commit_oid = deeper_module.fetch_authenticity_token_and_commit_oid()
if not auth_token or not commit_oid:
return jsonify({"success": False, "message": "Failed to get authentication tokens"}), 500
new_content = json.dumps(deeper_data, ensure_ascii=False)
update_response = deeper_module.update_user_json_file(auth_token, commit_oid, new_content)
return jsonify(update_response)
elif system_type == 'paires':
# Update paires.json
response = paires_module.fetch_json_from_github()
if not response['success']:
return jsonify({"success": False, "message": "Failed to fetch paires.json data"}), 500
paires_data = response['data']
paires_data['status'] = new_status
auth_token, commit_oid = paires_module.fetch_authenticity_token_and_commit_oid()
if not auth_token or not commit_oid:
return jsonify({"success": False, "message": "Failed to get authentication tokens"}), 500
new_content = json.dumps(paires_data, ensure_ascii=False)
update_response = paires_module.update_user_json_file(auth_token, commit_oid, new_content)
return jsonify(update_response)
else:
return jsonify({"success": False, "message": "Invalid system type"}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=False)