# 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/settings/forms.py
# class PasswordForm(Form):
#     next = HiddenField()
#     password = PasswordField('Current password', [Required()])
#     new_password = PasswordField('New password',
#                                  [Required(),
#                                   Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX)])
#     password_again = PasswordField('Password again',
#                                    [Required(),
#                                     Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX),
#                                     EqualTo('new_password')])
#     submit = SubmitField(u'Save')
# 
#     def validate_password(form, field):
#         user = User.get_by_id(current_user.id)
#         if not user.check_password(field.data):
#             raise ValidationError("Password is wrong.")


from flask import Blueprint, render_template, request, flash
from flask.ext.login import login_required, current_user
from ..extensions import db
from ..user import User
from .forms import PasswordForm
# -*- coding: utf-8 -*-




settings = Blueprint('settings', __name__, url_prefix='/settings')


@settings.route('/password', methods=['GET', 'POST'])
@login_required
def password():
    user = User.query.filter_by(name=current_user.name).first_or_404()

# 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/settings/forms.py
# class PasswordForm(Form):
#     next = HiddenField()
#     password = PasswordField('Current password', [Required()])
#     new_password = PasswordField('New password',
#                                  [Required(),
#                                   Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX)])
#     password_again = PasswordField('Password again',
#                                    [Required(),
#                                     Length(PASSWORD_LEN_MIN, PASSWORD_LEN_MAX),
#                                     EqualTo('new_password')])
#     submit = SubmitField(u'Save')
# 
#     def validate_password(form, field):
#         user = User.get_by_id(current_user.id)
#         if not user.check_password(field.data):
#             raise ValidationError("Password is wrong.")


# Path: fbone/settings/views.py
from flask import Blueprint, render_template, request, flash
from flask.ext.login import login_required, current_user
from ..extensions import db
from ..user import User
from .forms import PasswordForm

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




settings = Blueprint('settings', __name__, url_prefix='/settings')


@settings.route('/password', methods=['GET', 'POST'])
@login_required
def password():
    user = User.query.filter_by(name=current_user.name).first_or_404()

    form = PasswordForm(next=request.args.get('next'))