#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 18-8-12 下午5:10
# @Author  : Shark
# @File    : user.py
# @Software: PyCharm
from sqlalchemy import Column, Integer, String, SmallInteger
from werkzeug.security import generate_password_hash

from app.models.base import Base, db


class User(Base):
    id = Column(Integer, primary_key=True)
    email = Column(String(24), unique=True, nullable=False)
    nickname = Column(String(24), unique=True)
    auth = Column(SmallInteger, default=1)
    _password = Column('password', String(100))

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw):
        self._password = generate_password_hash(raw)

    @staticmethod
    def register_by_email(nickname, account, secret):
        with db.auto_commit():
            user.nickname = nickname
            user.email = account
            user.password = secret
            db.session.add(user)



