<commit_before>from datetime import datetime

from sqlalchemy import Column
from sqlalchemy_utils.types import phone_number
from werkzeug import generate_password_hash, check_password_hash
from flask.ext.login import UserMixin

from ..extensions import db
from .constants import (USER_ROLE, ADMIN, STAFF,
                        USER_STATUS, ACTIVE,
                        STRING_LEN)


class User(db.Model, UserMixin):
    __tablename__ = 'user_user'

    id = Column(db.Integer, primary_key=True)
    name = Column(db.String(STRING_LEN), nullable=False, unique=True)
    email = Column(db.String(STRING_LEN), nullable=False, unique=True)
    openid = Column(db.String(STRING_LEN), unique=True)
    activation_key = Column(db.String(STRING_LEN))
    created_time = Column(db.DateTime, default=datetime.utcnow)
    last_accessed = Column(db.DateTime)

    phone = Column(phone_number.PhoneNumberType())

    _password = Column('password', db.String(STRING_LEN*3), nullable=False)

    def __unicode__(self):
        return self.name

    def _get_password(self):
        return self._password

    def _set_password(self, password):
        self._password = generate_password_hash(password)
    # Hide password encryption by exposing password field only.
    password = db.synonym('_password',
                          descriptor=property(_get_password,
                                              _set_password))

    def check_password(self, password):
        if self.password is None:
            return False
        return check_password_hash(self.password, password)

    # One-to-many relationship between users and roles.
    role_code = Column(db.SmallInteger, default=STAFF)

    @property
    def role(self):
        return USER_ROLE[self.role_code]

    def is_admin(self):
        return self.role_code == ADMIN

    # One-to-many relationship between users and user_statuses.
    status_code = Column(db.SmallInteger, default=ACTIVE)

    @property
    def status(self):
        return USER_STATUS[self.status_code]

    # ================================================================
    # Class methods

    @classmethod
    def authenticate(cls, login, password):
        user = cls.query.filter(db.or_(User.name == login, User.email == login)).first()

        if user:
            authenticated = user.check_password(password)
        else:
            authenticated = False

        return user, authenticated
<commit_msg>use flask.ext.sqlalchemy fields, for discovery by alembic<commit_after>from datetime import datetime

from sqlalchemy_utils.types import phone_number
from werkzeug import generate_password_hash, check_password_hash
from flask.ext.login import UserMixin

from ..extensions import db
from .constants import (USER_ROLE, ADMIN, STAFF,
                        USER_STATUS, ACTIVE,
                        STRING_LEN)


class User(db.Model, UserMixin):
    __tablename__ = 'user_user'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(STRING_LEN), nullable=False, unique=True)
    email = db.Column(db.String(STRING_LEN), nullable=False, unique=True)
    openid = db.Column(db.String(STRING_LEN), unique=True)
    activation_key = db.Column(db.String(STRING_LEN))
    created_time = db.Column(db.DateTime, default=datetime.utcnow)
    last_accessed = db.Column(db.DateTime)

    phone = db.Column(phone_number.PhoneNumberType())

    _password = db.Column('password', db.String(STRING_LEN*3), nullable=False)

    def __unicode__(self):
        return self.name

    def _get_password(self):
        return self._password

    def _set_password(self, password):
        self._password = generate_password_hash(password)
    # Hide password encryption by exposing password field only.
    password = db.synonym('_password',
                          descriptor=property(_get_password,
                                              _set_password))

    def check_password(self, password):
        if self.password is None:
            return False
        return check_password_hash(self.password, password)

    # One-to-many relationship between users and roles.
    role_code = db.Column(db.SmallInteger, default=STAFF)

    @property
    def role(self):
        return USER_ROLE[self.role_code]

    def is_admin(self):
        return self.role_code == ADMIN

    # One-to-many relationship between users and user_statuses.
    status_code = db.Column(db.SmallInteger, default=ACTIVE)

    @property
    def status(self):
        return USER_STATUS[self.status_code]

    # ================================================================
    # Class methods

    @classmethod
    def authenticate(cls, login, password):
        user = cls.query.filter(db.or_(User.name == login, User.email == login)).first()

        if user:
            authenticated = user.check_password(password)
        else:
            authenticated = False

        return user, authenticated

{'fraction_non_alphanumeric': 0.11239134829189408, 'fraction_numerical': 0.0004042854255104103, 'mean_word_length': 2.977491961414791, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7289010', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1354, 'n_words': 318}