File size: 1,558 Bytes
2f88c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Date, ForeignKey, CheckConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from psql_database import engine, Base
from passlib.hash import bcrypt

# Define the User model
class User(Base):
    __tablename__ = 'users'
    user_id = Column(Integer, primary_key=True)
    first_name = Column(String(50), nullable=False)
    last_name = Column(String(50), nullable=False)
    email = Column(String(100), nullable=False, unique=True)
    password = Column(String(100), nullable=False) # HASHED PASSWORD
    
    todos = relationship('Todo', back_populates='user')
    
    
    def verify_password(self, plain_password):
        return bcrypt.verify(plain_password, self.password)
    

    
# Define the Todo model
class Todo(Base):
    __tablename__ = 'todos'
    todo_id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('users.user_id'), nullable=False)
    task_name = Column(String(100), nullable=False)
    task_description = Column(String)
    priority = Column(Integer, CheckConstraint('priority >= 1 AND priority <= 3', name="priority should be either 1 or 2 or 3"), nullable=False) # 1: high, 2: medium, 3: low
    category = Column(String(50))
    due_date = Column(Date, nullable=False)
    status = Column(Boolean, default=False)
    
    user = relationship('User', back_populates='todos')

# Create the tables
def create_database_tables():
    return Base.metadata.create_all(bind=engine)