open-law/app/models/user.py

58 lines
1.5 KiB
Python
Raw Normal View History

2023-04-20 13:10:16 +00:00
from uuid import uuid4
from flask_login import UserMixin, AnonymousUserMixin
from sqlalchemy import func
from sqlalchemy.ext.hybrid import hybrid_property
from werkzeug.security import generate_password_hash, check_password_hash
from app import db
2023-04-21 12:58:47 +00:00
from app.models.utils import BaseModel
2023-04-20 13:10:16 +00:00
from app.logger import log
from app import schema as s
def gen_password_reset_id() -> str:
return str(uuid4())
2023-04-21 12:58:47 +00:00
class User(BaseModel, UserMixin):
2023-04-20 13:10:16 +00:00
__tablename__ = "users"
username = db.Column(db.String(60), unique=True, nullable=False)
password_hash = db.Column(db.String(255), default="")
2023-04-21 12:58:47 +00:00
# Relationships
stars = db.relationship("Book", secondary="books_stars", back_populates="stars")
books = db.relationship("Book")
2023-04-20 13:10:16 +00:00
@hybrid_property
def password(self):
return self.password_hash
@password.setter
def password(self, password):
self.password_hash = generate_password_hash(password)
@classmethod
def authenticate(cls, user_id, password):
user = cls.query.filter(
func.lower(cls.username) == func.lower(user_id),
2023-04-20 13:10:16 +00:00
).first()
if not user:
log(log.WARNING, "user:[%s] not found", user_id)
if user is not None and check_password_hash(user.password, password):
return user
def __repr__(self):
return f"<{self.id}: {self.username}>"
2023-04-20 13:10:16 +00:00
@property
def json(self):
u = s.User.from_orm(self)
return u.json()
class AnonymousUser(AnonymousUserMixin):
pass