# Path: src/config.py
# APP_MIN_PASSWD_LENGTH = 8
# 
# Path: src/models.py
# class Users(MultiTenantBase, UserMixin, db.Model):
#     name = Column(String(127), nullable=True)               # optional
#     active = Column(Boolean, nullable=False, default=True)  # req security
#     admin = Column(Boolean, nullable=False, default=False)
#     email = Column(String(64), index=True, unique=True, nullable=False)
#     password = Column(String(72), nullable=False)  # bcrypt.length = 60
#     timezone = Column(TimezoneType(backend='pytz'))
# 
#     confirmed_at = Column(DateTime)
#     last_login_at = Column(DateTime)
#     current_login_at = Column(DateTime)
#     login_count = Column(Integer, default=0)
#     last_login_ip = Column(IPAddressType)
#     current_login_ip = Column(IPAddressType)
# 
#     roles = db.relationship('Roles', secondary=roles_users,
#                             backref='users', lazy='dynamic')
# 
#     def __init__(self, name='', email='', desc='', password=None,
#                  active=True, admin=False, org=None, roles=None, timezone=None):
#         self.active = active
#         self.admin = admin
#         self.desc = desc
#         self.email = email
#         self.name = name
#         self.timezone = timezone
# 
#         if password:
#             self.set_password(password)
#         if roles is not None:
#             if type(roles) is not list:
#                 roles = [roles]
#             self.roles = roles
# 
#         if type(org) is str:
#             orgobj = Orgs.query.filter_by(name=org).first()
#             if orgobj:
#                 org = orgobj
#             else:
#                 org = Orgs(name=org)
#                 db.session.add(org)
#                 db.session.commit()
#         self.org = org
# 
#     def set_password(self, password):
#         try:
#             self.password = encrypt_password(password)
#         except RuntimeError:  # :-/
#             with app.app_context():
#                 self.password = encrypt_password(password)
# 
#     def check_password(self, password):
#         return verify_password(password, self.password)
# 
#     def __repr__(self):
#         return '<%s %r>' % (self.__class__.__name__, self.email)


from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField, SubmitField  #, BooleanField
from wtforms.fields.html5 import EmailField
from wtforms.validators import InputRequired, Email, Length
from wtforms_alchemy import model_form_factory
from flask_security.forms import (LoginForm, ConfirmRegisterForm,
                                  SendConfirmationForm, ForgotPasswordForm)
from .config import APP_MIN_PASSWD_LENGTH as MIN_LEN
from .models import Users
ModelForm = model_form_factory(FlaskForm)


# override and extend the secuity views
class ExLoginForm(LoginForm):
    email = EmailField('foo@bar.com', [InputRequired(), Email()])
    password = PasswordField('Password', [InputRequired(), Length(MIN_LEN)])


class ExRegForm(ConfirmRegisterForm):
    org = StringField('Name of Organization', [InputRequired(), Length(6)])
    email = EmailField('foo@bar.com', [InputRequired(), Email()])
    password = PasswordField('Password', [InputRequired(), Length(MIN_LEN)])
    #~ password_confirm = # skipped due to confirmation mail.


class ExSendConfForm(SendConfirmationForm):
    submit = SubmitField('Resend Confirmation')


class ExForgotPasswordForm(ForgotPasswordForm):
    submit = SubmitField('Reset Password')


# automated forms
common_exclusions = tuple('password created_at updated_at'.split())


class UserForm(ModelForm):
    class Meta:

# Path: src/config.py
# APP_MIN_PASSWD_LENGTH = 8
# 
# Path: src/models.py
# class Users(MultiTenantBase, UserMixin, db.Model):
#     name = Column(String(127), nullable=True)               # optional
#     active = Column(Boolean, nullable=False, default=True)  # req security
#     admin = Column(Boolean, nullable=False, default=False)
#     email = Column(String(64), index=True, unique=True, nullable=False)
#     password = Column(String(72), nullable=False)  # bcrypt.length = 60
#     timezone = Column(TimezoneType(backend='pytz'))
# 
#     confirmed_at = Column(DateTime)
#     last_login_at = Column(DateTime)
#     current_login_at = Column(DateTime)
#     login_count = Column(Integer, default=0)
#     last_login_ip = Column(IPAddressType)
#     current_login_ip = Column(IPAddressType)
# 
#     roles = db.relationship('Roles', secondary=roles_users,
#                             backref='users', lazy='dynamic')
# 
#     def __init__(self, name='', email='', desc='', password=None,
#                  active=True, admin=False, org=None, roles=None, timezone=None):
#         self.active = active
#         self.admin = admin
#         self.desc = desc
#         self.email = email
#         self.name = name
#         self.timezone = timezone
# 
#         if password:
#             self.set_password(password)
#         if roles is not None:
#             if type(roles) is not list:
#                 roles = [roles]
#             self.roles = roles
# 
#         if type(org) is str:
#             orgobj = Orgs.query.filter_by(name=org).first()
#             if orgobj:
#                 org = orgobj
#             else:
#                 org = Orgs(name=org)
#                 db.session.add(org)
#                 db.session.commit()
#         self.org = org
# 
#     def set_password(self, password):
#         try:
#             self.password = encrypt_password(password)
#         except RuntimeError:  # :-/
#             with app.app_context():
#                 self.password = encrypt_password(password)
# 
#     def check_password(self, password):
#         return verify_password(password, self.password)
# 
#     def __repr__(self):
#         return '<%s %r>' % (self.__class__.__name__, self.email)


# Path: src/forms.py
from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField, SubmitField  #, BooleanField
from wtforms.fields.html5 import EmailField
from wtforms.validators import InputRequired, Email, Length
from wtforms_alchemy import model_form_factory
from flask_security.forms import (LoginForm, ConfirmRegisterForm,
                                  SendConfirmationForm, ForgotPasswordForm)
from .config import APP_MIN_PASSWD_LENGTH as MIN_LEN
from .models import Users

ModelForm = model_form_factory(FlaskForm)


# override and extend the secuity views
class ExLoginForm(LoginForm):
    email = EmailField('foo@bar.com', [InputRequired(), Email()])
    password = PasswordField('Password', [InputRequired(), Length(MIN_LEN)])


class ExRegForm(ConfirmRegisterForm):
    org = StringField('Name of Organization', [InputRequired(), Length(6)])
    email = EmailField('foo@bar.com', [InputRequired(), Email()])
    password = PasswordField('Password', [InputRequired(), Length(MIN_LEN)])
    #~ password_confirm = # skipped due to confirmation mail.


class ExSendConfForm(SendConfirmationForm):
    submit = SubmitField('Resend Confirmation')


class ExForgotPasswordForm(ForgotPasswordForm):
    submit = SubmitField('Reset Password')


# automated forms
common_exclusions = tuple('password created_at updated_at'.split())


class UserForm(ModelForm):
    class Meta:

        model = Users