
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
    UserMixin, RoleMixin, login_required

from app import db, app

# Define models
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore) 

# Create a user to test with
@app.before_first_request
def create_user():
    db.create_all()
    user_datastore.create_user(email='foo@foo.bar', password='baz')
    db.session.commit()


@@ -90,24 +90,8 @@
 ixin
-, login_required
 %0A%0Afr
@@ -874,16 +874,1157 @@
 mic'))%0A%0A
+# Other models.%0Aclass Player(db.Model):%0A    id = db.Column(db.Integer, primary_key=True)%0A    nick = db.Column(db.String(80))%0A    go_server_id = db.Column(db.Integer, db.ForeignKey('go_server.id'))%0A    go_server = db.relationship('GoServer', backref=db.backref('players', lazy='dynamic'))%0A%0Aclass Key(db.Model):%0A    id = db.Column(db.Integer, primary_key=True)%0A    player_id = db.Column(db.Integer, db.ForeignKey('player.id'))%0A    player = db.relationship('Player', backref=db.backref('keys', lazy='dynamic'))%0A    go_server_id = db.Column(db.Integer, db.ForeignKey('go_server.id'))%0A    go_server = db.relationship('GoServer')%0A%0Aclass Result(db.Model):%0A    id = db.Column(db.Integer, primary_key=True)%0A    white_id = db.Column(db.Integer, db.ForeignKey('player.id'))%0A    black_id = db.Column(db.Integer, db.ForeignKey('player.id')) %0A    date_played = db.Column(db.DateTime)%0A    reported = db.Column(db.DateTime)%0A    rated = db.Column(db.Boolean)%0A    sgf = db.Column(db.LargeBinary)%0A%0Aclass GoServer(db.Model):%0A    id = db.Column(db.Integer, primary_key=True)%0A    name = db.Column(db.String(80), unique=True)%0A    url = db.Column(db.String(180))%0A%0A%0A
 # Setup 
@@ -2300,16 +2300,272 @@
 ='baz')%0A
+    user_datastore.create_role(name='user', description='default role')%0A    user_datastore.create_role(name='server_admin', description='Admin of a Go Server')%0A    user_datastore.create_role(name='ratings_admin', description='Admin of AGA-Online Ratings')%0A
     db.s

add some models, add some roles