Refactor CRUD module imports and usage for clarity.

Replaces ambiguous shorthand references like `event` and `user` with more descriptive names such as `event_crud` and `user_crud`. Updates imports, function calls, tests, and other references across the codebase to maintain consistency. This improves code readability and reduces potential confusion.
This commit is contained in:
2025-03-15 01:22:04 +01:00
parent d5d6c4b3c9
commit fee7d8b5ec
11 changed files with 43 additions and 43 deletions

View File

@@ -9,7 +9,7 @@ from sqlalchemy.orm import Session
from app.api.dependencies.common import get_storage_provider from app.api.dependencies.common import get_storage_provider
from app.api.dependencies.auth import get_current_user from app.api.dependencies.auth import get_current_user
from app.core.database import get_db from app.core.database import get_db
from app.crud.event_theme import event_theme as event_theme_crud from app.crud.event_theme import event_theme_crud as event_theme_crud
from app.models import User from app.models import User
from app.schemas.event_themes import EventThemeCreate, EventThemeResponse, EventThemeUpdate from app.schemas.event_themes import EventThemeCreate, EventThemeResponse, EventThemeUpdate
from app.core.storage import StorageProvider from app.core.storage import StorageProvider

View File

@@ -10,7 +10,7 @@ from sqlalchemy.orm import Session
from app.api.dependencies.auth import get_current_user, get_optional_current_user from app.api.dependencies.auth import get_current_user, get_optional_current_user
from app.core.database import get_db from app.core.database import get_db
from app.crud.event import event from app.crud.event import event_crud
from app.models import Guest from app.models import Guest
from app.models.event_manager import EventManager from app.models.event_manager import EventManager
from app.models.user import User from app.models.user import User
@@ -96,13 +96,13 @@ def create_event(
) )
try: try:
# Check if slug is already taken # Check if slug is already taken
if event.get_by_slug(db, slug=event_in.slug): if event_crud.get_by_slug(db, slug=event_in.slug):
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail="An event with this slug already exists" detail="An event with this slug already exists"
) )
created_event = event.create_with_owner(db=db, obj_in=event_in, owner_id=current_user.id) created_event = event_crud.create_with_owner(db=db, obj_in=event_in, owner_id=current_user.id)
logger.info(f"Event created by {current_user.email}: {created_event.slug}") logger.info(f"Event created by {current_user.email}: {created_event.slug}")
return created_event return created_event
except SQLAlchemyError as e: except SQLAlchemyError as e:
@@ -134,12 +134,12 @@ def get_user_events(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
try: try:
total = event.count_user_events( total = event_crud.count_user_events(
db=db, db=db,
user_id=current_user.id, user_id=current_user.id,
include_inactive=include_inactive include_inactive=include_inactive
) )
items = event.get_user_events( items = event_crud.get_user_events(
db=db, db=db,
user_id=current_user.id, user_id=current_user.id,
skip=skip, skip=skip,
@@ -180,9 +180,9 @@ def get_upcoming_events(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
try: try:
items = event.get_upcoming_events(db=db, skip=skip, limit=limit) items = event_crud.get_upcoming_events(db=db, skip=skip, limit=limit)
# Count total upcoming events for pagination # Count total upcoming events for pagination
total = event.count_upcoming_events(db=db) total = event_crud.count_upcoming_events(db=db)
return { return {
"total": total, "total": total,
@@ -210,8 +210,8 @@ def get_public_events(
) -> Dict[str, Any]: ) -> Dict[str, Any]:
"""Get all public events with pagination.""" """Get all public events with pagination."""
try: try:
items = event.get_public_events(db=db, skip=skip, limit=limit) items = event_crud.get_public_events(db=db, skip=skip, limit=limit)
total = event.count_public_events(db=db) total = event_crud.count_public_events(db=db)
return { return {
"total": total, "total": total,
@@ -240,7 +240,7 @@ def get_event(
) -> EventResponse: ) -> EventResponse:
"""Get event by ID.""" """Get event by ID."""
try: try:
event_obj = event.get(db=db, id=event_id) event_obj = event_crud.get(db=db, id=event_id)
return validate_event_access( return validate_event_access(
db=db, db=db,
event_obj=event_obj, event_obj=event_obj,
@@ -269,7 +269,7 @@ def get_event_by_slug(
) -> EventResponse: ) -> EventResponse:
"""Get event by slug.""" """Get event by slug."""
try: try:
event_obj = event.get_by_slug(db=db, slug=slug) event_obj = event_crud.get_by_slug(db=db, slug=slug)
return validate_event_access( return validate_event_access(
db=db, db=db,
event_obj=event_obj, event_obj=event_obj,
@@ -304,7 +304,7 @@ def update_event(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
try: try:
event_obj = event.get(db=db, id=event_id) event_obj = event_crud.get(db=db, id=event_id)
if not event_obj: if not event_obj:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
@@ -332,14 +332,14 @@ def update_event(
# If slug is being updated, check if new slug is available and different # If slug is being updated, check if new slug is available and different
if event_in.slug and event_in.slug != event_obj.slug: if event_in.slug and event_in.slug != event_obj.slug:
existing = event.get_by_slug(db, slug=event_in.slug) existing = event_crud.get_by_slug(db, slug=event_in.slug)
if existing and existing.id != event_id: if existing and existing.id != event_id:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail="An event with this slug already exists" detail="An event with this slug already exists"
) )
return event.update(db=db, db_obj=event_obj, obj_in=event_in) return event_crud.update(db=db, db_obj=event_obj, obj_in=event_in)
except SQLAlchemyError: except SQLAlchemyError:
db.rollback() db.rollback()
raise HTTPException( raise HTTPException(
@@ -368,7 +368,7 @@ def delete_event(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
try: try:
event_obj = event.get(db=db, id=event_id) event_obj = event_crud.get(db=db, id=event_id)
if not event_obj: if not event_obj:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
@@ -389,10 +389,10 @@ def delete_event(
status_code=status.HTTP_403_FORBIDDEN, status_code=status.HTTP_403_FORBIDDEN,
detail="Only administrators can perform hard delete" detail="Only administrators can perform hard delete"
) )
event.remove(db=db, id=event_id) event_crud.remove(db=db, id=event_id)
else: else:
# Soft delete - set is_active to False # Soft delete - set is_active to False
event.update(db=db, db_obj=event_obj, obj_in={"is_active": False}) event_crud.update(db=db, db_obj=event_obj, obj_in={"is_active": False})
return None # 204 No Content return None # 204 No Content
except SQLAlchemyError: except SQLAlchemyError:

View File

@@ -170,4 +170,4 @@ class CRUDEvent(CRUDBase[Event, EventCreate, EventUpdate]):
# Create a singleton instance for use across the application # Create a singleton instance for use across the application
event = CRUDEvent(Event) event_crud = CRUDEvent(Event)

View File

@@ -22,4 +22,4 @@ class CRUDEventTheme(CRUDBase[EventTheme, EventThemeCreate, EventThemeUpdate]):
) )
event_theme = CRUDEventTheme(EventTheme) event_theme_crud = CRUDEventTheme(EventTheme)

View File

@@ -53,4 +53,4 @@ class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
# Create a singleton instance for use across the application # Create a singleton instance for use across the application
user = CRUDUser(User) user_crud = CRUDUser(User)

View File

@@ -3,7 +3,7 @@ import logging
from typing import Optional from typing import Optional
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.core.config import settings from app.core.config import settings
from app.crud.user import user as user_crud from app.crud.user import user_crud as user_crud
from app.schemas.users import UserCreate from app.schemas.users import UserCreate
from app.core.database import engine from app.core.database import engine
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@@ -15,7 +15,7 @@ from app.core.auth import (
) )
from app.models.user import User from app.models.user import User
from app.schemas.users import Token, UserCreate from app.schemas.users import Token, UserCreate
from app.crud.user import user as crud_user from app.crud.user import user_crud as crud_user
from app.core.auth import decode_token, get_token_data from app.core.auth import decode_token, get_token_data
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@@ -533,7 +533,7 @@ class TestGetEvent:
manager_user = self.create_mock_user(email="manager@example.com") manager_user = self.create_mock_user(email="manager@example.com")
mocked_event = self.create_mock_event(created_by=self.mock_user.id, managers=[manager_user], is_public=True) mocked_event = self.create_mock_event(created_by=self.mock_user.id, managers=[manager_user], is_public=True)
self.client.user = manager_user self.client.user_crud = manager_user
endpoint = self.get_event_endpoint(mocked_event) endpoint = self.get_event_endpoint(mocked_event)
response = self.client.get(endpoint) response = self.client.get(endpoint)
@@ -546,7 +546,7 @@ class TestGetEvent:
mocked_event = self.create_mock_event(created_by=self.mock_user.id, is_public=True) mocked_event = self.create_mock_event(created_by=self.mock_user.id, is_public=True)
self.client.user = superuser self.client.user_crud = superuser
endpoint = self.get_event_endpoint(mocked_event) endpoint = self.get_event_endpoint(mocked_event)
response = self.client.get(endpoint) response = self.client.get(endpoint)
@@ -571,7 +571,7 @@ class TestGetEvent:
is_public=True is_public=True
) )
self.client.user = other_user self.client.user_crud = other_user
endpoint = self.get_event_endpoint(mocked_event) endpoint = self.get_event_endpoint(mocked_event)
response = self.client.get(endpoint) response = self.client.get(endpoint)
@@ -598,7 +598,7 @@ class TestGetEvent:
self.db_session.add(guest_entry) self.db_session.add(guest_entry)
self.db_session.commit() self.db_session.commit()
self.client.user = guest_user self.client.user_crud = guest_user
endpoint = self.get_event_endpoint(mocked_event) endpoint = self.get_event_endpoint(mocked_event)
response = self.client.get(endpoint) response = self.client.get(endpoint)
@@ -616,7 +616,7 @@ class TestGetEvent:
is_public=False is_public=False
) )
self.client.user = other_user self.client.user_crud = other_user
endpoint = self.get_event_endpoint(mocked_event, access_code="123") endpoint = self.get_event_endpoint(mocked_event, access_code="123")
response = self.client.get(endpoint) response = self.client.get(endpoint)
@@ -636,7 +636,7 @@ class TestGetEvent:
user=None, user=None,
) )
self.client.user = None # Simulate no authenticated user self.client.user_crud = None # Simulate no authenticated user
endpoint = self.get_event_endpoint(mocked_event) endpoint = self.get_event_endpoint(mocked_event)
response = client.get(endpoint) response = client.get(endpoint)

View File

@@ -3,7 +3,7 @@ from datetime import datetime, timedelta, timezone
from uuid import UUID, uuid4 from uuid import UUID, uuid4
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
from app.crud.event import event as crud_event from app.crud.event import event_crud as crud_event
from app.schemas.events import EventCreate, EventUpdate from app.schemas.events import EventCreate, EventUpdate
from app.models.event import Event from app.models.event import Event

View File

@@ -3,7 +3,7 @@ from uuid import UUID
import pytest import pytest
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.crud.event_theme import event_theme from app.crud.event_theme import event_theme_crud
from app.schemas.event_themes import EventThemeCreate, EventThemeUpdate from app.schemas.event_themes import EventThemeCreate, EventThemeUpdate
@@ -23,14 +23,14 @@ def test_create_event_theme(db_session: Session) -> None:
"body": "Open Sans" "body": "Open Sans"
} }
) )
theme = event_theme.create(db=db_session, obj_in=theme_in) theme = event_theme_crud.create(db=db_session, obj_in=theme_in)
assert theme.name == theme_in.name assert theme.name == theme_in.name
assert theme.color_palette == theme_in.color_palette assert theme.color_palette == theme_in.color_palette
assert theme.fonts == theme_in.fonts assert theme.fonts == theme_in.fonts
def test_get_event_theme(db_session: Session, event_theme_fixture) -> None: def test_get_event_theme(db_session: Session, event_theme_fixture) -> None:
stored_theme = event_theme.get(db=db_session, id=event_theme_fixture.id) stored_theme = event_theme_crud.get(db=db_session, id=event_theme_fixture.id)
assert stored_theme assert stored_theme
assert stored_theme.id == event_theme_fixture.id assert stored_theme.id == event_theme_fixture.id
assert stored_theme.name == event_theme_fixture.name assert stored_theme.name == event_theme_fixture.name
@@ -39,7 +39,7 @@ def test_get_event_theme(db_session: Session, event_theme_fixture) -> None:
def test_get_event_theme_by_name(db_session: Session, event_theme_fixture) -> None: def test_get_event_theme_by_name(db_session: Session, event_theme_fixture) -> None:
stored_theme = event_theme.get_by_name(db=db_session, name=event_theme_fixture.name) stored_theme = event_theme_crud.get_by_name(db=db_session, name=event_theme_fixture.name)
assert stored_theme assert stored_theme
assert stored_theme.id == event_theme_fixture.id assert stored_theme.id == event_theme_fixture.id
assert stored_theme.name == event_theme_fixture.name assert stored_theme.name == event_theme_fixture.name
@@ -55,7 +55,7 @@ def test_update_event_theme(db_session: Session, event_theme_fixture) -> None:
"text": "#212121" "text": "#212121"
} }
) )
updated_theme = event_theme.update( updated_theme = event_theme_crud.update(
db=db_session, db=db_session,
db_obj=event_theme_fixture, db_obj=event_theme_fixture,
obj_in=theme_update obj_in=theme_update
@@ -69,11 +69,11 @@ def test_update_event_theme(db_session: Session, event_theme_fixture) -> None:
def test_delete_event_theme(db_session: Session, event_theme_fixture) -> None: def test_delete_event_theme(db_session: Session, event_theme_fixture) -> None:
theme_id = event_theme_fixture.id theme_id = event_theme_fixture.id
deleted_theme = event_theme.remove(db=db_session, id=theme_id) deleted_theme = event_theme_crud.remove(db=db_session, id=theme_id)
assert deleted_theme.id == theme_id assert deleted_theme.id == theme_id
# Verify theme no longer exists # Verify theme no longer exists
theme = event_theme.get(db=db_session, id=theme_id) theme = event_theme_crud.get(db=db_session, id=theme_id)
assert theme is None assert theme is None
@@ -83,7 +83,7 @@ def test_get_active_themes(db_session: Session, event_theme_fixture, mock_event)
db_session.commit() db_session.commit()
# Get active themes # Get active themes
active_themes = event_theme.get_active_themes(db=db_session) active_themes = event_theme_crud.get_active_themes(db=db_session)
assert len(active_themes) > 0 assert len(active_themes) > 0
assert any(theme.id == event_theme_fixture.id for theme in active_themes) assert any(theme.id == event_theme_fixture.id for theme in active_themes)
@@ -104,21 +104,21 @@ def test_get_multi_themes(db_session: Session, event_theme_fixture) -> None:
"body": "Lato" "body": "Lato"
} }
) )
event_theme.create(db=db_session, obj_in=another_theme) event_theme_crud.create(db=db_session, obj_in=another_theme)
# Test pagination # Test pagination
themes = event_theme.get_multi(db=db_session, skip=0, limit=10) themes = event_theme_crud.get_multi(db=db_session, skip=0, limit=10)
assert len(themes) >= 2 # Should have at least the fixture and the new theme assert len(themes) >= 2 # Should have at least the fixture and the new theme
def test_get_non_existent_theme(db_session: Session) -> None: def test_get_non_existent_theme(db_session: Session) -> None:
non_existent_id = UUID('00000000-0000-0000-0000-000000000000') non_existent_id = UUID('00000000-0000-0000-0000-000000000000')
theme = event_theme.get(db=db_session, id=non_existent_id) theme = event_theme_crud.get(db=db_session, id=non_existent_id)
assert theme is None assert theme is None
def test_get_non_existent_theme_by_name(db_session: Session) -> None: def test_get_non_existent_theme_by_name(db_session: Session) -> None:
non_existent_name = "Non-existent Theme" non_existent_name = "Non-existent Theme"
theme = event_theme.get_by_name(db=db_session, name=non_existent_name) theme = event_theme_crud.get_by_name(db=db_session, name=non_existent_name)
assert theme is None assert theme is None

View File

@@ -1,6 +1,6 @@
import pytest import pytest
from app.crud.user import user as user_crud from app.crud.user import user_crud as user_crud
from app.models.user import User from app.models.user import User
from app.schemas.users import UserCreate, UserUpdate from app.schemas.users import UserCreate, UserUpdate