# Path: project/user/services/create_user_service.py
# class CreateUserService:
# 
#     def __init__(self, username, password, generate_api_key=True, *args, **kwargs):
#         self.username = username
#         self.password = password
#         self.generate_api_key = generate_api_key
#         self.args = args
#         self.kwargs = kwargs
# 
#     def call(self):
#         self.kwargs.update(dict(username=self.username, password=self.password))
#         user = User(**self.kwargs)
#         db.session.add(user)
#         db.session.commit()
# 
#         ResetUserPasswordService(user, self.password).call()
#         if self.generate_api_key:
#             GenerateUserApiKeyService(user).call()
# 
#         return user
# 
# Path: project/user/services/login_user_service.py
# class LoginUserService:
# 
#     def __init__(self, user):
#         self.user = user
# 
#     def call(self):
#         return login_user(self.user)
# 
# Path: project/user/services/logout_user_service.py
# class LogoutUserService:
# 
#     @staticmethod
#     def call():
#         logout_user()
#         return None
# 
# Path: project/user/finders/user_finder.py
# class UserFinder:
# 
#     @classmethod
#     def all(cls):
#         return User.query.filter().all()
# 
#     @classmethod
#     def by_id(cls, user_id):
#         return User.query.filter(User.id == user_id).first()
# 
#     @classmethod
#     def by_username(cls, username):
#         return User.query.filter(User.username == username).first()
# 
#     @classmethod
#     def by_api_key(cls, api_key):
#         return User.query.filter(User.api_key == api_key).first()
# 
# Path: project/utils/flash.py
# def flash(message, category='default'):
#     """
#     For the given message, if html is present, add bootstrap .alert-link class to all links
#     """
#     if message.startswith('<p>') and message.endswith('</p>'):
#         message = BeautifulSoup(message, "lxml")
#         for a in message.find_all("a"):
#             if not a.get('class'):
#                 a.attrs.update({'class': []})
#             a['class'].extend(ALERT_LINK_CLASS)
#         message = str(message)
# 
#     FLASH(message, category)


from flask_bcrypt import check_password_hash
from project.user.services.create_user_service import CreateUserService
from project.user.services.login_user_service import LoginUserService
from project.user.services.logout_user_service import LogoutUserService
from project.user.finders.user_finder import UserFinder
from project.utils import flash




class UserHandler:

    @classmethod
    def login(cls, form):
        """
        Login user by giving "LoginForm" form and validate if username+password pair is valid
        """
        if not form.validate_on_submit():
            flash.danger(u'Form is not valid.')
            return False

        user = UserFinder.by_username(form.username.data)
        if not user:

# Path: project/user/services/create_user_service.py
# class CreateUserService:
# 
#     def __init__(self, username, password, generate_api_key=True, *args, **kwargs):
#         self.username = username
#         self.password = password
#         self.generate_api_key = generate_api_key
#         self.args = args
#         self.kwargs = kwargs
# 
#     def call(self):
#         self.kwargs.update(dict(username=self.username, password=self.password))
#         user = User(**self.kwargs)
#         db.session.add(user)
#         db.session.commit()
# 
#         ResetUserPasswordService(user, self.password).call()
#         if self.generate_api_key:
#             GenerateUserApiKeyService(user).call()
# 
#         return user
# 
# Path: project/user/services/login_user_service.py
# class LoginUserService:
# 
#     def __init__(self, user):
#         self.user = user
# 
#     def call(self):
#         return login_user(self.user)
# 
# Path: project/user/services/logout_user_service.py
# class LogoutUserService:
# 
#     @staticmethod
#     def call():
#         logout_user()
#         return None
# 
# Path: project/user/finders/user_finder.py
# class UserFinder:
# 
#     @classmethod
#     def all(cls):
#         return User.query.filter().all()
# 
#     @classmethod
#     def by_id(cls, user_id):
#         return User.query.filter(User.id == user_id).first()
# 
#     @classmethod
#     def by_username(cls, username):
#         return User.query.filter(User.username == username).first()
# 
#     @classmethod
#     def by_api_key(cls, api_key):
#         return User.query.filter(User.api_key == api_key).first()
# 
# Path: project/utils/flash.py
# def flash(message, category='default'):
#     """
#     For the given message, if html is present, add bootstrap .alert-link class to all links
#     """
#     if message.startswith('<p>') and message.endswith('</p>'):
#         message = BeautifulSoup(message, "lxml")
#         for a in message.find_all("a"):
#             if not a.get('class'):
#                 a.attrs.update({'class': []})
#             a['class'].extend(ALERT_LINK_CLASS)
#         message = str(message)
# 
#     FLASH(message, category)


# Path: project/user/handlers/user_handler.py
from flask_bcrypt import check_password_hash
from project.user.services.create_user_service import CreateUserService
from project.user.services.login_user_service import LoginUserService
from project.user.services.logout_user_service import LogoutUserService
from project.user.finders.user_finder import UserFinder
from project.utils import flash





class UserHandler:

    @classmethod
    def login(cls, form):
        """
        Login user by giving "LoginForm" form and validate if username+password pair is valid
        """
        if not form.validate_on_submit():
            flash.danger(u'Form is not valid.')
            return False

        user = UserFinder.by_username(form.username.data)
        if not user:

            flash.danger(u'User {} does not exists.'.format(form.username.data))