# Path: scribeui_pyramid/modules/app/sqla.py
# def get_session(request):
# def init_sqla(settings):
#     def __props__(self, convert_date=False):
#     def __todict__(self):
#         def convert_datetime(value):
#     def __iter__(self):
#     def __json__(self, request):
#     def fromdict(self, values):
#     def all(cls, page=None, limit=None):
#     def by_id(cls, id):
# def includeme(config):
# class BaseMixin(object):
# 
# Path: scribeui_pyramid/modules/workspaces/models.py
# class Workspace(Base, BaseMixin):
# 
#     __tablename__ = 'workspaces'
#     id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
#     name = sa.Column(sa.Unicode(30), nullable=False, unique=True)
#     password = sa.Column(sa.Unicode(256))
# 
# 
#     def __repr__(self):
#         return u"<Workspace('{0}')>".format(self.name)
# 
# 
#     @staticmethod
#     def password_manager():
#         return DelegatingPasswordManager(preferred=BCRYPTPasswordManager())
# 
# 
#     @staticmethod
#     def encode_password(raw):
#         return Workspace.password_manager().encode(raw)
# 
# 
#     @staticmethod
#     def check_password(hashed, raw):
#         return Workspace.password_manager().check(
#             hashed,
#             raw,
#             setter=Workspace.encode_password
#         )
# 
# 
#     @classmethod
#     def authenticate(cls, name, password):
#         workspace = DBSession.query(cls)\
#             .filter_by(name=name)\
#             .first()
# 
#         if workspace:
#             if workspace.password and password and Workspace.check_password(workspace.password, password):
#                 return workspace
#             elif not workspace.password and not password:
#                 return workspace
#         return None
# 
# 
#     @classmethod
#     def by_name(cls, name):
#         q = DBSession.query(cls)\
#             .filter(sa.func.lower(cls.name) == name.lower())
# 
#         return q.first()
# 
# 
#     def get_maps(self, type=None):
#         q = Map.query.filter(Map.workspace_id == self.id)
# 
#         if type:
#             q = q.filter(Map.type == type)
# 
#         return q.all()
# 
# 
#     def get_map_by_name(self, name):
#         q = DBSession.query(Map)\
#             .filter(Map.name == name.lower(), Map.workspace_id == self.id)
# 
#         return q.first()
# 
# Path: scribeui_pyramid/modules/webui/views.py
# class BaseView(object):
#     def __init__(self, request):
#         self.request = request
#         self.matchdict = request.matchdict
#         self.lm = request.layout_manager
# 
# Path: scribeui_pyramid/modules/auth/forms.py
# class LoginForm(Form):
#     name = TextField(
#         u'Workspace',
#         [validators.Required(message=u'Please select a workspace.')]
#     )
#     password = PasswordField(
#         u'Password'
#     )


import logging
from pyramid.httpexceptions import HTTPFound
from pyramid.security import (
    authenticated_userid,
    forget,
    remember
)
from pyramid.view import (
    forbidden_view_config,
    view_config
)
from ..app.sqla import DBSession
from ..workspaces.models import Workspace
from ..webui.views import BaseView
from .forms import LoginForm

    @view_config(
        route_name='auth.login',
        renderer='scribeui_pyramid:modules/auth/templates/login.jinja2'
    )
    @forbidden_view_config(renderer='scribeui_pyramid:modules/auth/templates/login.jinja2')
    def login(self):
        home_url = self.request.route_url('home')

        params = self.request.POST.copy()
        form = LoginForm(params)

        if params and form.validate():
            workspace = Workspace().authenticate(form.name.data, form.password.data)

            if workspace:
                log.info('Successful login for {0}'.format(form.name.data))
                headers = remember(self.request, workspace.name)
                return HTTPFound(location=home_url, headers=headers)
            else:
                self.request.session.flash(
                    'Wrong credentials!',
                    queue='warning'
                )
                log.warning('Failed login for {0}'.format(form.name.data))

        plugins_js = [
            self.request.static_url('scribeui_pyramid:static/js/login.js')
        ]


# Path: scribeui_pyramid/modules/app/sqla.py
# def get_session(request):
# def init_sqla(settings):
#     def __props__(self, convert_date=False):
#     def __todict__(self):
#         def convert_datetime(value):
#     def __iter__(self):
#     def __json__(self, request):
#     def fromdict(self, values):
#     def all(cls, page=None, limit=None):
#     def by_id(cls, id):
# def includeme(config):
# class BaseMixin(object):
# 
# Path: scribeui_pyramid/modules/workspaces/models.py
# class Workspace(Base, BaseMixin):
# 
#     __tablename__ = 'workspaces'
#     id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
#     name = sa.Column(sa.Unicode(30), nullable=False, unique=True)
#     password = sa.Column(sa.Unicode(256))
# 
# 
#     def __repr__(self):
#         return u"<Workspace('{0}')>".format(self.name)
# 
# 
#     @staticmethod
#     def password_manager():
#         return DelegatingPasswordManager(preferred=BCRYPTPasswordManager())
# 
# 
#     @staticmethod
#     def encode_password(raw):
#         return Workspace.password_manager().encode(raw)
# 
# 
#     @staticmethod
#     def check_password(hashed, raw):
#         return Workspace.password_manager().check(
#             hashed,
#             raw,
#             setter=Workspace.encode_password
#         )
# 
# 
#     @classmethod
#     def authenticate(cls, name, password):
#         workspace = DBSession.query(cls)\
#             .filter_by(name=name)\
#             .first()
# 
#         if workspace:
#             if workspace.password and password and Workspace.check_password(workspace.password, password):
#                 return workspace
#             elif not workspace.password and not password:
#                 return workspace
#         return None
# 
# 
#     @classmethod
#     def by_name(cls, name):
#         q = DBSession.query(cls)\
#             .filter(sa.func.lower(cls.name) == name.lower())
# 
#         return q.first()
# 
# 
#     def get_maps(self, type=None):
#         q = Map.query.filter(Map.workspace_id == self.id)
# 
#         if type:
#             q = q.filter(Map.type == type)
# 
#         return q.all()
# 
# 
#     def get_map_by_name(self, name):
#         q = DBSession.query(Map)\
#             .filter(Map.name == name.lower(), Map.workspace_id == self.id)
# 
#         return q.first()
# 
# Path: scribeui_pyramid/modules/webui/views.py
# class BaseView(object):
#     def __init__(self, request):
#         self.request = request
#         self.matchdict = request.matchdict
#         self.lm = request.layout_manager
# 
# Path: scribeui_pyramid/modules/auth/forms.py
# class LoginForm(Form):
#     name = TextField(
#         u'Workspace',
#         [validators.Required(message=u'Please select a workspace.')]
#     )
#     password = PasswordField(
#         u'Password'
#     )


# Path: scribeui_pyramid/modules/auth/views.py
import logging
from pyramid.httpexceptions import HTTPFound
from pyramid.security import (
    authenticated_userid,
    forget,
    remember
)
from pyramid.view import (
    forbidden_view_config,
    view_config
)
from ..app.sqla import DBSession
from ..workspaces.models import Workspace
from ..webui.views import BaseView
from .forms import LoginForm


    @view_config(
        route_name='auth.login',
        renderer='scribeui_pyramid:modules/auth/templates/login.jinja2'
    )
    @forbidden_view_config(renderer='scribeui_pyramid:modules/auth/templates/login.jinja2')
    def login(self):
        home_url = self.request.route_url('home')

        params = self.request.POST.copy()
        form = LoginForm(params)

        if params and form.validate():
            workspace = Workspace().authenticate(form.name.data, form.password.data)

            if workspace:
                log.info('Successful login for {0}'.format(form.name.data))
                headers = remember(self.request, workspace.name)
                return HTTPFound(location=home_url, headers=headers)
            else:
                self.request.session.flash(
                    'Wrong credentials!',
                    queue='warning'
                )
                log.warning('Failed login for {0}'.format(form.name.data))

        plugins_js = [
            self.request.static_url('scribeui_pyramid:static/js/login.js')
        ]


        workspaces = DBSession.query(Workspace.name).order_by(Workspace.name).all()