import os
from datetime import datetime
from flask import Flask, render_template, session, redirect, url_for, flash
from flask.ext.script import Manager, Shell
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SECRET_KEY'] = '123456seven'
app.config['SQLALCHEMY_DATABASE_URI'] =\
    'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)
migrate = Migrate(app, db)


def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role)

manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)


class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role %r>' % self.name

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return '<User %r>' % self.username


class NameForm(Form):
    name = StringField('What is your name?', validators=[Required()])
    submit = SubmitField('Submit')


@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.name.data).first()
        if user is None:
            user = User(username=form.name.data)
            db.session.add(user)
            session['known'] = False
        else:
            session['known'] = True
        session['name'] = form.name.data
        form.name.data = ''
        return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'),
                            known=session.get('known', False),
                            current_time=datetime.utcnow())

@app.route('/user/<name>')
def user(name):
    return render_template('user.html', name=name)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500


if __name__ == '__main__':
    manager.run()

@@ -329,16 +329,57 @@
 Command%0A
+from flask.ext.mail import Mail, Message%0A
 from fla
@@ -401,16 +401,16 @@
 rt Form%0A
-
 from wtf
@@ -752,16 +752,438 @@
  = True%0A
+app.config%5B'MAIL_SERVER'%5D = 'email-smtp.us-west-2.amazonaws.com'%0Aapp.config%5B'MAIL_PORT'%5D = 587%0Aapp.config%5B'MAIL_USE_TLS'%5D = True%0Aapp.config%5B'MAIL_USERNAME'%5D = os.environ.get('MAIL_USERNAME')%0Aapp.config%5B'MAIL_PASSWORD'%5D = os.environ.get('MAIL_PASSWORD')%0Aapp.config%5B'ZEEN_MAIL_SUBJECT_PREFIX'%5D = '%5Bzeen%5D'%0Aapp.config%5B'ZEEN_MAIL_SENDER'%5D = 'zeen Admin %3Czeen@hearn.me%3E'%0Aapp.config%5B'ZEEN_ADMIN'%5D = os.environ.get('ZEEN_ADMIN')%0A%0A
 %0Amanager
@@ -1294,16 +1294,33 @@
 pp, db)%0A
+mail = Mail(app)%0A
 %0A%0Adef ma
@@ -2537,16 +2537,177 @@
 = False%0A
+            if app.config%5B'ZEEN_ADMIN'%5D:%0A                send_email(app.config%5B'ZEEN_ADMIN'%5D, 'New User',%0A                           'mail/new_user', user=user)%0A
         
@@ -3296,24 +3296,24 @@
 r_error(e):%0A
-
     return r
@@ -3346,16 +3346,349 @@
 , 500%0A%0A%0A
+def send_email(to, subject, template, **kwargs):%0A    msg = Message(app.config%5B'ZEEN_MAIL_SUBJECT_PREFIX'%5D + subject,%0A                  sender=app.config%5B'ZEEN_MAIL_SENDER'%5D, recipients=%5Bto%5D)%0A    msg.body = render_template(template + '.txt', **kwargs)%0A    msg.html = render_template(template + '.html', **kwargs)%0A    mail.send(msg)%0A%0A%0A
 if __nam

send email to admin on user signup