Skip to content

Commit

Permalink
make linter happy :)
Browse files Browse the repository at this point in the history
  • Loading branch information
miboelae committed Mar 11, 2024
1 parent 282a32a commit f36e143
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
13 changes: 9 additions & 4 deletions backend/src/group/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@
from . import service
from .exceptions import NotAuthorized, NotAuthorizedToLeave, GroupNotFound

async def retrieve_group(group_id: int, db: Session = Depends(get_db)) -> Group: #as teammember and as not teammember

# as teammember and as not teammember
async def retrieve_group(group_id: int, db: Session = Depends(get_db)) -> Group:
group = await service.get_group_by_id(db, group_id)
if not group:
raise GroupNotFound()
return Group.model_validate(group)


async def retrieve_groups_by_user(user: User, db: Session = Depends(get_db)) -> GroupList:
grouplist = await service.get_groups_by_user(db, user.uid)
return GroupList.model_validate(grouplist)


async def retrieve_groups_by_project(project: Project, db: Session = Depends(get_db)) -> GroupList:
grouplist = await service.get_groups_by_project(db, project.id)
return GroupList.model_validate(grouplist)
grouplist = await service.get_groups_by_project(db, project.id)
return GroupList.model_validate(grouplist)


async def is_authorized_user(member: bool, group_id: int, user: User = Depends(get_authenticated_user), db: Session = Depends(get_db)):
groups = await service.get_groups_by_user(db, user.uid)
if member:
if not any(group.id == group_id for group in groups):
raise NotAuthorized()
raise NotAuthorized()
3 changes: 2 additions & 1 deletion backend/src/group/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ def __init__(self):
"""Raised when group is not found in database"""
super().__init__(status_code=404, detail="Group not found")


class NotAuthorized(HTTPException):
def __init__(self):
"""Raised when user is not privileged enough to do this action"""
super().__init__(status_code=403, detail="Not Authorized")


class NotAuthorizedToLeave(HTTPException):
def __init__(self):
"""Raised when user is not allowed to leave current group"""
super().__init__(status_code=403, detail="Not authorized to leave the group")

3 changes: 2 additions & 1 deletion backend/src/group/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
Column("team_id", ForeignKey("team.id")),
)


class Group(Base):
__tablename__ = "team"

id: Mapped[int] = mapped_column(primary_key=True)
team_name: Mapped[str]
score: Mapped[int]
project_id: Mapped[int]
deadline: Mapped[Date] # TODO: should be added to project
deadline: Mapped[Date] # TODO: should be added to project
6 changes: 5 additions & 1 deletion backend/src/group/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@
responses={404: {"description": "Not found"}},
)


@router.get("/")
async def get_groups(groups : GroupList = Depends(retrieve_groups_by_project)):
async def get_groups(groups: GroupList = Depends(retrieve_groups_by_project)):
return groups


@router.get("/{group_id}")
async def get_group(group: Group = Depends(retrieve_group)):
return group


@router.delete("/{group_id}",
dependencies=[Depends(user_permission_validation, is_authorized_user(True))], status_code=200)
async def leave_group(subject_id: int, db: Session = Depends(get_db)):
await service.leave_group(db, subject_id)
return "Successfully deleted"


@router.post("/{group_id}",
dependencies=[Depends(user_permission_validation, is_authorized_user(False))], status_code=201)
async def join_group(subject_id: int, db: Session = Depends(get_db)):
Expand Down
7 changes: 4 additions & 3 deletions backend/src/group/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydantic import BaseModel, Field
from typing import Sequence


class Groupbase(BaseModel):
project_id: int
score: int
Expand All @@ -15,13 +16,13 @@ class Group(Groupbase):
team_name: str = Field(min_length=1)


class GroupJoin(Groupbase): #TODO needed?
class GroupJoin(Groupbase): # TODO needed?
pass


class GroupLeave(Groupbase): #TODO needed?
class GroupLeave(Groupbase): # TODO needed?
pass


class GroupList(BaseModel):
pass
pass
4 changes: 2 additions & 2 deletions backend/src/group/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ async def get_group_by_id(db: Session, group_id: int) -> models.Group:

async def get_groups_by_project(db: Session, project_id: int) -> list[models.Group]:
return (db.query(models.Group).join(models.Project).
filter(models.Group.project_id == project_id).all())
filter(models.Group.project_id == project_id).all())


async def get_groups_by_user(db: Session, user_id: int) -> list[models.Group]:
return (db.query(models.Group).join(models.StudentGroup).
filter(models.StudentGroup.uid == user_id).all())
filter(models.StudentGroup.uid == user_id).all())


async def create_group(db: Session, group: schemas.GroupCreate) -> models.Group:
Expand Down

0 comments on commit f36e143

Please sign in to comment.