From 46656abbde6200c90ecff709dd8ff19d28425014 Mon Sep 17 00:00:00 2001 From: "Jean A. Eckelberg" Date: Tue, 3 Oct 2023 01:39:26 -0500 Subject: [PATCH 1/4] Updated models to include necessary relationships and created CRUD operations for the submission table --- server/crud/submission_CRUD.py | 54 +++++++++++++++++++++++++++++ server/models/errors.py | 7 +++- server/models/submission.py | 9 ++++- server/models/team.py | 7 +++- server/schemas/submission.py | 4 --- server/schemas/submission_schema.py | 11 +++--- 6 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 server/crud/submission_CRUD.py delete mode 100644 server/schemas/submission.py diff --git a/server/crud/submission_CRUD.py b/server/crud/submission_CRUD.py new file mode 100644 index 0000000..d648354 --- /dev/null +++ b/server/crud/submission_CRUD.py @@ -0,0 +1,54 @@ +import uuid +from typing import Type + +from sqlalchemy.orm import Session +from sqlalchemy import and_ + +from server.models.submission import Submission +from server.schemas.submission_schema import SubmissionBase, SubmissionWTeam, SubmissionSchema + + +def create(db: Session, submission: SubmissionWTeam) -> Submission: + db_submission: Submission = Submission(**submission.model_dump(exclude={'submission_id'})) + db.add(db_submission) + db.commit() + db.refresh(db_submission) + return db_submission + + +def read(db: Session, id: int) -> Submission | None: + return (db.query(Submission) + .filter(Submission.submission_id == id) + .first()) + + +def read_all_by_team_id(db: Session, team_uuid: uuid) -> list[Type[Submission]]: + return (db.query(Submission) + .filter(Submission.team_id_uuid == team_uuid) + .all()) + + +def update(db: Session, id: int, submission: SubmissionWTeam) -> Submission | None: + db_submission: Submission | None = (db.query(Submission) + .filter(Submission.submission_id == id).one_or_none()) + if db_submission is None: + return + + for key, value in submission.model_dump().items(): + setattr(db_submission, key, value) if value is not None else None + + db.commit() + db.refresh(db_submission) + return db_submission + + +def delete(db: Session, id: int, submission: SubmissionWTeam) -> None: + db_submission: Submission | None = (db.query(Submission) + .filter(and_(Submission.submission_id == id, + Submission.team_id_uuid == submission.team_id_uuid)) + .one_or_none()) + if db_submission is None: + return + + db.delete(db_submission) + db.commit() diff --git a/server/models/errors.py b/server/models/errors.py index 4f1f961..044791b 100644 --- a/server/models/errors.py +++ b/server/models/errors.py @@ -1,7 +1,10 @@ +from __future__ import annotations + from sqlalchemy import ForeignKey, Integer, String -from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy.orm import Mapped, mapped_column, relationship from .base import Base +from .submission import Submission class Errors(Base): @@ -10,3 +13,5 @@ class Errors(Base): run_id: Mapped[int] = mapped_column(Integer(), ForeignKey("run.run_id")) # run id submission_id: Mapped[int] = mapped_column(Integer(), ForeignKey("submission.submission_id")) # submission id fk error_txt: Mapped[str] = mapped_column(String(), nullable=True) + + submission: Mapped[Submission] = relationship(back_populates='error') diff --git a/server/models/submission.py b/server/models/submission.py index ec79b56..b2a3b7b 100644 --- a/server/models/submission.py +++ b/server/models/submission.py @@ -1,7 +1,11 @@ +from __future__ import annotations + from sqlalchemy import LargeBinary, ForeignKey, Integer, DateTime -from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy.orm import Mapped, mapped_column, relationship from .base import Base +from .errors import Errors +from .team import Team class Submission(Base): @@ -10,3 +14,6 @@ class Submission(Base): team_id_uuid: Mapped[int] = mapped_column(Integer(), ForeignKey("team.team_id_uuid")) submission_time: Mapped[str] = mapped_column(DateTime(), nullable=False) file_txt: Mapped[str] = mapped_column(LargeBinary(), nullable=False) + + team: Mapped[Team] = relationship(back_populates="submissions") + error: Mapped[Errors] = relationship(back_populates='submission') diff --git a/server/models/team.py b/server/models/team.py index a898afc..eb129e0 100644 --- a/server/models/team.py +++ b/server/models/team.py @@ -1,7 +1,10 @@ +from __future__ import annotations + from sqlalchemy import CheckConstraint, ForeignKey, Integer, String -from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy.orm import Mapped, mapped_column, relationship from .base import Base +from .submission import Submission from uuid import uuid4 @@ -11,3 +14,5 @@ class Team(Base): uni_id: Mapped[int] = mapped_column(Integer(), ForeignKey("university.uni_id")) team_type_id: Mapped[int] = mapped_column(Integer(), ForeignKey("team_type.team_type_id")) team_name: Mapped[str] = mapped_column(String(), CheckConstraint("team_name != ''"), unique=True, nullable=False) + + submissions: Mapped[list[Submission]] = relationship(back_populates="team") diff --git a/server/schemas/submission.py b/server/schemas/submission.py deleted file mode 100644 index c7d6107..0000000 --- a/server/schemas/submission.py +++ /dev/null @@ -1,4 +0,0 @@ -from pydantic import BaseModel - -class SubmissionBase(BaseModel): - ... \ No newline at end of file diff --git a/server/schemas/submission_schema.py b/server/schemas/submission_schema.py index e793a86..f2dbffa 100644 --- a/server/schemas/submission_schema.py +++ b/server/schemas/submission_schema.py @@ -5,7 +5,6 @@ class SubmissionBase(BaseModel): submission_id: int - team_id_uuid: int submission_time: str file_txt: str @@ -13,6 +12,10 @@ class Config: from_attributes = True -class SubmissionSchema(SubmissionBase): - team_id: list[team_schema.TeamBase] = [] - errors: list[errors_schema.ErrorsBase] = [] +class SubmissionWTeam(SubmissionBase): + team_id_uuid: int + + +class SubmissionSchema(SubmissionWTeam): + team: team_schema.TeamBase + error: errors_schema.ErrorsBase From 848c64f16fb66f0698bc215cdfd22d8763978886 Mon Sep 17 00:00:00 2001 From: "Jean A. Eckelberg" Date: Tue, 3 Oct 2023 18:35:13 -0500 Subject: [PATCH 2/4] Added error_id --- server/models/submission.py | 1 + server/schemas/submission_schema.py | 1 + 2 files changed, 2 insertions(+) diff --git a/server/models/submission.py b/server/models/submission.py index b2a3b7b..f476d28 100644 --- a/server/models/submission.py +++ b/server/models/submission.py @@ -12,6 +12,7 @@ class Submission(Base): __tablename__: str = 'submission' submission_id: Mapped[int] = mapped_column(Integer(), primary_key=True, autoincrement=True) team_id_uuid: Mapped[int] = mapped_column(Integer(), ForeignKey("team.team_id_uuid")) + error_id: Mapped[int] = mapped_column(Integer(), ForeignKey("errors.error_id")) submission_time: Mapped[str] = mapped_column(DateTime(), nullable=False) file_txt: Mapped[str] = mapped_column(LargeBinary(), nullable=False) diff --git a/server/schemas/submission_schema.py b/server/schemas/submission_schema.py index f2dbffa..13b1486 100644 --- a/server/schemas/submission_schema.py +++ b/server/schemas/submission_schema.py @@ -5,6 +5,7 @@ class SubmissionBase(BaseModel): submission_id: int + error_id: int submission_time: str file_txt: str From aa7b17f6adbf7527c8dadd7c4225646d1bdf3f09 Mon Sep 17 00:00:00 2001 From: "Jean A. Eckelberg" Date: Tue, 3 Oct 2023 18:39:21 -0500 Subject: [PATCH 3/4] formatting --- server/crud/submission_CRUD.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/crud/submission_CRUD.py b/server/crud/submission_CRUD.py index d648354..6c8edec 100644 --- a/server/crud/submission_CRUD.py +++ b/server/crud/submission_CRUD.py @@ -30,7 +30,8 @@ def read_all_by_team_id(db: Session, team_uuid: uuid) -> list[Type[Submission]]: def update(db: Session, id: int, submission: SubmissionWTeam) -> Submission | None: db_submission: Submission | None = (db.query(Submission) - .filter(Submission.submission_id == id).one_or_none()) + .filter(Submission.submission_id == id) + .one_or_none()) if db_submission is None: return From aa67c4f899ce0c21fdb18d39fc7efa15bba031fb Mon Sep 17 00:00:00 2001 From: "Jean A. Eckelberg" Date: Tue, 3 Oct 2023 18:41:31 -0500 Subject: [PATCH 4/4] helped make endpoint more secure --- server/crud/submission_CRUD.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/crud/submission_CRUD.py b/server/crud/submission_CRUD.py index 6c8edec..8dafe97 100644 --- a/server/crud/submission_CRUD.py +++ b/server/crud/submission_CRUD.py @@ -30,7 +30,8 @@ def read_all_by_team_id(db: Session, team_uuid: uuid) -> list[Type[Submission]]: def update(db: Session, id: int, submission: SubmissionWTeam) -> Submission | None: db_submission: Submission | None = (db.query(Submission) - .filter(Submission.submission_id == id) + .filter(and_(Submission.submission_id == id, + Submission.team_id_uuid == submission.team_id_uuid)) .one_or_none()) if db_submission is None: return