site/backend/app/models.py

37 lines
1.5 KiB
Python
Raw Normal View History

2024-08-01 13:35:28 +00:00
from datetime import datetime, timezone
from typing import Optional
import sqlalchemy as sa
import sqlalchemy.orm as so
2024-08-01 18:10:49 +00:00
from app import db, login
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
2024-08-01 13:35:28 +00:00
2024-08-01 18:10:49 +00:00
class User(UserMixin, db.Model):
2024-08-01 13:35:28 +00:00
id: so.Mapped[int] = so.mapped_column(primary_key=True)
username: so.Mapped[str] = so.mapped_column(sa.String(64), index=True, unique=True)
email: so.Mapped[str] = so.mapped_column(sa.String(120), index=True, unique=True)
password_hash: so.Mapped[Optional[str]] = so.mapped_column(sa.String(256))
posts: so.WriteOnlyMapped['Post'] = so.relationship(back_populates='author')
2024-08-01 18:10:49 +00:00
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
2024-08-01 13:35:28 +00:00
def __repr__(self):
return '<User {}>'.format(self.username)
class Post(db.Model):
id: so.Mapped[int] = so.mapped_column(primary_key=True)
body: so.Mapped[str] = so.mapped_column(sa.String(140))
timestamp: so.Mapped[datetime] = so.mapped_column(index=True, default=lambda: datetime.now(timezone.utc))
user_id: so.Mapped[int] = so.mapped_column(sa.ForeignKey(User.id), index=True)
author: so.Mapped[User] = so.relationship(back_populates='posts')
def __repr__(self):
return '<Post {}>'.format(self.body)
2024-08-01 18:10:49 +00:00
@login.user_loader
def load_user(id):
return db.session.get(User, int(id))