# Path: mass_flask_core/models/user.py
# class User(db.Document, UserMixin):
#     def is_authenticated(self):
#         return True
# 
#     username = StringField(min_length=3, max_length=50, unique=True, required=True)
#     password_hash = StringField(min_length=5, max_length=200)
#     user_level = UserLevelField(default=UserLevel.USER_LEVEL_USER, required=True)
# 
#     def set_password(self, password):
#         self.password_hash = generate_password_hash(password)
# 
#     def check_password(self, password):
#         return check_password_hash(self.password_hash, password)
# 
#     def get_id(self):
#         return str(self.id)
# 
#     @staticmethod
#     def user_loader(**kwargs):
#         if 'username' in kwargs and 'password' in kwargs:
#             user = User.objects(username=kwargs['username']).first()
#             if user and user.check_password(kwargs['password']):
#                 return user
#             else:
#                 return None
#         elif 'id' in kwargs and kwargs['id']:
#             return User.objects(id=ObjectId(kwargs['id'])).first()
#         else:
#             raise RuntimeError('This access method is not supported by the user loader.')


from flask_wtf import Form
from flask import flash
from wtforms import StringField, SubmitField, PasswordField
from wtforms.validators import DataRequired
from mass_flask_core.models import User



class LoginForm(Form):
    username = StringField(validators=[DataRequired()])
    password = PasswordField(validators=[DataRequired()])
    submit = SubmitField()

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

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

# Path: mass_flask_core/models/user.py
# class User(db.Document, UserMixin):
#     def is_authenticated(self):
#         return True
# 
#     username = StringField(min_length=3, max_length=50, unique=True, required=True)
#     password_hash = StringField(min_length=5, max_length=200)
#     user_level = UserLevelField(default=UserLevel.USER_LEVEL_USER, required=True)
# 
#     def set_password(self, password):
#         self.password_hash = generate_password_hash(password)
# 
#     def check_password(self, password):
#         return check_password_hash(self.password_hash, password)
# 
#     def get_id(self):
#         return str(self.id)
# 
#     @staticmethod
#     def user_loader(**kwargs):
#         if 'username' in kwargs and 'password' in kwargs:
#             user = User.objects(username=kwargs['username']).first()
#             if user and user.check_password(kwargs['password']):
#                 return user
#             else:
#                 return None
#         elif 'id' in kwargs and kwargs['id']:
#             return User.objects(id=ObjectId(kwargs['id'])).first()
#         else:
#             raise RuntimeError('This access method is not supported by the user loader.')


# Path: mass_flask_webui/forms/login.py
from flask_wtf import Form
from flask import flash
from wtforms import StringField, SubmitField, PasswordField
from wtforms.validators import DataRequired
from mass_flask_core.models import User




class LoginForm(Form):
    username = StringField(validators=[DataRequired()])
    password = PasswordField(validators=[DataRequired()])
    submit = SubmitField()

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

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

        user = User.user_loader(username=self.data['username'], password=self.data['password'])