# Path: flask_editablesite/user/models.py
# class User(UserMixin, SurrogatePK, TimeStamped, Confirmable, Model):
#     __tablename__ = 'users'
# 
#     email = Column(db.String(255), unique=True, nullable=False)
#     password = Column(db.String(255), nullable=True)
#     first_name = Column(db.String(255), nullable=True)
#     last_name = Column(db.String(255), nullable=True)
# 
#     def __init__(self, email, password=None, **kwargs):
#         db.Model.__init__(self, email=email, **kwargs)
#         if password:
#             self.set_password(password)
#         else:
#             self.password = None
# 
#     def set_password(self, password):
#         self.password = bcrypt.generate_password_hash(password)
# 
#     def check_password(self, value):
#         return bcrypt.check_password_hash(self.password, value)
# 
#     @property
#     def full_name(self):
#         if self.first_name and self.last_name:
#             return "{0} {1}".format(self.first_name, self.last_name)
# 
#         return ""
# 
#     @property
#     def full_name_or_email(self):
#         return self.full_name or self.email
# 
#     def __repr__(self):
#         return '{0} <{1}>'.format(self.full_name, self.email)
# 
#     @classmethod
#     def sessionstore_user(cls):
#         """
#         A dummy user object to represent the logged-in user when
#         we're set to 'sessionstore' instead of 'db'.
#         """
# 
#         return cls(
#             email=app.config['SESSIONSTORE_USER_EMAIL'],
#             first_name=app.config['SESSIONSTORE_USER_FIRST_NAME'],
#             last_name=app.config['SESSIONSTORE_USER_LAST_NAME'],
#             active=True)


from flask import current_app as app
from flask_wtf import Form
from wtforms import TextField, TextAreaField, PasswordField
from wtforms.validators import DataRequired, Email
from flask_editablesite.user.models import User
    message = TextAreaField('Message', validators=[DataRequired()])


class LoginForm(Form):
    email = TextField('Email', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])

    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.user = None

    def validate(self):
        initial_validation = super(LoginForm, self).validate()
        if not initial_validation:
            return False

        # Log in with dummy credentials when we're set to 'sessionstore'
        # instead of 'db'.
        if app.config.get('USE_SESSIONSTORE_NOT_DB'):
            if self.email.data != app.config['SESSIONSTORE_USER_EMAIL']:
                self.email.errors.append('Unknown email')
                return False

            if self.password.data != app.config['SESSIONSTORE_USER_PASSWORD']:
                self.password.errors.append('Invalid password')
                return False

            self.user = User.sessionstore_user()
        # Regular login.
        else:

# Path: flask_editablesite/user/models.py
# class User(UserMixin, SurrogatePK, TimeStamped, Confirmable, Model):
#     __tablename__ = 'users'
# 
#     email = Column(db.String(255), unique=True, nullable=False)
#     password = Column(db.String(255), nullable=True)
#     first_name = Column(db.String(255), nullable=True)
#     last_name = Column(db.String(255), nullable=True)
# 
#     def __init__(self, email, password=None, **kwargs):
#         db.Model.__init__(self, email=email, **kwargs)
#         if password:
#             self.set_password(password)
#         else:
#             self.password = None
# 
#     def set_password(self, password):
#         self.password = bcrypt.generate_password_hash(password)
# 
#     def check_password(self, value):
#         return bcrypt.check_password_hash(self.password, value)
# 
#     @property
#     def full_name(self):
#         if self.first_name and self.last_name:
#             return "{0} {1}".format(self.first_name, self.last_name)
# 
#         return ""
# 
#     @property
#     def full_name_or_email(self):
#         return self.full_name or self.email
# 
#     def __repr__(self):
#         return '{0} <{1}>'.format(self.full_name, self.email)
# 
#     @classmethod
#     def sessionstore_user(cls):
#         """
#         A dummy user object to represent the logged-in user when
#         we're set to 'sessionstore' instead of 'db'.
#         """
# 
#         return cls(
#             email=app.config['SESSIONSTORE_USER_EMAIL'],
#             first_name=app.config['SESSIONSTORE_USER_FIRST_NAME'],
#             last_name=app.config['SESSIONSTORE_USER_LAST_NAME'],
#             active=True)


# Path: flask_editablesite/public/forms.py
from flask import current_app as app
from flask_wtf import Form
from wtforms import TextField, TextAreaField, PasswordField
from wtforms.validators import DataRequired, Email
from flask_editablesite.user.models import User

    message = TextAreaField('Message', validators=[DataRequired()])


class LoginForm(Form):
    email = TextField('Email', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])

    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.user = None

    def validate(self):
        initial_validation = super(LoginForm, self).validate()
        if not initial_validation:
            return False

        # Log in with dummy credentials when we're set to 'sessionstore'
        # instead of 'db'.
        if app.config.get('USE_SESSIONSTORE_NOT_DB'):
            if self.email.data != app.config['SESSIONSTORE_USER_EMAIL']:
                self.email.errors.append('Unknown email')
                return False

            if self.password.data != app.config['SESSIONSTORE_USER_PASSWORD']:
                self.password.errors.append('Invalid password')
                return False

            self.user = User.sessionstore_user()
        # Regular login.
        else:

            self.user = User.query.filter_by(email=self.email.data).first()