# Path: template_maker/users/models.py
# class User(UserMixin, SurrogatePK, Model):
# 
#     __tablename__ = 'users'
#     email = Column(db.String(80), unique=True, nullable=False)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     first_name = Column(db.String(30), nullable=True)
#     last_name = Column(db.String(30), nullable=True)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
# 
#     def __init__(self, email, **kwargs):
#         db.Model.__init__(self, email=email, **kwargs)
# 
#     @property
#     def full_name(self):
#         return "{0} {1}".format(self.first_name, self.last_name)
# 
#     def __repr__(self):
#         return '<User({email!r})>'.format(email=self.email)


import json
import urllib
import urllib2
from flask import (
    Blueprint, render_template, request, flash,
    current_app, abort, redirect, url_for
)
from flask.ext.login import current_user, login_user, logout_user
from template_maker.users.models import User
@blueprint.route("/login", methods=["GET"])
def login():
    user = current_user if not current_user.is_anonymous() else dict(email=None)
    return render_template("users/login.html", current_user=user)

@blueprint.route('/logout', methods=['GET', 'POST'])
def logout():
    logout_user()
    if request.args.get('persona', None):
        return 'OK'
    else:
        flash('Logged out successfully!', 'alert-success')
        return render_template('users/logout.html')

@blueprint.route('/auth', methods=['POST'])
def auth():
    data = urllib.urlencode({
        'assertion' : request.form.get('assertion'),
        'audience': current_app.config.get('BROWSERID_URL')
    })
    req = urllib2.Request('https://verifier.login.persona.org/verify', data)

    response = json.loads(urllib2.urlopen(req).read())

    if response.get('status') != 'okay':
        abort(403)

    next_url = request.args.get('next', None)

    email = response.get('email')

# Path: template_maker/users/models.py
# class User(UserMixin, SurrogatePK, Model):
# 
#     __tablename__ = 'users'
#     email = Column(db.String(80), unique=True, nullable=False)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     first_name = Column(db.String(30), nullable=True)
#     last_name = Column(db.String(30), nullable=True)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
# 
#     def __init__(self, email, **kwargs):
#         db.Model.__init__(self, email=email, **kwargs)
# 
#     @property
#     def full_name(self):
#         return "{0} {1}".format(self.first_name, self.last_name)
# 
#     def __repr__(self):
#         return '<User({email!r})>'.format(email=self.email)


# Path: template_maker/users/views.py
import json
import urllib
import urllib2
from flask import (
    Blueprint, render_template, request, flash,
    current_app, abort, redirect, url_for
)
from flask.ext.login import current_user, login_user, logout_user
from template_maker.users.models import User

@blueprint.route("/login", methods=["GET"])
def login():
    user = current_user if not current_user.is_anonymous() else dict(email=None)
    return render_template("users/login.html", current_user=user)

@blueprint.route('/logout', methods=['GET', 'POST'])
def logout():
    logout_user()
    if request.args.get('persona', None):
        return 'OK'
    else:
        flash('Logged out successfully!', 'alert-success')
        return render_template('users/logout.html')

@blueprint.route('/auth', methods=['POST'])
def auth():
    data = urllib.urlencode({
        'assertion' : request.form.get('assertion'),
        'audience': current_app.config.get('BROWSERID_URL')
    })
    req = urllib2.Request('https://verifier.login.persona.org/verify', data)

    response = json.loads(urllib2.urlopen(req).read())

    if response.get('status') != 'okay':
        abort(403)

    next_url = request.args.get('next', None)

    email = response.get('email')

    user = User.query.filter(User.email==email).first()