-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Grupper #90
Open
Trobbi
wants to merge
6
commits into
main
Choose a base branch
from
nollningsgrupper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Grupper #90
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f835ff2
beginning of mentorgroups
Trobbi 69820e6
en del gjort med nollningsgrupper
Trobbi 14c77c5
Merge branch 'main' into nollningsgrupper
Trobbi c56a029
kinda working groups
Trobbi 7839756
small fixes kinda
Trobbi 52246d8
Merge branch 'main' into nollningsgrupper
Trobbi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,29 @@ | ||
from typing import Annotated | ||
from pydantic import StringConstraints | ||
from api_schemas.base_schema import BaseSchema | ||
from api_schemas.user_schemas import UserInGroupRead | ||
from helpers.constants import MAX_GROUP_NAME | ||
from helpers.types import GROUP_TYPE, GROUP_USER_TYPE | ||
|
||
|
||
class GroupCreate(BaseSchema): | ||
name: Annotated[str, StringConstraints(max_length=MAX_GROUP_NAME)] | ||
group_type: GROUP_TYPE | ||
|
||
|
||
class GroupUserRead(BaseSchema): | ||
user: UserInGroupRead | ||
group_user_type: str | ||
|
||
|
||
class GroupRead(BaseSchema): | ||
id: int | ||
name: str | ||
group_type: str | ||
group_users: list[GroupUserRead] | ||
|
||
|
||
class GroupAddUser(BaseSchema): | ||
group_id: int | ||
user_id: int | ||
group_user_type: GROUP_USER_TYPE |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from sqlalchemy import Enum, String | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
from helpers.constants import MAX_GROUP_NAME, MAX_GROUP_TYPE_NAME | ||
from .base_model import BaseModel_DB | ||
from sqlalchemy.orm import relationship, Mapped, mapped_column | ||
from sqlalchemy.ext.associationproxy import association_proxy, AssociationProxy | ||
|
||
if TYPE_CHECKING: | ||
from .user_model import User_DB | ||
from .group_user_model import GroupUser_DB | ||
|
||
|
||
class Group_DB(BaseModel_DB): | ||
__tablename__ = "group_table" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True, init=False) | ||
|
||
name: Mapped[str] = mapped_column(String(MAX_GROUP_NAME)) | ||
|
||
group_type: Mapped[Optional[str]] = mapped_column(String(MAX_GROUP_TYPE_NAME), default=None) | ||
|
||
group_users: Mapped[list["GroupUser_DB"]] = relationship( | ||
back_populates="group", cascade="all, delete-orphan", init=False | ||
) | ||
|
||
users: AssociationProxy[list["User_DB"]] = association_proxy( | ||
target_collection="group_users", attr="user", init=False | ||
) |
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,24 @@ | ||
from typing import TYPE_CHECKING | ||
from sqlalchemy import ForeignKey, String | ||
|
||
from db_models.group_model import Group_DB | ||
from helpers.constants import MAX_GROUP_USER_TYPE_NAME | ||
from helpers.types import GROUP_USER_TYPE | ||
from .base_model import BaseModel_DB | ||
from sqlalchemy.orm import relationship, Mapped, mapped_column | ||
|
||
|
||
if TYPE_CHECKING: | ||
from user_model import User_DB | ||
|
||
|
||
class GroupUser_DB(BaseModel_DB): | ||
__tablename__ = "group_user_table" | ||
|
||
user: Mapped["User_DB"] = relationship(back_populates="group_users") | ||
user_id: Mapped[int] = mapped_column(ForeignKey("user_table.id"), primary_key=True) | ||
|
||
group: Mapped["Group_DB"] = relationship(back_populates="group_users") | ||
group_id: Mapped[int] = mapped_column(ForeignKey("group_table.id"), primary_key=True) | ||
|
||
group_user_type: Mapped[GROUP_USER_TYPE] = mapped_column(default="Default") |
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
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,32 @@ | ||
from fastapi import APIRouter | ||
from api_schemas.group_schema import GroupAddUser, GroupCreate, GroupRead | ||
from database import DB_dependency | ||
from helpers.types import GROUP_TYPE, GROUP_USER_TYPE | ||
from user.permission import Permission | ||
from services.group_service import add_to_group, get_group, post_group, get_all_groups | ||
from typing import TYPE_CHECKING, Annotated, Literal | ||
|
||
if TYPE_CHECKING: | ||
from db_models.user_model import User_DB | ||
|
||
group_router = APIRouter() | ||
|
||
|
||
@group_router.post("/", dependencies=[Permission.require("manage", "Groups")]) | ||
def upload_mentor_group(db: DB_dependency, data: GroupCreate): | ||
return post_group(db, data) | ||
|
||
|
||
@group_router.get("/", dependencies=[Permission.require("view", "Groups")], response_model=list[GroupRead]) | ||
def get_groups(db: DB_dependency, group_type: GROUP_TYPE | None = None): | ||
return get_all_groups(db, group_type) | ||
|
||
|
||
@group_router.get("/{id}", dependencies=[Permission.require("view", "Groups")], response_model=GroupRead) | ||
def get_single_group(db: DB_dependency, id: int): | ||
return get_group(db, id) | ||
|
||
|
||
@group_router.post("{id}", dependencies=[Permission.require("manage", "Groups")]) | ||
def add_user_to_mentor_group(db: DB_dependency, data: GroupAddUser): | ||
return add_to_group(db, data) |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from fastapi import HTTPException | ||
from sqlalchemy.orm import Session | ||
from api_schemas.group_schema import GroupAddUser, GroupCreate | ||
from db_models.group_model import Group_DB | ||
from db_models.group_user_model import GroupUser_DB | ||
from db_models.user_model import User_DB | ||
from sqlalchemy.exc import DataError | ||
|
||
from helpers.types import GROUP_USER_TYPE | ||
|
||
|
||
def post_group(db: Session, data: GroupCreate): | ||
group = Group_DB(name=data.name, group_type=data.group_type) | ||
|
||
db.add(group) | ||
|
||
try: | ||
db.commit() | ||
except DataError: | ||
db.rollback() | ||
raise HTTPException(400, "Group type not allowed") | ||
|
||
return {"message": "group created successfully"} | ||
|
||
|
||
def get_all_groups(db: Session, group_type: str | None): | ||
if not group_type: | ||
groups = db.query(Group_DB).all() | ||
else: | ||
groups = db.query(Group_DB).filter(Group_DB.group_type == group_type).all() | ||
|
||
return groups | ||
|
||
|
||
def get_group(db: Session, id: int): | ||
group = db.query(Group_DB).filter(Group_DB.id == id).one_or_none() | ||
if group == None: | ||
raise HTTPException(404, detail="Group not found") | ||
|
||
return group | ||
|
||
|
||
def add_to_group(db: Session, data: GroupAddUser): | ||
group1 = db.query(Group_DB).filter(Group_DB.id == data.group_id).one_or_none() | ||
|
||
if group1 == None: | ||
raise HTTPException(404, detail="Group not found") | ||
|
||
user1 = db.query(User_DB).filter(User_DB.id == data.user_id).one_or_none() | ||
|
||
if user1 == None: | ||
raise HTTPException(404, detail="User not found") | ||
|
||
group_user = GroupUser_DB( | ||
user=user1, user_id=user1.id, group=group1, group_id=group1.id, group_user_type=data.group_user_type | ||
) | ||
|
||
db.add(group_user) | ||
try: | ||
db.commit() | ||
except DataError: | ||
db.rollback() | ||
raise HTTPException(404, detail="Invalid group_user type") | ||
|
||
return {"message": f"{user1.first_name} {user1.last_name} added successfully to group {group1.name}"} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also not sure about
fastapi_users_schemas.BaseUser[int]
here, is this something we always want?