Lazy-import Like model in SqlLikesService
The module-level import of likes.models.like.Like caused ImportError in non-likes services that register SqlLikesService. Move the import into a lazy helper called per-method. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,10 @@ from __future__ import annotations
|
|||||||
from sqlalchemy import select, update, func
|
from sqlalchemy import select, update, func
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from likes.models.like import Like
|
|
||||||
|
def _Like():
|
||||||
|
from likes.models.like import Like
|
||||||
|
return Like
|
||||||
|
|
||||||
|
|
||||||
class SqlLikesService:
|
class SqlLikesService:
|
||||||
@@ -18,6 +21,7 @@ class SqlLikesService:
|
|||||||
user_id: int, target_type: str,
|
user_id: int, target_type: str,
|
||||||
target_slug: str | None = None, target_id: int | None = None,
|
target_slug: str | None = None, target_id: int | None = None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
Like = _Like()
|
||||||
if not user_id or not target_type:
|
if not user_id or not target_type:
|
||||||
return False
|
return False
|
||||||
filters = [
|
filters = [
|
||||||
@@ -38,6 +42,7 @@ class SqlLikesService:
|
|||||||
self, session: AsyncSession, *,
|
self, session: AsyncSession, *,
|
||||||
user_id: int, target_type: str,
|
user_id: int, target_type: str,
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
|
Like = _Like()
|
||||||
if not user_id or not target_type:
|
if not user_id or not target_type:
|
||||||
return []
|
return []
|
||||||
result = await session.execute(
|
result = await session.execute(
|
||||||
@@ -54,6 +59,7 @@ class SqlLikesService:
|
|||||||
self, session: AsyncSession, *,
|
self, session: AsyncSession, *,
|
||||||
user_id: int, target_type: str,
|
user_id: int, target_type: str,
|
||||||
) -> list[int]:
|
) -> list[int]:
|
||||||
|
Like = _Like()
|
||||||
if not user_id or not target_type:
|
if not user_id or not target_type:
|
||||||
return []
|
return []
|
||||||
result = await session.execute(
|
result = await session.execute(
|
||||||
@@ -72,6 +78,7 @@ class SqlLikesService:
|
|||||||
target_slug: str | None = None, target_id: int | None = None,
|
target_slug: str | None = None, target_id: int | None = None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Toggle a like. Returns True if now liked, False if unliked."""
|
"""Toggle a like. Returns True if now liked, False if unliked."""
|
||||||
|
Like = _Like()
|
||||||
filters = [
|
filters = [
|
||||||
Like.user_id == user_id,
|
Like.user_id == user_id,
|
||||||
Like.target_type == target_type,
|
Like.target_type == target_type,
|
||||||
|
|||||||
Reference in New Issue
Block a user