Add user model.
This commit is contained in:
69
accountant/api/models/users.py
Normal file
69
accountant/api/models/users.py
Normal file
@ -0,0 +1,69 @@
|
||||
"""
|
||||
This file is part of Accountant.
|
||||
|
||||
Accountant is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Accountant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
# vim: set tw=80 ts=4 sw=4 sts=4:
|
||||
from passlib.hash import sha256_crypt
|
||||
from itsdangerous import (TimedJSONWebSignatureSerializer as Serializer,
|
||||
BadSignature, SignatureExpired)
|
||||
|
||||
from flask import current_app as app
|
||||
|
||||
from flask.ext.login import UserMixin
|
||||
|
||||
from sqlalchemy import true
|
||||
|
||||
from accountant import db
|
||||
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
email = db.Column(db.String(200), nullable=False, unique=True, index=True)
|
||||
password = db.Column(db.String(100), nullable=True)
|
||||
active = db.Column(db.Boolean, nullable=False, default=True,
|
||||
server_default=true())
|
||||
|
||||
@property
|
||||
def is_active(self):
|
||||
return self.active
|
||||
|
||||
@classmethod
|
||||
def query(cls, session):
|
||||
return session.query(cls)
|
||||
|
||||
@classmethod
|
||||
def hash_password(cls, password):
|
||||
return sha256_crypt.encrypt(password)
|
||||
|
||||
def verify_password(self, password):
|
||||
return sha256_crypt.verify(password, self.password)
|
||||
|
||||
def generate_auth_token(self, expiration=600):
|
||||
serializer = Serializer(app.config['SECRET_KEY'], expires_in=expiration)
|
||||
return serializer.dumps({'id': self.id})
|
||||
|
||||
@classmethod
|
||||
def verify_auth_token(cls, session, token):
|
||||
serializer = Serializer(app.config['SECRET_KEY'])
|
||||
|
||||
try:
|
||||
data = serializer.loads(token)
|
||||
except SignatureExpired:
|
||||
return None
|
||||
except BadSignature:
|
||||
return None
|
||||
|
||||
user = cls.query(session).get(data['id'])
|
||||
return user
|
Reference in New Issue
Block a user