
- 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
72 lines
1.9 KiB
Python
72 lines
1.9 KiB
Python
from typing import Generator
|
|
from fastapi import Depends
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import SessionLocal
|
|
from app.models.user import User
|
|
|
|
|
|
def get_db() -> Generator:
|
|
"""
|
|
Dependency to get a database session.
|
|
"""
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def get_default_user(db: Session = Depends(get_db)) -> User:
|
|
"""
|
|
Get a default user for API access without authentication.
|
|
Returns the first superuser in the database, or creates one if none exists.
|
|
"""
|
|
# Try to get the first user
|
|
user = db.query(User).filter(User.is_superuser).first()
|
|
|
|
# If no user exists, create a default superuser
|
|
if not user:
|
|
# Import here to avoid circular imports
|
|
from app.core.security import get_password_hash
|
|
from app.models.news import UserPreference
|
|
|
|
# Create a default superuser
|
|
user = User(
|
|
email="admin@example.com",
|
|
username="admin",
|
|
hashed_password=get_password_hash("admin"),
|
|
is_active=True,
|
|
is_superuser=True,
|
|
)
|
|
db.add(user)
|
|
db.commit()
|
|
db.refresh(user)
|
|
|
|
# Create default user preferences
|
|
user_preference = UserPreference(user_id=user.id)
|
|
db.add(user_preference)
|
|
db.commit()
|
|
|
|
return user
|
|
|
|
|
|
def get_current_user(db: Session = Depends(get_db)) -> User:
|
|
"""
|
|
Dependency to get the current user without authentication.
|
|
"""
|
|
return get_default_user(db)
|
|
|
|
|
|
def get_current_active_user(db: Session = Depends(get_db)) -> User:
|
|
"""
|
|
Dependency to get the current active user without authentication.
|
|
"""
|
|
return get_default_user(db)
|
|
|
|
|
|
def get_current_active_superuser(db: Session = Depends(get_db)) -> User:
|
|
"""
|
|
Dependency to get the current active superuser without authentication.
|
|
"""
|
|
return get_default_user(db) |