# Path: project/home/decorators.py
# def roles_required(*role_names):
#     """
#     This decorator ensures that the current user is logged in, and has *all* of the specified roles (AND operation).
#     Example::
#         @route('/escape')
#         @roles_required('Special', 'Agent')
#         def escape_capture():  # User must be 'Special' AND 'Agent'
#             ...
#     Calls unauthenticated_view() when the user is not logged in or when user has not confirmed their email address.
#     Calls unauthorized_view() when the user does not have the required roles.
#     Calls the decorated view otherwise.
#     """
#     def wrapper(view_function):
# 
#         @wraps(view_function)
#         def decorator(*args, **kwargs):
#             # User must have the required roles
#             if not user.has_roles(*role_names):
#                 # Redirect to the unauthorized page
#                 return app.login_manager.unauthorized()
# 
#             # It's OK to call the view
#             return view_function(*args, **kwargs)
#         return decorator
#     return wrapper


from werkzeug.exceptions import HTTPException
from flask import Response, redirect
from flask_admin import BaseView, expose
from flask_admin.contrib.sqla import ModelView as DefaultModelView
from flask_login import login_required
from project.home.decorators import roles_required


class BasicAuthException(HTTPException):
    def __init__(self, message):
        super().__init__(message, Response(
            "You could not be authenticated. Please refresh the page.", 401,
            {'WWW-Authenticate': 'Basic realm="Login Required"'}
        ))


class ModelView(DefaultModelView):

    column_auto_select_related = True


# Path: project/home/decorators.py
# def roles_required(*role_names):
#     """
#     This decorator ensures that the current user is logged in, and has *all* of the specified roles (AND operation).
#     Example::
#         @route('/escape')
#         @roles_required('Special', 'Agent')
#         def escape_capture():  # User must be 'Special' AND 'Agent'
#             ...
#     Calls unauthenticated_view() when the user is not logged in or when user has not confirmed their email address.
#     Calls unauthorized_view() when the user does not have the required roles.
#     Calls the decorated view otherwise.
#     """
#     def wrapper(view_function):
# 
#         @wraps(view_function)
#         def decorator(*args, **kwargs):
#             # User must have the required roles
#             if not user.has_roles(*role_names):
#                 # Redirect to the unauthorized page
#                 return app.login_manager.unauthorized()
# 
#             # It's OK to call the view
#             return view_function(*args, **kwargs)
#         return decorator
#     return wrapper


# Path: project/admin/views.py
from werkzeug.exceptions import HTTPException
from flask import Response, redirect
from flask_admin import BaseView, expose
from flask_admin.contrib.sqla import ModelView as DefaultModelView
from flask_login import login_required
from project.home.decorators import roles_required



class BasicAuthException(HTTPException):
    def __init__(self, message):
        super().__init__(message, Response(
            "You could not be authenticated. Please refresh the page.", 401,
            {'WWW-Authenticate': 'Basic realm="Login Required"'}
        ))


class ModelView(DefaultModelView):

    column_auto_select_related = True


    def __init__(self, model, session, basic_auth, *args, **kwargs):