Spaces:
Running
Running
| from flask_sqlalchemy import SQLAlchemy | |
| from flask_login import UserMixin | |
| from werkzeug.security import generate_password_hash, check_password_hash | |
| from datetime import datetime # --- NEW: Imported for timestamps --- | |
| # Initialize SQLAlchemy | |
| db = SQLAlchemy() | |
| class User(UserMixin, db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| email = db.Column(db.String(120), unique=True, nullable=False) | |
| username = db.Column(db.String(80), unique=True, nullable=False) | |
| password_hash = db.Column(db.String(128)) | |
| # --- NEW: Relationship to link users to their detection logs --- | |
| logs = db.relationship('DetectionLog', backref='user', lazy=True) | |
| def set_password(self, password): | |
| """Hashes the password and stores it.""" | |
| self.password_hash = generate_password_hash(password) | |
| def check_password(self, password): | |
| """Verifies the hashed password.""" | |
| return check_password_hash(self.password_hash, password) | |
| def __repr__(self): | |
| return f"<User {self.username}>" | |
| # --- NEW: Model to store detection history logs --- | |
| class DetectionLog(db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) | |
| filename = db.Column(db.String(255), nullable=False) | |
| media_type = db.Column(db.String(50), nullable=False) # 'Video' or 'Image' | |
| prediction = db.Column(db.String(50), nullable=False) # 'FAKE' or 'REAL' | |
| confidence = db.Column(db.Float, nullable=False) | |
| timestamp = db.Column(db.DateTime, default=datetime.utcnow) | |
| def __repr__(self): | |
| return f"<DetectionLog {self.filename} - {self.prediction}>" |