from flask_sqlalchemy import SQLAlchemy from flask_bcrypt import Bcrypt from sqlalchemy import Table, Column, Integer, ForeignKey, String import uuid db = SQLAlchemy() bcrypt = Bcrypt() user_event_association = Table('user_event', db.Model.metadata, Column('user_id', String(36), ForeignKey('user.id')), Column('event_id', Integer, ForeignKey('event.id')) ) class Event(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) description = db.Column(db.String(200), nullable=False) location = db.Column(db.String(100), nullable=False) deleted = db.Column(db.Boolean, default=False) duedate = db.Column(db.DateTime, nullable=False) created_at = db.Column(db.DateTime, default=db.func.now()) user_id = db.Column(db.String(36), db.ForeignKey('user.id'), nullable=False) users = db.relationship('User', secondary=user_event_association, back_populates='events') def to_dict(self): return { 'id': self.id, 'title': self.title, 'description': self.description, 'location': self.location, 'duedate': self.duedate.isoformat() if self.duedate else None, 'created_at': self.created_at.isoformat(), 'user_id': self.user_id } class User(db.Model): id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4())) name = db.Column(db.String(100), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password_hash = db.Column(db.String(128), nullable=False) location = db.Column(db.String(100), nullable=False) events = db.relationship('Event', secondary=user_event_association, back_populates='users') def set_password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode('utf-8') def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) def to_dict(self): return { 'id': self.id, 'name': self.name, 'email': self.email, 'location': self.location }