# Path: fbone/user/models.py
# class User(db.Model, UserMixin):
# 
#     __tablename__ = 'users'
# 
#     id = Column(db.Integer, primary_key=True)
#     name = Column(db.String(STRING_LEN), nullable=False, unique=True)
#     email = Column(db.String(STRING_LEN), nullable=False, unique=True)
#     openid = Column(db.String(STRING_LEN), unique=True)
#     activation_key = Column(db.String(STRING_LEN))
#     created_time = Column(db.DateTime, default=get_current_time)
# 
#     _password = Column('password', db.String(STRING_LEN), nullable=False)
# 
#     def _get_password(self):
#         return self._password
# 
#     def _set_password(self, password):
#         self._password = generate_password_hash(password)
#     # Hide password encryption by exposing password field only.
#     password = db.synonym('_password',
#                           descriptor=property(_get_password,
#                                               _set_password))
# 
#     def check_password(self, password):
#         if self.password is None:
#             return False
#         return check_password_hash(self.password, password)
# 
#     # ================================================================
#     role_code = Column(db.SmallInteger, default=USER, nullable=False)
# 
#     @property
#     def role(self):
#         return USER_ROLE[self.role_code]
# 
#     def is_admin(self):
#         return self.role_code == ADMIN
# 
#     # ================================================================
#     # One-to-many relationship between users and user_statuses.
#     status_code = Column(db.SmallInteger, default=INACTIVE)
# 
#     @property
#     def status(self):
#         return USER_STATUS[self.status_code]
# 
#     # ================================================================
#     # Class methods
# 
#     @classmethod
#     def authenticate(cls, login, password):
#         user = cls.query.filter(db.or_(User.name == login,
#                                        User.email == login)).first()
# 
#         if user:
#             authenticated = user.check_password(password)
#         else:
#             authenticated = False
# 
#         return user, authenticated
# 
#     @classmethod
#     def search(cls, keywords):
#         criteria = []
#         for keyword in keywords.split():
#             keyword = '%' + keyword + '%'
#             criteria.append(db.or_(
#                 User.name.ilike(keyword),
#                 User.email.ilike(keyword),
#             ))
#         q = reduce(db.and_, criteria)
#         return cls.query.filter(q)
# 
#     @classmethod
#     def get_by_id(cls, user_id):
#         return cls.query.filter_by(id=user_id).first_or_404()
# 
#     def check_name(self, name):
#         return User.query.filter(db.and_(User.name == name,
#                                          User.email != self.id)).count() == 0
# 
# Path: fbone/frontend/forms.py
# class SignupForm(Form):
#     next = HiddenField()
#     email = EmailField(u'Email',
#                        [Required(), Email()],
#                        description=u"What's your email address?")
#     password = PasswordField(u'Password',
#                              [Required(),
#                               Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX)],
#                              description=u'%s characters or more! Be tricky.' %
#                              PASSWORD_LEN_MIN)
#     name = TextField(u'Choose your username',
#                      [Required(),
#                       Length(USERNAME_LEN_MIN, USERNAME_LEN_MAX)],
#                      description=u"Don't worry. you can change it later.")
#     agree = BooleanField(u'Agree to the ' +
#                          Markup('<a target="blank" href="/terms">' +
#                                 'Terms of Service</a>'),
#                          [Required()])
#     submit = SubmitField('Sign up')
# 
#     def validate_name(self, field):
#         if User.query.filter_by(name=field.data).first() is not None:
#             raise ValidationError(u'This username is taken')
# 
#     def validate_email(self, field):
#         if User.query.filter_by(email=field.data).first() is not None:
#             raise ValidationError(u'This email is taken')
# 
# class LoginForm(Form):
#     next = HiddenField()
#     login = TextField(u'Username or email', [Required()])
#     password = PasswordField('Password',
#                              [Required(),
#                               Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX)])
#     remember = BooleanField('Remember me')
#     submit = SubmitField('Sign in')
# 
# class RecoverPasswordForm(Form):
#     email = EmailField(u'Your email', [Email()])
#     submit = SubmitField('Send instructions')
# 
# class ReauthForm(Form):
#     next = HiddenField()
#     password = PasswordField(u'Password',
#                              [Required(),
#                               Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX)])
#     submit = SubmitField('Reauthenticate')
# 
# class ChangePasswordForm(Form):
#     activation_key = HiddenField()
#     password = PasswordField(u'Password', [Required()])
#     password_again = PasswordField(u'Password again',
#                                    [EqualTo('password',
#                                             message="Passwords don't match")])
#     submit = SubmitField('Save')
# 
# class OpenIDForm(Form):
#     openid = TextField(u'Your OpenID', [Required()])
#     submit = SubmitField(u'Log in with OpenID')


from uuid import uuid4
from flask import (Blueprint, render_template, current_app, request,
                   flash, url_for, redirect, session, abort)
from flask.ext.mail import Message
from flask.ext.babel import gettext as _
from flask.ext.login import (login_required, login_user, current_user,
                             logout_user, confirm_login, login_fresh)
from ..user import User
from ..extensions import db, mail, login_manager, oid
from .forms import (SignupForm, LoginForm, RecoverPasswordForm, ReauthForm,
                    ChangePasswordForm, OpenIDForm)
# -*- coding: utf-8 -*-





frontend = Blueprint('frontend', __name__)


@frontend.route('/login/openid', methods=['GET', 'POST'])
@oid.loginhandler
def login_openid():
    if current_user.is_authenticated():
        return redirect(url_for('user.index'))


# Path: fbone/user/models.py
# class User(db.Model, UserMixin):
# 
#     __tablename__ = 'users'
# 
#     id = Column(db.Integer, primary_key=True)
#     name = Column(db.String(STRING_LEN), nullable=False, unique=True)
#     email = Column(db.String(STRING_LEN), nullable=False, unique=True)
#     openid = Column(db.String(STRING_LEN), unique=True)
#     activation_key = Column(db.String(STRING_LEN))
#     created_time = Column(db.DateTime, default=get_current_time)
# 
#     _password = Column('password', db.String(STRING_LEN), nullable=False)
# 
#     def _get_password(self):
#         return self._password
# 
#     def _set_password(self, password):
#         self._password = generate_password_hash(password)
#     # Hide password encryption by exposing password field only.
#     password = db.synonym('_password',
#                           descriptor=property(_get_password,
#                                               _set_password))
# 
#     def check_password(self, password):
#         if self.password is None:
#             return False
#         return check_password_hash(self.password, password)
# 
#     # ================================================================
#     role_code = Column(db.SmallInteger, default=USER, nullable=False)
# 
#     @property
#     def role(self):
#         return USER_ROLE[self.role_code]
# 
#     def is_admin(self):
#         return self.role_code == ADMIN
# 
#     # ================================================================
#     # One-to-many relationship between users and user_statuses.
#     status_code = Column(db.SmallInteger, default=INACTIVE)
# 
#     @property
#     def status(self):
#         return USER_STATUS[self.status_code]
# 
#     # ================================================================
#     # Class methods
# 
#     @classmethod
#     def authenticate(cls, login, password):
#         user = cls.query.filter(db.or_(User.name == login,
#                                        User.email == login)).first()
# 
#         if user:
#             authenticated = user.check_password(password)
#         else:
#             authenticated = False
# 
#         return user, authenticated
# 
#     @classmethod
#     def search(cls, keywords):
#         criteria = []
#         for keyword in keywords.split():
#             keyword = '%' + keyword + '%'
#             criteria.append(db.or_(
#                 User.name.ilike(keyword),
#                 User.email.ilike(keyword),
#             ))
#         q = reduce(db.and_, criteria)
#         return cls.query.filter(q)
# 
#     @classmethod
#     def get_by_id(cls, user_id):
#         return cls.query.filter_by(id=user_id).first_or_404()
# 
#     def check_name(self, name):
#         return User.query.filter(db.and_(User.name == name,
#                                          User.email != self.id)).count() == 0
# 
# Path: fbone/frontend/forms.py
# class SignupForm(Form):
#     next = HiddenField()
#     email = EmailField(u'Email',
#                        [Required(), Email()],
#                        description=u"What's your email address?")
#     password = PasswordField(u'Password',
#                              [Required(),
#                               Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX)],
#                              description=u'%s characters or more! Be tricky.' %
#                              PASSWORD_LEN_MIN)
#     name = TextField(u'Choose your username',
#                      [Required(),
#                       Length(USERNAME_LEN_MIN, USERNAME_LEN_MAX)],
#                      description=u"Don't worry. you can change it later.")
#     agree = BooleanField(u'Agree to the ' +
#                          Markup('<a target="blank" href="/terms">' +
#                                 'Terms of Service</a>'),
#                          [Required()])
#     submit = SubmitField('Sign up')
# 
#     def validate_name(self, field):
#         if User.query.filter_by(name=field.data).first() is not None:
#             raise ValidationError(u'This username is taken')
# 
#     def validate_email(self, field):
#         if User.query.filter_by(email=field.data).first() is not None:
#             raise ValidationError(u'This email is taken')
# 
# class LoginForm(Form):
#     next = HiddenField()
#     login = TextField(u'Username or email', [Required()])
#     password = PasswordField('Password',
#                              [Required(),
#                               Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX)])
#     remember = BooleanField('Remember me')
#     submit = SubmitField('Sign in')
# 
# class RecoverPasswordForm(Form):
#     email = EmailField(u'Your email', [Email()])
#     submit = SubmitField('Send instructions')
# 
# class ReauthForm(Form):
#     next = HiddenField()
#     password = PasswordField(u'Password',
#                              [Required(),
#                               Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX)])
#     submit = SubmitField('Reauthenticate')
# 
# class ChangePasswordForm(Form):
#     activation_key = HiddenField()
#     password = PasswordField(u'Password', [Required()])
#     password_again = PasswordField(u'Password again',
#                                    [EqualTo('password',
#                                             message="Passwords don't match")])
#     submit = SubmitField('Save')
# 
# class OpenIDForm(Form):
#     openid = TextField(u'Your OpenID', [Required()])
#     submit = SubmitField(u'Log in with OpenID')


# Path: fbone/frontend/views.py
from uuid import uuid4
from flask import (Blueprint, render_template, current_app, request,
                   flash, url_for, redirect, session, abort)
from flask.ext.mail import Message
from flask.ext.babel import gettext as _
from flask.ext.login import (login_required, login_user, current_user,
                             logout_user, confirm_login, login_fresh)
from ..user import User
from ..extensions import db, mail, login_manager, oid
from .forms import (SignupForm, LoginForm, RecoverPasswordForm, ReauthForm,
                    ChangePasswordForm, OpenIDForm)

# -*- coding: utf-8 -*-





frontend = Blueprint('frontend', __name__)


@frontend.route('/login/openid', methods=['GET', 'POST'])
@oid.loginhandler
def login_openid():
    if current_user.is_authenticated():
        return redirect(url_for('user.index'))


    form = OpenIDForm()