inkboard7 / models.py
aminskjen's picture
Update models.py
cb55930 verified
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from app import db
class User(UserMixin, db.Model):
"""User model for registration and authentication"""
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(255), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# Relationship to creations (One-to-Many)
creations = db.relationship('Creation', backref='user', lazy=True, cascade='all, delete-orphan')
def set_password(self, password):
"""Hash and set the user's password"""
self.password_hash = generate_password_hash(password)
def check_password(self, password):
"""Check if the given password matches the stored hash"""
return check_password_hash(self.password_hash, password)
def __repr__(self):
return f'<User {self.username}>'
class Creation(db.Model):
"""Model for creative user content (scene β†’ story + image)"""
__tablename__ = 'creations'
id = db.Column(db.String(36), primary_key=True) # UUID string
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
scene_idea = db.Column(db.Text, nullable=False)
story = db.Column(db.Text, nullable=False)
image_url = db.Column(db.Text, nullable=True)
journal_entry = db.Column(db.Text, nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
def __repr__(self):
return f'<Creation {self.id[:8]}>'