Spaces:
Runtime error
Runtime error
Last push before leaving
Browse filesFiles changed (2)
- src/crud/__init__.py+9-13
- src/crud/users.py+68-4
6 unmodified lines7891011121314159 unmodified lines25262728293013 unmodified lines44454647484950515253541 unmodified line565758596061626364656667686 unmodified lineskeeping the router imports clean."""# Import utility functions firstfrom .utils import hash_password, verify_passwordfrom .students import (create_student,get_all_students,9 unmodified linesupdate_user_tag_id,get_user_by_id,delete_user,)from .devices import (get_device_by_id_str,13 unmodified linesdelete_pending_link,)# Export all functions (optional, for explicit imports)__all__ = [# Utilities'hash_password','verify_password',# Students'create_student','get_all_students',1 unmodified line'get_student_by_tag_id','update_student_tag_id','delete_student',# Users'create_user','get_user_by_username','get_user_by_tag_id','update_user_tag_id','get_user_by_id','delete_user',# Devices'get_device_by_id_str','get_device_by_api_key',6 unmodified lines7891011129 unmodified lines2223242526272813 unmodified lines424344454647484950515253545556571 unmodified line5960616263646 unmodified lineskeeping the router imports clean."""from .students import (create_student,get_all_students,9 unmodified linesupdate_user_tag_id,get_user_by_id,delete_user,hash_password, # Import hash_password from users module)from .devices import (get_device_by_id_str,13 unmodified linesdelete_pending_link,)# Export all functions__all__ = [# Users'create_user','get_user_by_username','get_user_by_tag_id','update_user_tag_id','get_user_by_id','delete_user','hash_password',# Students'create_student','get_all_students',1 unmodified line'get_student_by_tag_id','update_student_tag_id','delete_student',# Devices'get_device_by_id_str','get_device_by_api_key',
2 unmodified lines3456789101113 unmodified lines2526272829303132333435366 unmodified lines434445464748495023 unmodified lines74757677787937 unmodified lines1171181191201211221232 unmodified lines"""from sqlalchemy.orm import Sessionfrom fastapi import HTTPException, statusfrom src import modelsfrom src.crud import hash_password # Import from crud module instead of authdef get_user_by_username(db: Session, username: str) -> models.User | None:"""13 unmodified lines"""return db.query(models.User).offset(skip).limit(limit).all()def get_user_by_tag_id(db:Session, tag_id:int) -> models.User | None:"""Retrieves a user by their TAG_ID."""return db.query(models.User).filter(models.User.tag_id == tag_id).first()def create_user(db: Session, user_data: models.UserCreate) -> models.User:"""Creates a new user account.6 unmodified linesdetail=f"Username '{user_data.username}' is already registered.")# Hash the passwordhashed_password = hash_password(user_data.password) # Use the imported function# Create new userdb_user = models.User(23 unmodified linesdetail=f"User with ID {user_id} not found.")# Update fieldsdb_user.username = user_update.usernamedb_user.name = user_update.name37 unmodified lines# Handle dependencies: set foreign keys to NULL where a user is referenceddb.query(models.ClearanceStatus).filter(models.ClearanceStatus.cleared_by == user_to_delete.id).update({"cleared_by": None})db.query(models.PendingTagLink).filter(models.PendingTagLink.initiated_by_user_id == user_to_delete.id).delete()db.delete(user_to_delete)db.commit()2 unmodified lines345678910111213141513 unmodified lines2930313233343536373839404142434445464748495051525354555657585960616263646566676 unmodified lines747576777879808182838485868788899023 unmodified lines11411511611711811912012112212312412512612712812913013113213313413513613737 unmodified lines1751761771781791801811821831841851861872 unmodified lines"""from sqlalchemy.orm import Sessionfrom fastapi import HTTPException, statusimport bcryptfrom src import modelsdef hash_password(password: str) -> str:"""Hashes a password using bcrypt."""return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')def get_user_by_username(db: Session, username: str) -> models.User | None:"""13 unmodified lines"""return db.query(models.User).offset(skip).limit(limit).all()def get_user_by_tag_id(db: Session, tag_id: str) -> models.User | None:"""Retrieves a user by their TAG_ID."""return db.query(models.User).filter(models.User.tag_id == tag_id).first()def update_user_tag_id(db: Session, username: str, new_tag_id: str) -> models.User:"""Updates a user's tag_id."""# Check if the new tag_id is already in useexisting_user_with_tag = get_user_by_tag_id(db, new_tag_id)if existing_user_with_tag:raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,detail=f"Tag ID '{new_tag_id}' is already assigned to another user.")# Get the user to updateuser = get_user_by_username(db, username)if not user:raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f"User '{username}' not found.")# Update the tag_iduser.tag_id = new_tag_iddb.commit()db.refresh(user)return userdef create_user(db: Session, user_data: models.UserCreate) -> models.User:"""Creates a new user account.6 unmodified linesdetail=f"Username '{user_data.username}' is already registered.")# Check if tag_id is already in use (if provided)if user_data.tag_id:existing_tag_user = get_user_by_tag_id(db, user_data.tag_id)if existing_tag_user:raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,detail=f"Tag ID '{user_data.tag_id}' is already assigned to another user.")# Hash the passwordhashed_password = hash_password(user_data.password)# Create new userdb_user = models.User(23 unmodified linesdetail=f"User with ID {user_id} not found.")# Check if new username is already taken (if changed)if user_update.username != db_user.username:existing_user = get_user_by_username(db, user_update.username)if existing_user:raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,detail=f"Username '{user_update.username}' is already taken.")# Check if new tag_id is already in use (if changed and provided)if user_update.tag_id and user_update.tag_id != db_user.tag_id:existing_tag_user = get_user_by_tag_id(db, user_update.tag_id)if existing_tag_user:raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,detail=f"Tag ID '{user_update.tag_id}' is already assigned to another user.")# Update fieldsdb_user.username = user_update.usernamedb_user.name = user_update.name37 unmodified lines# Handle dependencies: set foreign keys to NULL where a user is referenceddb.query(models.ClearanceStatus).filter(models.ClearanceStatus.cleared_by == user_to_delete.id).update({"cleared_by": None})# Check if PendingTagLink model exists before trying to deletetry:db.query(models.PendingTagLink).filter(models.PendingTagLink.initiated_by_user_id == user_to_delete.id).delete()except AttributeError:# PendingTagLink model doesn't exist, skip this cleanuppassdb.delete(user_to_delete)db.commit()
src/crud/__init__.py+9-13
src/crud/users.py+68-4