
- Replace authentication system with automatic default user creation - Update all API endpoints to work without authentication - Modify user endpoints to work with default user - Update README.md to reflect the open access model - Fix linting issues and ensure code quality
128 lines
3.4 KiB
Python
128 lines
3.4 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api import deps
|
|
from app.models.user import User
|
|
from app.schemas.user import User as UserSchema, UserUpdate
|
|
from app.schemas.news import UserPreference as UserPreferenceSchema, UserPreferenceUpdate
|
|
from app.services.user import (
|
|
get_user_by_email,
|
|
get_user,
|
|
get_user_preference,
|
|
update_user_preference,
|
|
update_user,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/default", response_model=UserSchema)
|
|
async def get_default_user(
|
|
db: Session = Depends(deps.get_db),
|
|
) -> Any:
|
|
"""
|
|
Get the default user for open API access.
|
|
"""
|
|
return deps.get_default_user(db)
|
|
|
|
|
|
@router.get("/me", response_model=UserSchema)
|
|
async def read_users_me(
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get current user (default user without authentication).
|
|
"""
|
|
return current_user
|
|
|
|
|
|
@router.put("/me", response_model=UserSchema)
|
|
async def update_user_me(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
user_in: UserUpdate,
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Update current user (default user without authentication).
|
|
"""
|
|
if user_in.email and user_in.email != current_user.email:
|
|
if get_user_by_email(db, user_in.email):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Email already registered",
|
|
)
|
|
|
|
user = update_user(db, current_user, user_in)
|
|
return user
|
|
|
|
|
|
@router.get("/me/preferences", response_model=UserPreferenceSchema)
|
|
async def read_user_preferences(
|
|
db: Session = Depends(deps.get_db),
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get current user's preferences (default user without authentication).
|
|
"""
|
|
preferences = get_user_preference(db, current_user.id)
|
|
if not preferences:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="User preferences not found",
|
|
)
|
|
return preferences
|
|
|
|
|
|
@router.put("/me/preferences", response_model=UserPreferenceSchema)
|
|
async def update_user_preferences(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
preferences_in: UserPreferenceUpdate,
|
|
current_user: User = Depends(deps.get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Update current user's preferences (default user without authentication).
|
|
"""
|
|
preferences = update_user_preference(
|
|
db,
|
|
current_user.id,
|
|
keywords=preferences_in.keywords,
|
|
sources=preferences_in.sources,
|
|
categories=preferences_in.categories,
|
|
countries=preferences_in.countries,
|
|
languages=preferences_in.languages,
|
|
)
|
|
return preferences
|
|
|
|
|
|
@router.get("/", response_model=List[UserSchema])
|
|
async def read_users(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Retrieve all users.
|
|
"""
|
|
users = db.query(User).offset(skip).limit(limit).all()
|
|
return users
|
|
|
|
|
|
@router.get("/{user_id}", response_model=UserSchema)
|
|
async def read_user(
|
|
user_id: int,
|
|
db: Session = Depends(deps.get_db),
|
|
) -> Any:
|
|
"""
|
|
Get a specific user by id.
|
|
"""
|
|
user = get_user(db, user_id=user_id)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="User not found",
|
|
)
|
|
return user |