# Path: yapper/blueprints/user/decorators.py
# def admin_required(f):
#     """Like `permission required`, but specific to admin."""
#     return permission_required(Permission.ADMIN)(f)
# 
# Path: yapper/blueprints/user/forms.py
# class LoginForm(Form):
#     email = StringField(u'Email', validators=[DataRequired(),
#                         Length(1, 64), Email()])
#     password = PasswordField(u'Password', validators=[DataRequired()])
#     remember_me = BooleanField(u'Remember Me')
#     submit = SubmitField(u'Log In')
# 
# class RegisterForm(Form):
#     name = StringField(u'Your Name', validators=[DataRequired()])
#     email = StringField(u'Email', validators=[DataRequired(),
#                         Length(1, 64), Email()])
# 
#     password = PasswordField(u'Password', validators=[DataRequired(),
#                              EqualTo('cpassword',
#                              message=u'Both passwords should be same')])
#     cpassword = PasswordField(u'Confirm Password', validators=[DataRequired()])
#     role = SelectField(u'Role', coerce=int)
#     confirm = BooleanField(u'Register With Confirmation?')
#     submit = SubmitField(u'Sign Up')
# 
#     def __init__(self, **kwargs):
#         super(RegisterForm, self).__init__(**kwargs)
#         self.role.choices = [(r.id, r.name)
#                              for r in Role.query.with_entities(
#                              Role.id, Role.name).all()]
# 
#     def validate_email(self, field):
#         if User.query.filter_by(email=field.data).first():
#             raise ValidationError('Email already registered')
# 
# Path: yapper/blueprints/user/models.py
# class User(UserMixin, BaseModel):
#     __tablename__ = 'users'
#     name = db.Column(db.String(64))
#     email = db.Column(db.String(64), unique=True, index=True)
#     password_hash = db.Column(db.String(128))
#     role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
#     status = db.Column(db.Boolean, default=False)
#     posts = db.relationship('Post', backref='author', lazy='dynamic')
# 
#     def to_json(self):
#         json_data = {
#             'id': self.id,
#             'name': self.name
#         }
#         return json_data
# 
#     def __init__(self, **kwargs):
#         super(User, self).__init__(**kwargs)
#         if self.role is None:
#             if self.email == current_app.config['FLASKY_ADMIN']:
#                 self.role = Role.query.filter_by(
#                     name="admin"
#                 ).first()
#                 self.status = True
#             if self.role is None:
#                 self.role = Role.query.filter_by(default=True).first()
# 
#     @property
#     def password(self):
#         raise AttributeError(u'Password is not a readable attribute.')
# 
#     @password.setter
#     def password(self, password):
#         self.password_hash = generate_password_hash(password)
# 
#     def get_id(self):
#         return self.id
# 
#     def verify_password(self, password):
#         return check_password_hash(self.password_hash, password)
# 
#     def __repr__(self):
#         return '<User %s>' % self.email
# 
#     def can(self, permissions):
#         return self.role is not None and \
#             (self.role.permissions & permissions) == permissions
# 
#     def is_administrator(self):
#         return self.is_admin()
# 
#     def is_admin(self):
#         return self.can(Permission.ADMIN)
# 
#     def is_confirmed(self):
#         return self.status is not None and self.status is True


from flask import (
    Blueprint,
    render_template,
    flash,
    redirect,
    request,
    url_for
)
from flask_login import (
    login_required,
    login_user,
    logout_user,
    current_user
)
from .decorators import admin_required
from .forms import LoginForm, RegisterForm
from .models import User
def login():
    if current_user is not None and current_user.is_authenticated() \
            and current_user.is_confirmed():
        return redirect(url_for('main.index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user is not None and user.verify_password(form.password.data) \
                and user.is_confirmed():
            login_user(user, form.remember_me.data)
            return redirect(request.args.get('next', '') or
                            url_for('main.index'))
        flash(u'Invalid combination', 'warning')
    return render_template('user/login.html', form=form)


@user.route('/logout')
@login_required
def logout():
    logout_user()
    flash(u'You have been logged out.', 'info')
    return redirect(url_for('main.index'))


@user.route('/signup', methods=['GET', 'POST'])
@login_required
@admin_required
def signup():
    form = RegisterForm()
    if form.validate_on_submit():

# Path: yapper/blueprints/user/decorators.py
# def admin_required(f):
#     """Like `permission required`, but specific to admin."""
#     return permission_required(Permission.ADMIN)(f)
# 
# Path: yapper/blueprints/user/forms.py
# class LoginForm(Form):
#     email = StringField(u'Email', validators=[DataRequired(),
#                         Length(1, 64), Email()])
#     password = PasswordField(u'Password', validators=[DataRequired()])
#     remember_me = BooleanField(u'Remember Me')
#     submit = SubmitField(u'Log In')
# 
# class RegisterForm(Form):
#     name = StringField(u'Your Name', validators=[DataRequired()])
#     email = StringField(u'Email', validators=[DataRequired(),
#                         Length(1, 64), Email()])
# 
#     password = PasswordField(u'Password', validators=[DataRequired(),
#                              EqualTo('cpassword',
#                              message=u'Both passwords should be same')])
#     cpassword = PasswordField(u'Confirm Password', validators=[DataRequired()])
#     role = SelectField(u'Role', coerce=int)
#     confirm = BooleanField(u'Register With Confirmation?')
#     submit = SubmitField(u'Sign Up')
# 
#     def __init__(self, **kwargs):
#         super(RegisterForm, self).__init__(**kwargs)
#         self.role.choices = [(r.id, r.name)
#                              for r in Role.query.with_entities(
#                              Role.id, Role.name).all()]
# 
#     def validate_email(self, field):
#         if User.query.filter_by(email=field.data).first():
#             raise ValidationError('Email already registered')
# 
# Path: yapper/blueprints/user/models.py
# class User(UserMixin, BaseModel):
#     __tablename__ = 'users'
#     name = db.Column(db.String(64))
#     email = db.Column(db.String(64), unique=True, index=True)
#     password_hash = db.Column(db.String(128))
#     role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
#     status = db.Column(db.Boolean, default=False)
#     posts = db.relationship('Post', backref='author', lazy='dynamic')
# 
#     def to_json(self):
#         json_data = {
#             'id': self.id,
#             'name': self.name
#         }
#         return json_data
# 
#     def __init__(self, **kwargs):
#         super(User, self).__init__(**kwargs)
#         if self.role is None:
#             if self.email == current_app.config['FLASKY_ADMIN']:
#                 self.role = Role.query.filter_by(
#                     name="admin"
#                 ).first()
#                 self.status = True
#             if self.role is None:
#                 self.role = Role.query.filter_by(default=True).first()
# 
#     @property
#     def password(self):
#         raise AttributeError(u'Password is not a readable attribute.')
# 
#     @password.setter
#     def password(self, password):
#         self.password_hash = generate_password_hash(password)
# 
#     def get_id(self):
#         return self.id
# 
#     def verify_password(self, password):
#         return check_password_hash(self.password_hash, password)
# 
#     def __repr__(self):
#         return '<User %s>' % self.email
# 
#     def can(self, permissions):
#         return self.role is not None and \
#             (self.role.permissions & permissions) == permissions
# 
#     def is_administrator(self):
#         return self.is_admin()
# 
#     def is_admin(self):
#         return self.can(Permission.ADMIN)
# 
#     def is_confirmed(self):
#         return self.status is not None and self.status is True


# Path: yapper/blueprints/user/controllers.py
from flask import (
    Blueprint,
    render_template,
    flash,
    redirect,
    request,
    url_for
)
from flask_login import (
    login_required,
    login_user,
    logout_user,
    current_user
)
from .decorators import admin_required
from .forms import LoginForm, RegisterForm
from .models import User

def login():
    if current_user is not None and current_user.is_authenticated() \
            and current_user.is_confirmed():
        return redirect(url_for('main.index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user is not None and user.verify_password(form.password.data) \
                and user.is_confirmed():
            login_user(user, form.remember_me.data)
            return redirect(request.args.get('next', '') or
                            url_for('main.index'))
        flash(u'Invalid combination', 'warning')
    return render_template('user/login.html', form=form)


@user.route('/logout')
@login_required
def logout():
    logout_user()
    flash(u'You have been logged out.', 'info')
    return redirect(url_for('main.index'))


@user.route('/signup', methods=['GET', 'POST'])
@login_required
@admin_required
def signup():
    form = RegisterForm()
    if form.validate_on_submit():

        user = User(