from flask import Flask, render_template, request
from flask_bootstrap import Bootstrap
import db
import config
import utils

app = Flask(__name__)
app.config.from_object(config.BaseConfig)
Bootstrap(app)
my_db = db.Database(app.config['DB_PATH'])

brew_name = "Fruit Beer"
brew_month = "October"


@app.route('/')
def hello_world():
    return render_template('index.html', brew_name=brew_name, brew_month=brew_month)


@app.route('/generate', methods=["POST"])
def generate():
    try:
        ip = utils.get_ip(request)
        pass
    except:
        ip = ''
        pass

    if ip != '5.135.188.148' and ip != '178.32.58.160':
        try:
            error = None
            identifier = my_db.get_identifier(request.form['name'])
            pass
        except StopIteration:
            identifier = ''
            error = 'Maximum entry limit reached - please contact Sean or Joe'
            pass
    else:
        error = None
        identifier = 'F'

    return render_template('generate.html', brew_name=brew_name, brew_month=brew_month, identifier=identifier, error=error)


@app.route('/getip')
def getip():
    return utils.get_ip(request)


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')
