# Path: flaskapp/forms.py
# class LoginForm(FlaskForm):
#     """Sign in form
#     """
#     email = StringField(
#         'Email address',
#         validators=[
#             InputRequired('Please enter your email address'),
#             Email()
#             ])
# 
#     password = PasswordField(
#         'Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please enter your password')
#             ])
# 
#     remember_me = BooleanField(
#         'Remember me',
#         widget=CheckboxInput(),
#         default=True)
# 
#     def validate_password(form, field):
#         """Verify password
#         """
#         if not form.email.data:
#             raise StopValidation()
#         
#         # get user and verify password
#         u = User.query.filter(User.email == form.email.data).first()
#         if not u or not verify_password_hash(field.data, u.password):
#             raise ValidationError('Email and password must match')
# 
# class CreateAccountForm(FlaskForm):
#     """Create account form
#     """
#     email = StringField(
#         'Email address',
#         validators=[
#             InputRequired('Please enter an email address'),
#             Email()
#             ])
# 
#     password = PasswordField(
#         'Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please choose a password'),
#             EqualTo('password_confirm', message='Passwords must match')
#             ])
#     
#     password_confirm = PasswordField(
#         'Confirm Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please confirm your password')
#             ])
# 
#     newsletter = BooleanField(
#         'Please send me product updates',
#         widget=CheckboxInput(),
#         default=True
#         )
# 
#     def validate_email(form, field):
#         """Check if email already exists
#         """
#         u = User.query.filter(User.email == field.data).first()
#         if u != None:
#             raise ValidationError('Did your forget your password?')
# 
# class ForgotPasswordForm(FlaskForm):
#     email = StringField(
#         'Email address',
#         validators=[
#             InputRequired('Please enter an email address'),
#             Email()
#             ])
# 
#     def validate_email(form, field):
#         """Check if email exists
#         """
#         u = User.query.filter(User.email == field.data).first()
#         if u == None:
#             raise ValidationError('Sorry, %s is not registered' % field.data)
# 
# class ResetPasswordForm(FlaskForm):
#     password = PasswordField(
#         'New Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please choose a password'),
#             EqualTo('password_confirm', message='Passwords must match')
#             ])
# 
#     password_confirm = PasswordField(
#         'Confirm Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please confirm your password')
#             ])
# 
# Path: flaskapp/models.py
# class User(db.Model):
#     """User object
#     """
#     id = db.Column(db.Integer, primary_key=True)
#     email = db.Column(db.String(120), index=True, unique=True, nullable=False)
#     password = db.Column(db.String(60), index=True, nullable=False)
#     is_verified = db.Column(db.Boolean(), default=False, nullable=False)
#     password_reset_requests = db.relationship(
#         'PasswordResetRequest',
#         backref=db.backref('user'))
#     email_verification_requests = db.relationship(
#         'EmailVerificationRequest',
#         backref=db.backref('user'))
# 
#     def __repr__(self):
#         return '<User %r>' % self.email
# 
#     # ===========================
#     # Flask-Login methods
#     # ===========================
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return True
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return self.id
# 
# class EmailVerificationRequest(db.Model):
#     """EmailVerificationRequest object
#     """
#     key = db.Column(db.String(64), primary_key=True)
#     fk_user = db.Column(db.Integer, db.ForeignKey('user.id'))
#     create_ts = db.Column(db.DateTime, default=datetime.datetime.utcnow, \
#                               nullable=False)
# 
# class PasswordResetRequest(db.Model):
#     """PasswordResetRequest object
#     """
#     key = db.Column(db.String(64), primary_key=True)
#     fk_user = db.Column(db.Integer, db.ForeignKey('user.id'))
#     create_ts = db.Column(db.DateTime, default=datetime.datetime.utcnow, \
#                               nullable=False)
# 
# Path: flaskapp/lib/util.py
# def generate_password_hash(password):
#     """Method to centralize the hashing of passwords
#     """
#     password = password.encode('utf-8')
#     return bcrypt.hashpw(password, bcrypt.gensalt(12))


import os
import datetime
from flask import (Blueprint, render_template, url_for, redirect, request,
                   current_app, g)
from flask_login import login_user, logout_user, login_required
from flask_principal import identity_changed, Identity, AnonymousIdentity
from flask_mail import Message
from flask_wtf import FlaskForm
from premailer import transform
from flaskapp.meta import db, mail
from flaskapp.forms import (LoginForm, CreateAccountForm, ForgotPasswordForm,
                            ResetPasswordForm)
from flaskapp.models import (User, EmailVerificationRequest, 
                             PasswordResetRequest)
from flaskapp.lib.util import generate_password_hash




bp = Blueprint('auth', __name__)


@bp.route('/login', methods=['GET', 'POST'])
def login():
    """GET|POST /login: login form handler
    """

# Path: flaskapp/forms.py
# class LoginForm(FlaskForm):
#     """Sign in form
#     """
#     email = StringField(
#         'Email address',
#         validators=[
#             InputRequired('Please enter your email address'),
#             Email()
#             ])
# 
#     password = PasswordField(
#         'Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please enter your password')
#             ])
# 
#     remember_me = BooleanField(
#         'Remember me',
#         widget=CheckboxInput(),
#         default=True)
# 
#     def validate_password(form, field):
#         """Verify password
#         """
#         if not form.email.data:
#             raise StopValidation()
#         
#         # get user and verify password
#         u = User.query.filter(User.email == form.email.data).first()
#         if not u or not verify_password_hash(field.data, u.password):
#             raise ValidationError('Email and password must match')
# 
# class CreateAccountForm(FlaskForm):
#     """Create account form
#     """
#     email = StringField(
#         'Email address',
#         validators=[
#             InputRequired('Please enter an email address'),
#             Email()
#             ])
# 
#     password = PasswordField(
#         'Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please choose a password'),
#             EqualTo('password_confirm', message='Passwords must match')
#             ])
#     
#     password_confirm = PasswordField(
#         'Confirm Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please confirm your password')
#             ])
# 
#     newsletter = BooleanField(
#         'Please send me product updates',
#         widget=CheckboxInput(),
#         default=True
#         )
# 
#     def validate_email(form, field):
#         """Check if email already exists
#         """
#         u = User.query.filter(User.email == field.data).first()
#         if u != None:
#             raise ValidationError('Did your forget your password?')
# 
# class ForgotPasswordForm(FlaskForm):
#     email = StringField(
#         'Email address',
#         validators=[
#             InputRequired('Please enter an email address'),
#             Email()
#             ])
# 
#     def validate_email(form, field):
#         """Check if email exists
#         """
#         u = User.query.filter(User.email == field.data).first()
#         if u == None:
#             raise ValidationError('Sorry, %s is not registered' % field.data)
# 
# class ResetPasswordForm(FlaskForm):
#     password = PasswordField(
#         'New Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please choose a password'),
#             EqualTo('password_confirm', message='Passwords must match')
#             ])
# 
#     password_confirm = PasswordField(
#         'Confirm Password',
#         widget=PasswordInput(hide_value=False),
#         validators=[
#             InputRequired('Please confirm your password')
#             ])
# 
# Path: flaskapp/models.py
# class User(db.Model):
#     """User object
#     """
#     id = db.Column(db.Integer, primary_key=True)
#     email = db.Column(db.String(120), index=True, unique=True, nullable=False)
#     password = db.Column(db.String(60), index=True, nullable=False)
#     is_verified = db.Column(db.Boolean(), default=False, nullable=False)
#     password_reset_requests = db.relationship(
#         'PasswordResetRequest',
#         backref=db.backref('user'))
#     email_verification_requests = db.relationship(
#         'EmailVerificationRequest',
#         backref=db.backref('user'))
# 
#     def __repr__(self):
#         return '<User %r>' % self.email
# 
#     # ===========================
#     # Flask-Login methods
#     # ===========================
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return True
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return self.id
# 
# class EmailVerificationRequest(db.Model):
#     """EmailVerificationRequest object
#     """
#     key = db.Column(db.String(64), primary_key=True)
#     fk_user = db.Column(db.Integer, db.ForeignKey('user.id'))
#     create_ts = db.Column(db.DateTime, default=datetime.datetime.utcnow, \
#                               nullable=False)
# 
# class PasswordResetRequest(db.Model):
#     """PasswordResetRequest object
#     """
#     key = db.Column(db.String(64), primary_key=True)
#     fk_user = db.Column(db.Integer, db.ForeignKey('user.id'))
#     create_ts = db.Column(db.DateTime, default=datetime.datetime.utcnow, \
#                               nullable=False)
# 
# Path: flaskapp/lib/util.py
# def generate_password_hash(password):
#     """Method to centralize the hashing of passwords
#     """
#     password = password.encode('utf-8')
#     return bcrypt.hashpw(password, bcrypt.gensalt(12))


# Path: flaskapp/views/auth.py
import os
import datetime
from flask import (Blueprint, render_template, url_for, redirect, request,
                   current_app, g)
from flask_login import login_user, logout_user, login_required
from flask_principal import identity_changed, Identity, AnonymousIdentity
from flask_mail import Message
from flask_wtf import FlaskForm
from premailer import transform
from flaskapp.meta import db, mail
from flaskapp.forms import (LoginForm, CreateAccountForm, ForgotPasswordForm,
                            ResetPasswordForm)
from flaskapp.models import (User, EmailVerificationRequest, 
                             PasswordResetRequest)
from flaskapp.lib.util import generate_password_hash





bp = Blueprint('auth', __name__)


@bp.route('/login', methods=['GET', 'POST'])
def login():
    """GET|POST /login: login form handler
    """

    form = LoginForm()