Event-CRUD-Flask-python3-API/models.py

49 lines
1.6 KiB
Python
Raw Normal View History

from flask_sqlalchemy import SQLAlchemy
2024-01-07 11:28:49 +00:00
from flask_bcrypt import Bcrypt
import uuid
db = SQLAlchemy()
2024-01-07 11:28:49 +00:00
bcrypt = Bcrypt()
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)
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-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)
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
}