# 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
# 
# class Role(BaseModel):
#     __tablename__ = 'roles'
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(64), unique=True)
#     default = db.Column(db.Boolean, default=False, index=True)
#     permissions = db.Column(db.Integer)
#     users = db.relationship('User', backref='role', lazy='dynamic')
# 
#     def __repr__(self):
#         return '<Role %s>' % self.name
# 
#     def __init__(self, **kwargs):
#         super(Role, self).__init__(**kwargs)
#         if self.name and self.name in ["admin", "administrator"]:
#             self.permissions = Permission.ADMIN
#         else:
#             self.permissions = Permission.WRITE_POSTS
# 
#     @staticmethod
#     def insert_roles():
#         # L.i('Inserting default roles.')
#         roles = {
#             'User': Permission.user(),
#             'Admin': Permission.admin()
#         }
#         for r in roles:
#             nm = r.lower()
#             role = Role.query.filter_by(name=nm).first()
#             if role is None:
#                 role = Role(name=nm)
#             role.permissions = roles[r][0]
#             role.default = roles[r][1]
#             db.session.add(role)
#         db.session.commit()
#         # L.i('Default roles %s added.', ', '.join(roles.keys()))


from flask_wtf import Form
from wtforms import StringField, PasswordField, BooleanField, \
    SubmitField, SelectField, ValidationError
from wtforms.validators import DataRequired, Length, Email, EqualTo
from .models import User, Role


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(

# 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
# 
# class Role(BaseModel):
#     __tablename__ = 'roles'
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(64), unique=True)
#     default = db.Column(db.Boolean, default=False, index=True)
#     permissions = db.Column(db.Integer)
#     users = db.relationship('User', backref='role', lazy='dynamic')
# 
#     def __repr__(self):
#         return '<Role %s>' % self.name
# 
#     def __init__(self, **kwargs):
#         super(Role, self).__init__(**kwargs)
#         if self.name and self.name in ["admin", "administrator"]:
#             self.permissions = Permission.ADMIN
#         else:
#             self.permissions = Permission.WRITE_POSTS
# 
#     @staticmethod
#     def insert_roles():
#         # L.i('Inserting default roles.')
#         roles = {
#             'User': Permission.user(),
#             'Admin': Permission.admin()
#         }
#         for r in roles:
#             nm = r.lower()
#             role = Role.query.filter_by(name=nm).first()
#             if role is None:
#                 role = Role(name=nm)
#             role.permissions = roles[r][0]
#             role.default = roles[r][1]
#             db.session.add(role)
#         db.session.commit()
#         # L.i('Default roles %s added.', ', '.join(roles.keys()))


# Path: yapper/blueprints/user/forms.py
from flask_wtf import Form
from wtforms import StringField, PasswordField, BooleanField, \
    SubmitField, SelectField, ValidationError
from wtforms.validators import DataRequired, Length, Email, EqualTo
from .models import User, Role



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()]