Skip to content

Commit

Permalink
server running!
Browse files Browse the repository at this point in the history
  • Loading branch information
azliu0 committed Apr 15, 2024
1 parent ea0e4b4 commit b9a99f7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
8 changes: 8 additions & 0 deletions server/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ruff: noqa: F401

"""Export server models."""

from server.models.document import Document
from server.models.email import Email
from server.models.response import Response
from server.models.thread import Thread
5 changes: 3 additions & 2 deletions server/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from server import db
from server.models.document_response import document_response_table

if TYPE_CHECKING:
from server.models.response import Response

from server.models.document_response import document_response_table


class Document(db.Model):
"""Document.
Expand All @@ -35,7 +36,7 @@ class Document(db.Model):
to_delete: Mapped[bool] = mapped_column(default=False, init=False)
response_count: Mapped[int] = mapped_column(default=0, init=False)

responses: Mapped[List[Response]] = relationship(
responses: Mapped[List["Response"]] = relationship(
secondary=document_response_table, back_populates="documents"
)

Expand Down
10 changes: 7 additions & 3 deletions server/models/document_response.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"""Association table for Document and Response."""

from sqlalchemy import Column, ForeignKey, Table
from sqlalchemy import Column, ForeignKey, Integer, Table

from server import db

document_response_table: Table = Table(
"document_response_table",
db.Model.metadata,
Column(
"document_id", ForeignKey("document.id", ondelete="CASCADE"), primary_key=True
"document_id",
Integer,
ForeignKey("Documents.id", ondelete="CASCADE"),
primary_key=True,
),
Column(
"response_id",
ForeignKey("response.id", ondelete="CASCADE"),
Integer,
ForeignKey("Responses.id", ondelete="CASCADE"),
primary_key=True,
),
)
9 changes: 5 additions & 4 deletions server/models/email.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Email."""

import datetime
from typing import TYPE_CHECKING, Optional

from sqlalchemy import DateTime, ForeignKey
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from server import db
Expand Down Expand Up @@ -32,20 +33,20 @@ class Email(db.Model):
__tablename__ = "Emails"

id: Mapped[str] = mapped_column(primary_key=True, init=False)
date: Mapped[DateTime] = mapped_column(nullable=False)
date: Mapped[datetime.datetime] = mapped_column(nullable=False)
sender: Mapped[str] = mapped_column(nullable=False)
subject: Mapped[str] = mapped_column(nullable=False)
body: Mapped[str] = mapped_column(nullable=False)
message_id: Mapped[str] = mapped_column(nullable=False)

response: Mapped[Optional[Response]] = relationship(
response: Mapped[Optional["Response"]] = relationship(
back_populates="email", init=False
)

is_reply: Mapped[bool] = mapped_column(nullable=False)

thread_id: Mapped[str] = mapped_column(ForeignKey("Threads.id"), nullable=False)
thread: Mapped[Thread] = relationship(back_populates="emails", init=False)
thread: Mapped["Thread"] = relationship(back_populates="emails", init=False)

def map(self):
"""Map the email to a dictionary."""
Expand Down
17 changes: 11 additions & 6 deletions server/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

from typing import TYPE_CHECKING, List

from sqlalchemy import ForeignKey, UniqueConstraint
from sqlalchemy import ForeignKey, Text, UniqueConstraint
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.types import Numeric

from server import db
from server.models.document_response import document_response_table

if TYPE_CHECKING:
from server.models.document import Document
from server.models.email import Email

from server.models.document_response import document_response_table


class Response(db.Model):
"""Response.
Expand All @@ -34,18 +37,20 @@ class Response(db.Model):

id: Mapped[str] = mapped_column(primary_key=True, init=False)
response: Mapped[str] = mapped_column(nullable=False)
questions: Mapped[List[str]] = mapped_column(nullable=False)
questions: Mapped[List[str]] = mapped_column(ARRAY(Text), nullable=False)

documents: Mapped[List[List[Document]]] = relationship(
documents: Mapped[List[List["Document"]]] = relationship(
secondary=document_response_table, back_populates="responses"
)

document_confidences: Mapped[List[List[float]]] = mapped_column(nullable=False)
document_confidences: Mapped[List[List[float]]] = mapped_column(
ARRAY(Numeric), nullable=False
)

confidence: Mapped[float] = mapped_column(nullable=False)

email_id: Mapped[str] = mapped_column(ForeignKey("Emails.id", ondelete="CASCADE"))
email: Mapped[Email] = relationship(
email: Mapped["Email"] = relationship(
back_populates="response", init=False, single_parent=True
)

Expand Down
2 changes: 1 addition & 1 deletion server/models/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Thread(db.Model):
last_email: Mapped[Optional[str]] = mapped_column(nullable=True, init=False)
resolved: Mapped[bool] = mapped_column(nullable=False, default=False)

emails: Mapped[List[Email]] = relationship(
emails: Mapped[List["Email"]] = relationship(
"Email",
back_populates="thread",
default_factory=list,
Expand Down

0 comments on commit b9a99f7

Please sign in to comment.