:param UserStruct: The User model structure containing other classes
    :param login_view: The login view interface
    :param template_dir: The directory containing the view pages
    :return: UserAdmin

    Doc:
    User Admin is a view that allows you to admin users.
    You must create a Pylot view called `UserAdmin` to activate it

    UserAdmin = app.views.user_admin(User, Login)
    class UserAdmin(UserAdmin, Pylot):
        pass

    The user admin create some global available vars under '__.user_admin'

    It's also best to add some security access on it
    class UserAdmin(UserAdmin, Pylot):
        decorators = [login_required]

    You can customize the user info page (::get) by creating the directory in your
    templates dir, and include the get.html inside of it

    ie:
    >/admin/templates/UserAdmin/get.html

    <div>
        {% include "Pylot/UserAdmin/get.html" %}
    <div>

    <div>Hello {{ __.user_admin.user.name }}<div>

def user_admin_view(model, login_view="Login", template_dir=None):
    """
    :param UserStruct: The User model structure containing other classes
    :param login_view: The login view interface
    :param template_dir: The directory containing the view pages
    :return: UserAdmin

    Doc:
    User Admin is a view that allows you to admin users.
    You must create a Pylot view called `UserAdmin` to activate it

    UserAdmin = app.views.user_admin(User, Login)
    class UserAdmin(UserAdmin, Pylot):
        pass

    The user admin create some global available vars under '__.user_admin'

    It's also best to add some security access on it
    class UserAdmin(UserAdmin, Pylot):
        decorators = [login_required]

    You can customize the user info page (::get) by creating the directory in your
    templates dir, and include the get.html inside of it

    ie:
    >/admin/templates/UserAdmin/get.html

    <div>
        {% include "Pylot/UserAdmin/get.html" %}
    <div>

    <div>Hello {{ __.user_admin.user.name }}<div>

    """

    Pylot.context_(COMPONENT_USER_ADMIN=True)

    User = model.UserStruct.User
    LoginView = login_view

    if not template_dir:
        template_dir = "Pylot/UserAdmin"
    template_page = template_dir + "/%s.html"

    class UserAdmin(object):
        route_base = "user-admin"

        @classmethod
        def _options(cls):
            return {
                "user_role": [("Rol", "Role")], #[(role, role) for i, role in enumerate(.all_roles)],
                "user_status": [("Sta", "Stat")] #[(status, status) for i, status in enumerate(User.all_status)]
            }

        @classmethod
        def search_handler(cls, per_page=20):
            """
            To initiate a search
            """
            page = request.args.get("page", 1)
            show_deleted = True if request.args.get("show-deleted") else False
            name = request.args.get("name")
            email = request.args.get("email")

            users = User.all(include_deleted=show_deleted)
            users = users.order_by(User.name.asc())
            if name:
                users = users.filter(User.name.contains(name))
            if email:
                users = users.filter(User.email.contains(email))

            users = users.paginate(page=page, per_page=per_page)

            cls.__(user_admin=dict(
                options=cls._options(),
                users=users,
                search_query={
                       "excluded_deleted": request.args.get("show-deleted"),
                       "role": request.args.get("role"),
                       "status": request.args.get("status"),
                       "name": request.args.get("name"),
                       "email": request.args.get("email")
                    }
                ))
            return users

        @classmethod
        def get_user_handler(cls, id):
            """
            Get a user
            """
            user = User.get(id, include_deleted=True)
            if not user:
                abort(404, "User doesn't exist")
            cls.__(user_admin=dict(user=user, options=cls._options()))
            return user

        def index(self):
            self.search_handler()
            return self.render(view_template=template_page % "index")

        def get(self, id):
            self.get_user_handler(id)
            return self.render(view_template=template_page % "get")

        def post(self):
            try:
                id = request.form.get("id")
                user = User.get(id, include_deleted=True)
                if not user:
                    self.error_("Can't change user info. Invalid user")
                    return redirect(url_for("UserAdmin:index"))

                delete_entry = True if request.form.get("delete-entry") else False
                if delete_entry:
                    user.update(status=user.STATUS_SUSPENDED)
                    user.delete()
                    self.success_("User DELETED Successfully!")
                    return redirect(url_for("UserAdmin:get", id=id))

                email = request.form.get("email")
                password = request.form.get("password")
                password2 = request.form.get("password2")
                name = request.form.get("name")
                role = request.form.get("user_role")
                status = request.form.get("user_status")
                upd = {}
                if email and email != user.email:
                    LoginView.change_login_handler(user_context=user)
                if password and password2:
                    LoginView.change_password_handler(user_context=user)
                if name != user.name:
                    upd.update({"name": name})
                if role and role != user.role:
                    upd.update({"role": role})
                if status and status != user.status:
                    if user.is_deleted and status == user.STATUS_ACTIVE:
                        user.delete(False)
                    upd.update({"status": status})
                if upd:
                    user.update(**upd)
                self.success_("User's Info updated successfully!")

            except Exception as ex:
                self.error_("Error: %s " % ex.message)
            return redirect(url_for("UserAdmin:get", id=id))


        @route("reset-password", methods=["POST"])
        def reset_password(self):
            try:
                id = request.form.get("id")
                user = User.get(id)
                if not user:
                    self.error_("Can't reset password. Invalid user")
                    return redirect(url_for("User:index"))

                password = LoginView.reset_password_handler(user_context=user)
                self.success_("User's password reset successfully!")
            except Exception as ex:
                self.error_("Error: %s " % ex.message)
            return redirect(url_for("UserAdmin:get", id=id))

        @route("create", methods=["POST"])
        def create(self):
            try:
                account = LoginView.signup_handler()
                account.set_role(request.form.get("role", "USER"))
                self.success_("User created successfully!")
                return redirect(url_for("UserAdmin:get", id=account.id))
            except Exception as ex:
                self.error_("Error: %s" % ex.message)
            return redirect(url_for("UserAdmin:index"))

    return UserAdmin