# Path: makesite/modules/flask/base/ext.py
# def config_extensions(app):
# def config_babel(app):
#     def get_locale():
# 
# Path: makesite/modules/flask/base/auth/models.py
# class User(db.Model, UserMixin, BaseMixin):
#     " Main user model. "
# 
#     __tablename__ = 'users_user'
# 
#     username = db.Column(db.String(50), unique=True)
#     email = db.Column(db.String(120))
#     active = db.Column(db.Boolean, default=True)
#     _pw_hash = db.Column(db.String(199), nullable=False)
# 
#     # OAuth creds
#     oauth_token = db.Column(db.String(200))
#     oauth_secret = db.Column(db.String(200))
# 
#     @declared_attr
#     def roles(self):
#         assert self
#         return db.relationship("Role", secondary=userroles, backref="users")
# 
#     @hybrid_property
#     def pw_hash(self):
#         """Simple getter function for the user's password."""
#         return self._pw_hash
# 
#     @pw_hash.setter
#     def pw_hash(self, raw_password):
#         """ Password setter, that handles the hashing
#             in the database.
#         """
#         self._pw_hash = generate_password_hash(raw_password)
# 
#     @staticmethod
#     def permission(role):
#         perm = Permission(RoleNeed(role))
#         return perm.can()
# 
#     def check_password(self, password):
#         return check_password_hash(self.pw_hash, password)
# 
#     def is_active(self):
#         return self.active
# 
#     def __unicode__(self):
#         return self.username
# 
#     def __repr__(self):
#         return '<User %r>' % (self.username)


from flask import Blueprint
from flask_login import LoginManager, login_required, logout_user, login_user, current_user
from flask_principal import Principal, identity_changed, Identity, AnonymousIdentity, identity_loaded, UserNeed, RoleNeed
from ..ext import db
from .models import User


class UserManager(Blueprint):

    def __init__(self, *args, **kwargs):
        self._login_manager = None
        self._principal = None
        self.app = None
        super(UserManager, self).__init__(*args, **kwargs)

    def register(self, app, *args, **kwargs):
        " Activate loginmanager and principal. "

        if not self._login_manager or self.app != app:
            self._login_manager = LoginManager()
            self._login_manager.user_callback = self.user_loader
            self._login_manager.setup_app(app)
            self._login_manager.login_view = 'urls.index'
            self._login_manager.login_message = u'You need to be signed in for this page.'

        self.app = app

        if not self._principal:
            self._principal = Principal(app)
            identity_loaded.connect(self.identity_loaded)

        super(UserManager, self).register(app, *args, **kwargs)

    @staticmethod
    def user_loader(pk):

# Path: makesite/modules/flask/base/ext.py
# def config_extensions(app):
# def config_babel(app):
#     def get_locale():
# 
# Path: makesite/modules/flask/base/auth/models.py
# class User(db.Model, UserMixin, BaseMixin):
#     " Main user model. "
# 
#     __tablename__ = 'users_user'
# 
#     username = db.Column(db.String(50), unique=True)
#     email = db.Column(db.String(120))
#     active = db.Column(db.Boolean, default=True)
#     _pw_hash = db.Column(db.String(199), nullable=False)
# 
#     # OAuth creds
#     oauth_token = db.Column(db.String(200))
#     oauth_secret = db.Column(db.String(200))
# 
#     @declared_attr
#     def roles(self):
#         assert self
#         return db.relationship("Role", secondary=userroles, backref="users")
# 
#     @hybrid_property
#     def pw_hash(self):
#         """Simple getter function for the user's password."""
#         return self._pw_hash
# 
#     @pw_hash.setter
#     def pw_hash(self, raw_password):
#         """ Password setter, that handles the hashing
#             in the database.
#         """
#         self._pw_hash = generate_password_hash(raw_password)
# 
#     @staticmethod
#     def permission(role):
#         perm = Permission(RoleNeed(role))
#         return perm.can()
# 
#     def check_password(self, password):
#         return check_password_hash(self.pw_hash, password)
# 
#     def is_active(self):
#         return self.active
# 
#     def __unicode__(self):
#         return self.username
# 
#     def __repr__(self):
#         return '<User %r>' % (self.username)


# Path: makesite/modules/flask/base/auth/manager.py
from flask import Blueprint
from flask_login import LoginManager, login_required, logout_user, login_user, current_user
from flask_principal import Principal, identity_changed, Identity, AnonymousIdentity, identity_loaded, UserNeed, RoleNeed
from ..ext import db
from .models import User



class UserManager(Blueprint):

    def __init__(self, *args, **kwargs):
        self._login_manager = None
        self._principal = None
        self.app = None
        super(UserManager, self).__init__(*args, **kwargs)

    def register(self, app, *args, **kwargs):
        " Activate loginmanager and principal. "

        if not self._login_manager or self.app != app:
            self._login_manager = LoginManager()
            self._login_manager.user_callback = self.user_loader
            self._login_manager.setup_app(app)
            self._login_manager.login_view = 'urls.index'
            self._login_manager.login_message = u'You need to be signed in for this page.'

        self.app = app

        if not self._principal:
            self._principal = Principal(app)
            identity_loaded.connect(self.identity_loaded)

        super(UserManager, self).register(app, *args, **kwargs)

    @staticmethod
    def user_loader(pk):

        return User.query.options(db.joinedload(User.roles)).get(pk)