Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… edition from interface
  • Loading branch information
Paul Bouquet committed Jul 24, 2018
1 parent 931f492 commit 00409be
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,5 @@ config\.json
*.csv

*.ods

temp_config\.json
6 changes: 6 additions & 0 deletions config/cst.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
CONFIG_PORTFOLIO_TOTAL = "total"

# Notification
CONFIG_NOTIFICATION_TYPE = "notification_type"
CONFIG_NOTIFICATION_INSTANCE = "notifier"
CONFIG_CATEGORY_NOTIFICATION = "notification"
CONFIG_NOTIFICATION_GLOBAL_INFO = "global_info"
Expand Down Expand Up @@ -224,6 +225,11 @@
STRATEGIES_REQUIRED_EVALUATORS = "required_evaluators"
TRADING_MODE_REQUIRED_STRATEGIES = "required_strategies"

# Web interface
UPDATED_CONFIG_SEPARATOR = "_"
GLOBAL_CONFIG_KEY = "global_config"
EVALUATOR_CONFIG_KEY = "evaluator_config"


class TentacleManagerActions(Enum):
INSTALL = 1
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog for 0.1.5_2-beta

**Warning** :
- You have to encrypt your exchange token : **please run python tools/temp_encrypt_tool.py**
- Notification type key changed from "type" to "notification_type"

# Concerned issues :
#269 [Tool] Implement ConfigManager
Expand Down
36 changes: 22 additions & 14 deletions interfaces/web/controllers/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,39 @@
from flask import render_template, request, jsonify

from config.cst import CONFIG_EXCHANGES, CONFIG_CATEGORY_SERVICES, CONFIG_CATEGORY_NOTIFICATION, \
CONFIG_TRADER, CONFIG_SIMULATOR, CONFIG_CRYPTO_CURRENCIES
CONFIG_TRADER, CONFIG_SIMULATOR, CONFIG_CRYPTO_CURRENCIES, GLOBAL_CONFIG_KEY, EVALUATOR_CONFIG_KEY
from interfaces import get_bot

from interfaces.web import server_instance
from interfaces.web.models.configuration import get_evaluator_config, update_evaluator_config, \
get_evaluator_startup_config, get_services_list, get_symbol_list
get_evaluator_startup_config, get_services_list, get_symbol_list, update_global_config, get_global_config
from interfaces.web.util.flask_util import get_rest_reply


@server_instance.route("/config")
@server_instance.route('/config', methods=['GET', 'POST'])
def config():
if request.method == 'POST':
update_type = request.args["update_type"]
if update_type == "evaluator_config":
request_data = request.get_json()
success = False

if request_data:
success = update_evaluator_config(request_data)

if success:
return get_rest_reply(jsonify(get_evaluator_config()))
else:
return get_rest_reply('{"update": "ko"}', 500)
request_data = request.get_json()
success = False

if request_data:
# update global config if required
if GLOBAL_CONFIG_KEY in request_data and request_data[GLOBAL_CONFIG_KEY]:
success = update_global_config(request_data[GLOBAL_CONFIG_KEY])

# update evaluator config if required
if EVALUATOR_CONFIG_KEY in request_data and request_data[EVALUATOR_CONFIG_KEY]:
success = update_evaluator_config(request_data[EVALUATOR_CONFIG_KEY])

if success:
# return get_rest_reply(jsonify({
# GLOBAL_CONFIG_KEY: get_global_config(),
# EVALUATOR_CONFIG_KEY: get_evaluator_config()
# }))
return get_rest_reply("")
else:
return get_rest_reply('{"update": "ko"}', 500)
else:
g_config = get_bot().get_config()
user_exchanges = [e for e in g_config[CONFIG_EXCHANGES]]
Expand Down
19 changes: 18 additions & 1 deletion interfaces/web/models/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
from tools.config_manager import ConfigManager


def get_global_config():
return get_bot().get_config()


def get_global_startup_config():
return get_bot().get_startup_config()


def get_evaluator_config():
return get_bot().get_config()[CONFIG_EVALUATOR]

Expand All @@ -15,14 +23,23 @@ def get_evaluator_startup_config():


def update_evaluator_config(new_config):
current_config = get_bot().get_config()[CONFIG_EVALUATOR]
current_config = get_evaluator_config()
try:
ConfigManager.update_evaluator_config(new_config, current_config)
return True
except Exception:
return False


def update_global_config(new_config):
current_config = get_global_config()
# try:
ConfigManager.update_global_config(new_config, current_config)
return True
# except Exception:
# return False


def get_services_list():
return [
service().get_type()
Expand Down
6 changes: 1 addition & 5 deletions interfaces/web/static/js/common/cst.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var config_value_attr = "config-value";
var current_value_attr = "current-value";
var startup_value_attr = "startup-config-value";
var update_url_attr = "update-url";
var config_type_attr = "config_type";
var config_type_attr = "config-type";
var config_root_class = "config-root";
var config_container_class = "config-container";
var config_element_class = "config-element";
Expand All @@ -27,8 +27,4 @@ var deactivated = "Deactivated";
// utility functions
function log(text){
window.console&&console.log(text);
}

function replace_eol_by_html(str){
return str.replace(/(?:\r\n|\r|\n)/g, '<br />');
}
8 changes: 8 additions & 0 deletions interfaces/web/static/js/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ function handle_editable(){
$(".editable").each(function(){
$(this).editable();
});
}

function replace_break_line(str, replacement=""){
return str.replace(/(?:\r\n|\r|\n)/g, replacement);
}

function replace_spaces(str, replacement=""){
return str.replace(/ /g, replacement);
}
19 changes: 17 additions & 2 deletions interfaces/web/static/js/components/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ function handle_save_buttons(){
var update_url = full_config.attr(update_url_attr);

full_config.find("."+config_element_class).each(function(){
var new_value = $(this).attr(current_value_attr)
var new_value = "";
var config_type = $(this).attr(config_type_attr);

if(!(config_type in updated_config)){
updated_config[config_type] = {};
}

if($(this)[0].hasAttribute(current_value_attr)){
new_value = $(this).attr(current_value_attr);
}else{
new_value = replace_spaces(replace_break_line($(this).text()));
}

if(new_value.toLowerCase() != $(this).attr(config_value_attr).toLowerCase() ){
updated_config[$(this).attr(config_key_attr)]=new_value;
updated_config[config_type][$(this).attr(config_key_attr)] = new_value;
}
})

Expand Down Expand Up @@ -62,6 +74,9 @@ function handle_evaluator_configuration_editor(){
});
}

function handle_global_configuration_editor(){
}

function reset_configuration_element(element){
var full_config = get_active_tab_config();
full_config.find("."+ config_element_class).each(function(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<div class="card-body">

<!--Title-->
<h4 class="card-title"><a href="#" id="crypto_currencies" data-type="text" data-pk="1" data-title="Enter cryptocurrency name"
class="editable editable-click" data-original-title="{{crypto_currency}}" title="{{crypto_currency}}">{{crypto_currency}}</a></h4>
<h4 class="card-title">{{crypto_currency}}</h4>

<p class="card-text symbols">
{% if crypto_currency in config_symbols %}
Expand All @@ -22,7 +21,6 @@ <h4 class="card-title"><a href="#" id="crypto_currencies" data-type="text" data-
{% endif %}
</p>

<button type="button" class="btn btn-warning edit-btn px-3"><i class="fa fa-edit" aria-hidden="true"></i> Edit</button>
<button type="button" class="btn btn-danger remove-btn px-3"><i class="fa fa-ban" aria-hidden="true"></i> Remove</button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% macro config_evaluator_card(evaluator_startup_config, evaluator_name, activated) -%}
<a href="#" onclick="return false;" class="list-group-item list-group-item-action col-sm-6 col-md-4 col-lg-3 {{'list-group-item-success' if activated else 'list-group-item-light'}} config-element evaluator-config-element" id={{evaluator_name}} config-key={{evaluator_name}} current-value={{activated}} config-value={{activated}} startup-config-value={{evaluator_startup_config[evaluator_name]}}>{{ evaluator_name }}
<a href="#" onclick="return false;" class="list-group-item list-group-item-action col-sm-6 col-md-4 col-lg-3 {{'list-group-item-success' if activated else 'list-group-item-light'}} config-element evaluator-config-element" id={{evaluator_name}} config-type="evaluator_config" config-key={{evaluator_name}} current-value={{activated}} config-value={{activated}} startup-config-value={{evaluator_startup_config[evaluator_name]}}>{{ evaluator_name }}
<span class="float-right">
<span class="badge {{'badge-warning' if evaluator_startup_config[evaluator_name] != activated else ('badge-success' if activated else 'badge-secondary')}}">{{('Activation pending restart' if activated else 'Deactivation pending restart') if evaluator_startup_config[evaluator_name] != activated else ('Activated' if activated else 'Deactivated')}}</span>
</span>
Expand Down
42 changes: 29 additions & 13 deletions interfaces/web/templates/components/config/exchange_card.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% macro config_exchange_card(exchange, add_class='') -%}
{% macro config_exchange_card(config, exchange, add_class='') -%}
<!-- Card -->
<div class="card mb-4 {{add_class}}">

Expand All @@ -11,26 +11,42 @@
<div class="card-body">

<!--Title-->
<h4 class="card-title"><a href="#" id="exchange" data-type="text" data-pk="1" data-title="Enter exchange name"
class="editable editable-click" data-original-title="{{exchange}}" title="{{exchange}}">{{exchange}}</a></h4>
<h4 class="card-title">{{exchange}}</h4>

<p class="card-text api">
API Key : <a href="#" id="exchange_api-key" data-type="text" data-pk="1" data-title="Enter api key"
class="editable editable-click">*********</a><br>
API Secret : <a href="#" id="exchange_api-secret" data-type="text" data-pk="1" data-title="Enter exchange api secret"
class="editable editable-click">************</a><br>
API Key : <a href="#"
id="exchange_api-key"
config-key="exchanges_{{exchange}}_api-key"
config-type="global_config"
config-value="*********"
startup-config-value="*********"
data-type="text"
data-pk="1"
data-title="Enter api key"
class="editable editable-click config-element">
*********</a><br>

API Secret : <a href="#"
id="exchange_api-secret"
config-key="exchanges_{{exchange}}_api-secret"
config-type="global_config"
config-value="************"
startup-config-value="************"
data-type="text"
data-pk="1"
data-title="Enter exchange api secret"
class="editable editable-click config-element">
************</a><br>
</p>

<p class="card-text websocket">
Websocket :
{% if "web_socket" in exchange and exchange["web_socket"] == True %}
<input checked type="checkbox">
{% else %}
<input type="checkbox">
{% endif %}
{% if "web_socket" in exchange and exchange["web_socket"] == True %}
<!--checked-->
{% endif %}
<input disabled type="checkbox">
</p>

<button type="button" class="btn btn-warning edit-btn px-3"><i class="fa fa-edit" aria-hidden="true"></i> Edit</button>
<button type="button" class="btn btn-danger remove-btn px-3"><i class="fa fa-ban" aria-hidden="true"></i> Remove</button>
</div>
</div>
Expand Down
4 changes: 1 addition & 3 deletions interfaces/web/templates/components/config/service_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<div class="card-body">

<!--Title-->
<h4 class="card-title"><a href="#" id="service" data-type="text" data-pk="1" data-title="Enter service name"
class="editable editable-click" data-original-title="{{service}}" title="{{service}}">{{service}}</a></h4>
<h4 class="card-title">{{service}}</h4>

<p class="card-text api">
UserName : <a href="#" id="service_user-name" data-type="text" data-pk="1" data-title="Enter service username"
Expand All @@ -26,7 +25,6 @@ <h4 class="card-title"><a href="#" id="service" data-type="text" data-pk="1" dat
class="editable editable-click">*********</a>
</p>

<button type="button" class="btn btn-warning edit-btn px-3"><i class="fa fa-edit" aria-hidden="true"></i> Edit</button>
<button type="button" class="btn btn-danger remove-btn px-3"><i class="fa fa-ban" aria-hidden="true"></i> Remove</button>
</div>
</div>
Expand Down
24 changes: 10 additions & 14 deletions interfaces/web/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ <h1>This page is currently in development</h1>
</div>

<!-- Card deck -->
<div class="card-deck">
<div class="card-deck config-container" update-url="{{ url_for('config') }}">
{% for exchange in config_exchanges %}
{{ m_config_exchange_card.config_exchange_card(exchange) }}
{{ m_config_exchange_card.config_exchange_card(config_exchanges, exchange) }}
{% endfor %}
</div>
</div>

<!--Panel Evaluators-->
<div class="tab-pane fade config-root" id="panelEvaluators" role="tabpanel"><br>

<div class="row config-container" update-url="{{ url_for('config', update_type='evaluator_config') }}" config_type="bool" id="evaluator-config-root">
<div class="row config-container" update-url="{{ url_for('config') }}" id="evaluator-config-root">
{% for evaluator_name, activated in get_evaluator_config().items() %}
{{ m_config_evaluator_card.config_evaluator_card(get_evaluator_startup_config(), evaluator_name, activated) }}
{% endfor %}
Expand All @@ -99,7 +99,7 @@ <h1>This page is currently in development</h1>
<!--Panel Trading-->
<div class="tab-pane fade config-root" id="panelTrading" role="tabpanel"><br>
<!-- Card deck -->
<div class="card-deck">
<div class="card-deck config-container" update-url="{{ url_for('config') }}">
{% for trader in ["Trader", "Trader Simulator"] %}
{{ m_config_trader_card.config_trader_card(trader) }}
{% endfor %}
Expand All @@ -109,7 +109,7 @@ <h1>This page is currently in development</h1>
<!--Panel Notifications-->
<div class="tab-pane fade config-root" id="panelNotifications" role="tabpanel"><br>
<!-- Card deck -->
<div class="card-deck">
<div class="card-deck config-container" update-url="{{ url_for('config') }}">
<!-- Card -->
<div class="card mb-4">

Expand Down Expand Up @@ -158,7 +158,7 @@ <h4 class="card-title">Notification type</h4>
</div>

<!-- Card deck -->
<div class="card-deck">
<div class="card-deck config-container" update-url="{{ url_for('config') }}">
{% for service in services_list %}
{{ m_config_service_card.config_service_card(service) }}
{% endfor %}
Expand All @@ -177,7 +177,7 @@ <h4 class="card-title">Notification type</h4>
</div>

<!-- Card deck -->
<div class="card-deck">
<div class="card-deck config-container" update-url="{{ url_for('config') }}">
{% for crypto_currency in config_symbols %}
{{ m_config_currency_card.config_currency_card(config_symbols, crypto_currency) }}
{% endfor %}
Expand All @@ -189,7 +189,7 @@ <h4 class="card-title">Notification type</h4>
<div class="d-none">
<!-- Exchange -->
<div id="AddExchange-template-default">
{{ m_config_exchange_card.config_exchange_card(exchange=config_default_value, add_class=modified_class) }}
{{ m_config_exchange_card.config_exchange_card(config=config_exchanges, exchange=config_default_value, add_class=modified_class) }}
</div>

<!-- Services -->
Expand All @@ -214,7 +214,9 @@ <h4 class="card-title">Notification type</h4>

handle_reset_buttons();
handle_save_buttons();

handle_evaluator_configuration_editor();
handle_global_configuration_editor();

$('#save-config').prop('disabled', true);
$('#reset-config').prop('disabled', true);
Expand All @@ -239,11 +241,5 @@ <h4 class="card-title">Notification type</h4>
$(this).remove();
});
});

// Card deck editing
$(".card").on("click", ".edit-btn", function() {
$()
});

</script>
{% endblock additional_scripts %}
Loading

0 comments on commit 00409be

Please sign in to comment.