Mail box and OCR done
This commit is contained in:
66
backend/database.py
Normal file
66
backend/database.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import os
|
||||
from sqlalchemy import create_engine, Column, Integer, String, Text, DateTime, Boolean, ForeignKey, LargeBinary
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker, relationship
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
DB_USER = os.getenv("DB_USER")
|
||||
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
||||
DB_HOST = os.getenv("DB_HOST")
|
||||
DB_PORT = os.getenv("DB_PORT")
|
||||
import urllib.parse
|
||||
|
||||
# ... (imports)
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
DB_USER = os.getenv("DB_USER")
|
||||
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
||||
DB_HOST = os.getenv("DB_HOST")
|
||||
DB_PORT = os.getenv("DB_PORT")
|
||||
DB_NAME = os.getenv("DB_NAME")
|
||||
|
||||
encoded_user = urllib.parse.quote_plus(DB_USER)
|
||||
encoded_password = urllib.parse.quote_plus(DB_PASSWORD)
|
||||
|
||||
# SQLAlchemy Database URL
|
||||
DATABASE_URL = f"postgresql://{encoded_user}:{encoded_password}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
|
||||
|
||||
engine = create_engine(DATABASE_URL)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
class Email(Base):
|
||||
__tablename__ = "emails"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
subject = Column(String, index=True)
|
||||
sender = Column(String, index=True)
|
||||
body = Column(Text)
|
||||
received_date = Column(DateTime)
|
||||
is_read = Column(Boolean, default=False)
|
||||
|
||||
attachments = relationship("Attachment", back_populates="email")
|
||||
|
||||
class Attachment(Base):
|
||||
__tablename__ = "attachments"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
email_id = Column(Integer, ForeignKey("emails.id"))
|
||||
filename = Column(String)
|
||||
content_type = Column(String)
|
||||
file_content = Column(LargeBinary) # Storing content directly in DB as requested
|
||||
|
||||
email = relationship("Email", back_populates="attachments")
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user