2024-01-04 12:21:37 +00:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2024-01-07 11:28:49 +00:00
|
|
|
from flask_bcrypt import Bcrypt
|
2024-01-08 10:19:55 +00:00
|
|
|
from sqlalchemy import Table, Column, Integer, ForeignKey, String
|
2024-01-07 11:28:49 +00:00
|
|
|
import uuid
|
2024-01-04 12:21:37 +00:00
|
|
|
|
|
|
|
db = SQLAlchemy()
|
2024-01-07 11:28:49 +00:00
|
|
|
bcrypt = Bcrypt()
|
2024-01-04 12:21:37 +00:00
|
|
|
|
2024-01-08 10:19:55 +00:00
|
|
|
|
|
|
|
user_event_association = Table('user_event', db.Model.metadata,
|
|
|
|
Column('user_id', String(36), ForeignKey('user.id')),
|
|
|
|
Column('event_id', Integer, ForeignKey('event.id'))
|
|
|
|
)
|
|
|
|
|
2024-01-04 12:21:37 +00:00
|
|
|
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))
|
|
|
|
location = db.Column(db.String(100))
|
|
|
|
deleted = db.Column(db.Boolean, default=False)
|
|
|
|
duedate = db.Column(db.DateTime)
|
|
|
|
created_at = db.Column(db.DateTime, default=db.func.now())
|
2024-01-07 14:21:35 +00:00
|
|
|
user_id = db.Column(db.String(36), db.ForeignKey('user.id'), nullable=False)
|
2024-01-08 10:19:55 +00:00
|
|
|
users = db.relationship('User', secondary=user_event_association, back_populates='events')
|
|
|
|
|
2024-01-04 12:21:37 +00:00
|
|
|
|
|
|
|
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,
|
2024-01-07 14:21:35 +00:00
|
|
|
'created_at': self.created_at.isoformat(),
|
|
|
|
'user_id': self.user_id
|
2024-01-04 12:21:37 +00:00
|
|
|
}
|
2024-01-07 11:28:49 +00:00
|
|
|
|
|
|
|
class User(db.Model):
|
|
|
|
id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
|
|
|
|
name = db.Column(db.String(100))
|
|
|
|
email = db.Column(db.String(120), unique=True, nullable=False)
|
|
|
|
password_hash = db.Column(db.String(128))
|
|
|
|
location = db.Column(db.String(100), nullable=False)
|
2024-01-08 10:19:55 +00:00
|
|
|
events = db.relationship('Event', secondary=user_event_association, back_populates='users')
|
|
|
|
|
2024-01-07 11:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2024-01-08 10:19:55 +00:00
|
|
|
}
|
|
|
|
|