hfexample's picture
Deploy clean snapshot of the repository
e221c83
from . import db
from werkzeug.security import generate_password_hash, check_password_hash
import uuid
from sqlalchemy.sql import func
import datetime
from datetime import timezone, timedelta
class User(db.Model):
id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
username = db.Column(db.String(80), unique=True, nullable=False)
nickname = db.Column(db.String(80), nullable=True)
password_hash = db.Column(db.String(256), nullable=False)
diaries = db.relationship('Diary', backref='author', lazy=True, cascade="all, delete-orphan")
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)
class Diary(db.Model):
id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
user_id = db.Column(db.String(36), db.ForeignKey('user.id'), nullable=False)
content = db.Column(db.Text, nullable=False)
emotion = db.Column(db.String(20), nullable=False)
recommendation = db.Column(db.Text, nullable=True)
created_at = db.Column(db.DateTime(timezone=True), default=func.now())
__table_args__ = (db.Index('idx_diary_user_id_created_at', "user_id", "created_at"),)