from flask import Flask, render_template, request, jsonify, json, flash
from flask_mail import Mail, Message
from forms import ContactForm


app = Flask(__name__)
app.secret_key = 'weidmanssecret'

# add mail server config
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'seth@sethweidman.com'
app.config['MAIL_PASSWORD'] = '**PASSWORD**'

mail = Mail(app)

@app.route('/app/contact', methods=('GET', 'POST'))
def contact():
    # form = ContactForm(data=request.form)
    # dir()
    form = ContactForm(obj=dict(request.form));
    # form.populate_obj(request.form)

    if request.method == 'POST':
        if form.validate() == False:

            return render_template('form.html', form=form)
        else:
            msg = Message("Message from your visitor " + form.name.data,
                          sender=form.email.data,
                          recipients=['seth@sethweidman.com'])
            msg.body = """
            From: %s <%s>,
            %s
            """ % (form.name.data, form.email.data, form.message.data)
            mail.send(msg)
            return "Successfully sent message!"
    elif request.method == 'GET':
        return render_template('form.html', form=form)

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