-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
122 additions
and
104 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
backend/alembic/versions/ecbdc859aca6_update_project_scheme.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""update project scheme | ||
Revision ID: ecbdc859aca6 | ||
Revises: 29a17fa183de | ||
Create Date: 2024-03-12 16:43:51.794097 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = 'ecbdc859aca6' | ||
down_revision: Union[str, None] = '29a17fa183de' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.alter_column('project', 'id', | ||
existing_type=sa.BIGINT(), | ||
type_=sa.Integer(), | ||
existing_nullable=False, | ||
autoincrement=True) | ||
op.alter_column('project', 'deadline', | ||
existing_type=sa.DATE(), | ||
type_=sa.DateTime(), | ||
existing_nullable=False) | ||
op.alter_column('project', 'subject_id', | ||
existing_type=sa.BIGINT(), | ||
type_=sa.Integer(), | ||
existing_nullable=True) | ||
op.alter_column('project', 'enroll_deadline', | ||
existing_type=sa.DATE(), | ||
type_=sa.DateTime(), | ||
existing_nullable=True) | ||
op.drop_constraint('project_subject_id_fkey', 'project', type_='foreignkey') | ||
op.create_foreign_key(None, 'project', 'subject', [ | ||
'subject_id'], ['id'], ondelete='CASCADE') | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint(None, 'project', type_='foreignkey') #type: ignore | ||
op.create_foreign_key('project_subject_id_fkey', 'project', 'subject', [ | ||
'subject_id'], ['id'], ondelete='SET NULL') | ||
op.alter_column('project', 'enroll_deadline', | ||
existing_type=sa.DateTime(), | ||
type_=sa.DATE(), | ||
existing_nullable=True) | ||
op.alter_column('project', 'subject_id', | ||
existing_type=sa.Integer(), | ||
type_=sa.BIGINT(), | ||
existing_nullable=True) | ||
op.alter_column('project', 'deadline', | ||
existing_type=sa.DateTime(), | ||
type_=sa.DATE(), | ||
existing_nullable=False) | ||
op.alter_column('project', 'id', | ||
existing_type=sa.Integer(), | ||
type_=sa.BIGINT(), | ||
existing_nullable=False, | ||
autoincrement=True) | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
from sqlalchemy import MetaData, create_engine | ||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine | ||
from sqlalchemy.orm import declarative_base | ||
from sqlalchemy.orm import sessionmaker | ||
from src import config | ||
|
||
SQLALCHEMY_DATABASE_URL = config.CONFIG.database_uri | ||
|
||
# TODO: migrate full codebase to async | ||
engine = create_engine(SQLALCHEMY_DATABASE_URL) | ||
async_engine = create_async_engine(SQLALCHEMY_DATABASE_URL[:len( | ||
"postgresql")] + "+asyncpg" + SQLALCHEMY_DATABASE_URL[len("postgresql"):]) | ||
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
AsyncSessionLocal = async_sessionmaker(async_engine, autoflush=False) | ||
|
||
Base = declarative_base() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,5 @@ | ||
# exceptions.py | ||
|
||
from fastapi import HTTPException | ||
|
||
|
||
def NoProjectsFoundException(subject_id: int): | ||
return HTTPException(status_code=404, detail=f"No projects found for subject {subject_id}") | ||
|
||
|
||
def UnauthorizedToCreateProjectException(): | ||
return HTTPException(status_code=403, detail="User is not authorized to create projects for this subject") | ||
|
||
|
||
def ProjectNotFoundException(): | ||
return HTTPException(status_code=404, detail="Project not found") | ||
|
||
|
||
def UnauthorizedToDeleteProjectException(): | ||
return HTTPException(status_code=403, detail="User is not authorized to delete this project") | ||
|
||
|
||
def UnauthorizedToUpdateProjectException(): | ||
return HTTPException(status_code=403, detail="User is not authorized to update projects for this subject") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,38 @@ | ||
from datetime import datetime, date | ||
from datetime import datetime, date, timezone | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel, Field, validator | ||
from pydantic import BaseModel, Field, validator, ConfigDict, field_validator | ||
|
||
|
||
class ProjectCreate(BaseModel): | ||
name: str = Field(..., min_length=1) | ||
deadline: datetime | ||
subject_id: int | ||
description: str | ||
|
||
# Check if deadline is not in the past | ||
@validator('deadline', pre=True, always=True) | ||
def validate_deadline(cls, value): | ||
if value < date.today(): | ||
@field_validator('deadline') | ||
def validate_deadline(cls, value: datetime) -> datetime: | ||
if value < datetime.now(value.tzinfo): | ||
raise ValueError('The deadline cannot be in the past') | ||
return value | ||
|
||
|
||
class ProjectResponse(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
id: int | ||
name: str | ||
deadline: datetime | ||
subject_id: int | ||
description: str | ||
|
||
class Config: | ||
orm_mode = True | ||
|
||
|
||
class ProjectUpdate(BaseModel): | ||
name: Optional[str] = Field(None, min_length=1) | ||
deadline: Optional[datetime] = None | ||
subject_id: Optional[int] = None | ||
description: Optional[str] = None | ||
|
||
@validator('deadline', pre=True, always=True) | ||
def validate_deadline(cls, value): | ||
if value is not None and value < datetime.now(): | ||
@field_validator('deadline') | ||
def validate_deadline(cls, value: datetime) -> datetime: | ||
if value is not None and value < datetime.now(value.tzinfo): | ||
raise ValueError('The deadline cannot be in the past') | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters