File size: 1,891 Bytes
f2ef360
 
 
 
 
 
 
cb55930
f2ef360
 
 
 
 
 
 
 
cb55930
f2ef360
 
 
cb55930
f2ef360
 
 
cb55930
f2ef360
 
 
 
 
 
cb55930
f2ef360
 
cb55930
f2ef360
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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]}>'