def donated():
    counter_file = os.path.join(current_path(),
                                'static/json_files/counter.json')
    templateData = {"title": "Thank You!", "donated": donate_check()}
    with open(counter_file, 'w') as fp:
        json.dump("donated", fp)
    return (render_template('warden/warden_thanks.html', **templateData))
def donate_check():
    counter_file = os.path.join(current_path(),
                                'static/json_files/counter.json')
    donated = False
    try:
        with open(counter_file) as data_file:
            json_all = json.loads(data_file.read())
        if json_all == "donated":
            donated = True
    except Exception:
        donated = False
    return (donated)
def data_folder():
    data_file = os.path.join(current_path(),
                             'static/json_files/specter_data_folder.json')

    if request.method == 'GET':
        try:
            with open(data_file) as data_file:
                return_data = json.loads(data_file.read())
        except Exception as e:
            return_data = str(e)
        return (json.dumps(return_data))

    if request.method == 'POST':
        results = request.form
        # Test this data folder
        try:
            data_folder = results['data_folder']
            # Test if can get specter data
            specter = specter_update(load=False, data_folder=data_folder)
            # Check Status
            is_configured = specter['is_configured']
            is_running = specter['is_running']
            wallets = specter['wallets']['wallets']

        except Exception as e:
            return json.dumps({"message": "Error: " + str(e)})

        message = ""
        ok_save = True
        if not is_configured:
            ok_save = False
            message += "Specter does not seem to be configured in this folder. "

        if not is_running:
            ok_save = False
            message += "Specter does not seem to be running in this folder. "

        if wallets == {}:
            ok_save = False
            message += "No wallets found - check folder."

        if ok_save:
            with open(data_file, 'w') as fp:
                json.dump(results, fp)
            message += "Specter Data folder found. Saved Successfully."

        return json.dumps({"message": message})
def fxsymbol(context, fx, output='symbol'):
    # Gets an FX 3 letter symbol and returns the HTML symbol
    # Sample outputs are:
    # "EUR": {
    # "symbol": "",
    # "name": "Euro",
    # "symbol_native": "",
    # "decimal_digits": 2,
    # "rounding": 0,
    # "code": "EUR",
    # "name_plural": "euros"
    try:
        from thewarden.users.utils import current_path
        filename = os.path.join(current_path(),
                                'static/json_files/currency.json')
        with open(filename) as fx_json:
            fx_list = json.load(fx_json, encoding='utf-8')
        out = fx_list[fx][output]
    except Exception:
        out = fx
    return (out)
def warden_page():

    # For now pass only static positions, will update prices and other
    # data through javascript after loaded. This improves load time
    # and refresh speed.
    # Get positions and prepare df for delivery
    df = positions()
    if df.index.name != 'trade_asset_ticker':
        df.set_index('trade_asset_ticker', inplace=True)
    df = df[df['is_currency'] == 0].sort_index(ascending=True)
    df = df.to_dict(orient='index')

    # Open Counter, increment, send data
    counter_file = os.path.join(current_path(),
                                'static/json_files/counter.json')
    donated = False
    try:
        with open(counter_file) as data_file:
            json_all = json.loads(data_file.read())
        if json_all == "donated":
            donated = True
        else:
            counter = int(json_all)
            counter += 1
            if counter == 25:
                flash(
                    "Looks like you've been using the app frequently. " +
                    "Awesome! Consider donating.", "info")
            if counter == 50:
                flash(
                    "Open Source software is transparent and free. " +
                    "Support it. Make a donation.", "info")
            if counter == 200:
                flash(
                    "Looks like you are a frequent user of the WARden. " +
                    "Have you donated?", "info")
            if counter >= 1000:
                flash(
                    "You've opened this page 1,000 times or more. " +
                    "Really! Time to make a donation?", "danger")
            with open(counter_file, 'w') as fp:
                json.dump(counter, fp)

    except Exception:
        # File wasn't found. Create start at zero
        if not donated:
            flash(
                "Welcome. Consider making a donation " +
                "to support this software.", "info")
            counter = 0
            with open(counter_file, 'w') as fp:
                json.dump(counter, fp)

    alerts = False
    meta = warden_metadata()
    if not meta['warden_enabled']:
        abort(500, 'WARden is not Enabled. Check your Connections.')
    if isinstance(meta['old_new_df_old'], pd.DataFrame):
        if not meta['old_new_df_old'].empty:
            alerts = True
    if isinstance(meta['old_new_df_new'], pd.DataFrame):
        if not meta['old_new_df_new'].empty:
            alerts = True

    templateData = {
        "title": "Portfolio Dashboard",
        "warden_metadata": meta,
        "warden_enabled": warden_metadata()['warden_enabled'],
        "portfolio_data": df,
        "FX": FX,
        "donated": donated,
        "alerts": alerts,
        "specter": specter_update(load=False)
    }
    return (render_template('warden/warden.html', **templateData))