<commit_before># coding=utf-8
import logging

from sqlalchemy import (
    Table, Column, Integer, Unicode, String, DateTime, Boolean, ForeignKey,
)
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import validates, relationship, backref

from .utils import get_uhmac, get_token
from ._compat import to_unicode, to_native


class DictSerializable(object):
    """Makes an object serializable to a dictionary.
    """

    def _asdict(self):
        repr(self)  # Force to load the data
        dictrep = self.__dict__.copy()
        dictrep.pop('_sa_instance_state', None)
        return dictrep

    to_dict = _asdict


def extend_user_model(auth, UserMixin=None, roles=False):
    db = auth.db
    AuthUserMixin = get_auth_user_mixin(auth, roles=roles)

    if UserMixin is not None:
        parents = (UserMixin, AuthUserMixin, DictSerializable, db.Model)
        tablename = getattr(UserMixin, '__tablename__', None)
        if not tablename:
            tablename = auth.users_model_name.lower().rstrip('s') + 's'
    else:
        parents = (AuthUserMixin, DictSerializable, db.Model)
        tablename = auth.users_model_name.lower().rstrip('s') + 's'

    return type(auth.users_model_name, parents, {'__tablename__': tablename})


def get_auth_user_mixin(auth, roles=False):
    db = auth.db

    class AuthUserMixin(object):
        id = Column(Integer, primary_key=True)
        login = Column(Unicode, nullable=False, unique=True, index=True)
        password = Column(String(255), nullable=True)
        last_sign_in = Column(DateTime, nullable=True)
        deleted = Column(Boolean, default=False)

        @hybrid_property
        def _password(self):
            """Backwards compatibility fix."""
            return self.password

        @_password.setter
        def _password(self, value):
            """Backwards compatibility fix."""
            self.set_raw_password(value)

        @validates('password')
        def __hash_password(self, key, secret):
            logger = logging.getLogger(__name__)
            logger.debug(u'Hash updated for user `{0}`'.format(self.login))
            return auth.hash_password(secret)

        @validates('login')
        def __clean_login(self, key, login):
            login = to_unicode(login or u'').strip()
            if auth.case_insensitive:
                login = login.lower()
            return login
<commit_msg>Add a length to the login column to be usable in lesser db dialects<commit_after># coding=utf-8
import logging

from sqlalchemy import (
    Table, Column, Integer, Unicode, String, DateTime, Boolean, ForeignKey,
)
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import validates, relationship, backref

from .utils import get_uhmac, get_token
from ._compat import to_unicode, to_native


class DictSerializable(object):
    """Makes an object serializable to a dictionary.
    """

    def _asdict(self):
        repr(self)  # Force to load the data
        dictrep = self.__dict__.copy()
        dictrep.pop('_sa_instance_state', None)
        return dictrep

    to_dict = _asdict


def extend_user_model(auth, UserMixin=None, roles=False):
    db = auth.db
    AuthUserMixin = get_auth_user_mixin(auth, roles=roles)

    if UserMixin is not None:
        parents = (UserMixin, AuthUserMixin, DictSerializable, db.Model)
        tablename = getattr(UserMixin, '__tablename__', None)
        if not tablename:
            tablename = auth.users_model_name.lower().rstrip('s') + 's'
    else:
        parents = (AuthUserMixin, DictSerializable, db.Model)
        tablename = auth.users_model_name.lower().rstrip('s') + 's'

    return type(auth.users_model_name, parents, {'__tablename__': tablename})


def get_auth_user_mixin(auth, roles=False):
    db = auth.db

    class AuthUserMixin(object):
        id = Column(Integer, primary_key=True)
        login = Column(Unicode(255), nullable=False, unique=True, index=True)
        password = Column(String(255), nullable=True)
        last_sign_in = Column(DateTime, nullable=True)
        deleted = Column(Boolean, default=False)

        @hybrid_property
        def _password(self):
            """Backwards compatibility fix."""
            return self.password

        @_password.setter
        def _password(self, value):
            """Backwards compatibility fix."""
            self.set_raw_password(value)

        @validates('password')
        def __hash_password(self, key, secret):
            logger = logging.getLogger(__name__)
            logger.debug(u'Hash updated for user `{0}`'.format(self.login))
            return auth.hash_password(secret)

        @validates('login')
        def __clean_login(self, key, login):
            login = to_unicode(login or u'').strip()
            if auth.case_insensitive:
                login = login.lower()
            return login

{'fraction_non_alphanumeric': 0.1024380249948781, 'fraction_numerical': 0.0026633886498668305, 'mean_word_length': 2.8993610223642174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1829461', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1442, 'n_words': 355}