# 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()


from wtforms import (
    Form,
    TextField,
    PasswordField,
    validators
)
from wtforms.validators import ValidationError
from .models import Workspace
# -*- coding: utf-8 -*-



class EncryptedPasswordField(PasswordField):
    """Encrypt password after validation"""
    def post_validate(self, form, validation_stopped):
        self.data = Workspace.encode_password(self.data)


class WorkspaceForm(Form):
    name = TextField(
        u"name",
        [validators.Required(message=u'Please enter a workspace name.')]
    )
    email = TextField(
        u'Email', [
            validators.Required(message=u'Please enter an email address.'),
            validators.Email(message=u'Invalid email address.')
        ]
    )

    def validate_workspacename(self, field):
        value = field.data

# 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/workspaces/forms.py
from wtforms import (
    Form,
    TextField,
    PasswordField,
    validators
)
from wtforms.validators import ValidationError
from .models import Workspace

# -*- coding: utf-8 -*-



class EncryptedPasswordField(PasswordField):
    """Encrypt password after validation"""
    def post_validate(self, form, validation_stopped):
        self.data = Workspace.encode_password(self.data)


class WorkspaceForm(Form):
    name = TextField(
        u"name",
        [validators.Required(message=u'Please enter a workspace name.')]
    )
    email = TextField(
        u'Email', [
            validators.Required(message=u'Please enter an email address.'),
            validators.Email(message=u'Invalid email address.')
        ]
    )

    def validate_workspacename(self, field):
        value = field.data

        exists = Workspace().by_name(value)