Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /ParisNeo_lollms /backend /routers /discussion_groups.py
| from typing import List | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.orm import Session | |
| from backend.db import get_db | |
| from backend.db.models.user import User as DBUser | |
| from backend.db.models.discussion_group import DiscussionGroup as DBDiscussionGroup | |
| from backend.models import UserAuthDetails | |
| from backend.models.discussion import ( | |
| DiscussionGroupCreate, | |
| DiscussionGroupPublic, | |
| DiscussionGroupUpdate, | |
| ) | |
| from backend.session import get_current_active_user, get_current_db_user_from_token | |
| discussion_groups_router = APIRouter( | |
| prefix="/api/discussion-groups", | |
| tags=["Discussion Groups"], | |
| dependencies=[Depends(get_current_active_user)] | |
| ) | |
| def check_for_circular_dependency(db: Session, group_id: str, new_parent_id: str, owner_id: int): | |
| """ | |
| Checks if setting new_parent_id as the parent of group_id would create a circular dependency. | |
| """ | |
| if not new_parent_id: | |
| return # Moving to top-level, no cycle possible | |
| path = {group_id} | |
| current_id = new_parent_id | |
| while current_id: | |
| if current_id in path: | |
| raise HTTPException(status_code=400, detail="Circular dependency detected. A group cannot be a descendant of itself.") | |
| path.add(current_id) | |
| parent = db.query(DBDiscussionGroup.parent_id).filter( | |
| DBDiscussionGroup.id == current_id, | |
| DBDiscussionGroup.owner_user_id == owner_id | |
| ).scalar() | |
| current_id = parent | |
| def get_user_discussion_groups( | |
| db: Session = Depends(get_db), | |
| current_user: DBUser = Depends(get_current_db_user_from_token) | |
| ): | |
| return db.query(DBDiscussionGroup).filter(DBDiscussionGroup.owner_user_id == current_user.id).order_by(DBDiscussionGroup.name).all() | |
| def create_discussion_group( | |
| group_data: DiscussionGroupCreate, | |
| db: Session = Depends(get_db), | |
| current_user: DBUser = Depends(get_current_db_user_from_token) | |
| ): | |
| new_group = DBDiscussionGroup( | |
| name=group_data.name, | |
| owner_user_id=current_user.id, | |
| parent_id=group_data.parent_id | |
| ) | |
| db.add(new_group) | |
| db.commit() | |
| db.refresh(new_group) | |
| return new_group | |
| def update_discussion_group( | |
| group_id: str, | |
| group_data: DiscussionGroupUpdate, | |
| db: Session = Depends(get_db), | |
| current_user: DBUser = Depends(get_current_db_user_from_token) | |
| ): | |
| group = db.query(DBDiscussionGroup).filter( | |
| DBDiscussionGroup.id == group_id, | |
| DBDiscussionGroup.owner_user_id == current_user.id | |
| ).first() | |
| if not group: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Group not found or you don't have permission to edit it.") | |
| if group_data.parent_id == group_id: | |
| raise HTTPException(status_code=400, detail="A group cannot be its own parent.") | |
| # New check for circular dependencies | |
| if group_data.parent_id: | |
| check_for_circular_dependency(db, group_id, group_data.parent_id, current_user.id) | |
| group.name = group_data.name | |
| group.parent_id = group_data.parent_id | |
| db.commit() | |
| db.refresh(group) | |
| return group | |
| def delete_discussion_group( | |
| group_id: str, | |
| db: Session = Depends(get_db), | |
| current_user: DBUser = Depends(get_current_db_user_from_token) | |
| ): | |
| group = db.query(DBDiscussionGroup).filter( | |
| DBDiscussionGroup.id == group_id, | |
| DBDiscussionGroup.owner_user_id == current_user.id | |
| ).first() | |
| if not group: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Group not found or you don't have permission to delete it.") | |
| # Re-parent any child groups to null (making them top-level) before deleting | |
| child_groups = db.query(DBDiscussionGroup).filter(DBDiscussionGroup.parent_id == group_id).all() | |
| for child in child_groups: | |
| child.parent_id = None | |
| db.delete(group) | |
| db.commit() |
Xet Storage Details
- Size:
- 4.25 kB
- Xet hash:
- 205fbcc4214029dd41aa9f2754b5f092faac9e1f1a880353feefa2408e759724
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.