Skip to content

Commit de3cb54

Browse files
committed
Updates to admin_users
1 parent 937c741 commit de3cb54

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/writing_assistant/admin_users.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
async def list_users():
1010
"""List all users."""
1111
from sqlalchemy import select
12+
from sqlalchemy.orm import selectinload
1213

13-
from writing_assistant.app.database import get_async_session
14+
from writing_assistant.app.database import get_session_maker
1415
from writing_assistant.app.models import User
1516

16-
async for session in get_async_session():
17+
session_maker = get_session_maker()
18+
async with session_maker() as session:
1719
result = await session.execute(
18-
select(User).order_by(User.created_at.desc())
20+
select(User)
21+
.options(selectinload(User.documents))
22+
.order_by(User.created_at.desc())
1923
)
2024
users = result.scalars().all()
2125

@@ -43,13 +47,17 @@ async def list_users():
4347
async def delete_user(email: str):
4448
"""Delete a user and all their documents."""
4549
from sqlalchemy import select
50+
from sqlalchemy.orm import selectinload
4651

47-
from writing_assistant.app.database import get_async_session
52+
from writing_assistant.app.database import get_session_maker
4853
from writing_assistant.app.models import User
4954

50-
async for session in get_async_session():
55+
session_maker = get_session_maker()
56+
async with session_maker() as session:
5157
result = await session.execute(
52-
select(User).where(User.email == email)
58+
select(User)
59+
.options(selectinload(User.documents))
60+
.where(User.email == email)
5361
)
5462
user = result.scalar_one_or_none()
5563

@@ -83,10 +91,11 @@ async def reset_password(email: str):
8391
from fastapi_users.password import PasswordHelper
8492
from sqlalchemy import select
8593

86-
from writing_assistant.app.database import get_async_session
94+
from writing_assistant.app.database import get_session_maker
8795
from writing_assistant.app.models import User
8896

89-
async for session in get_async_session():
97+
session_maker = get_session_maker()
98+
async with session_maker() as session:
9099
result = await session.execute(
91100
select(User).where(User.email == email)
92101
)
@@ -121,10 +130,11 @@ async def toggle_active(email: str):
121130
"""Toggle user active status."""
122131
from sqlalchemy import select
123132

124-
from writing_assistant.app.database import get_async_session
133+
from writing_assistant.app.database import get_session_maker
125134
from writing_assistant.app.models import User
126135

127-
async for session in get_async_session():
136+
session_maker = get_session_maker()
137+
async with session_maker() as session:
128138
result = await session.execute(
129139
select(User).where(User.email == email)
130140
)
@@ -145,10 +155,11 @@ async def make_superuser(email: str):
145155
"""Make a user a superuser."""
146156
from sqlalchemy import select
147157

148-
from writing_assistant.app.database import get_async_session
158+
from writing_assistant.app.database import get_session_maker
149159
from writing_assistant.app.models import User
150160

151-
async for session in get_async_session():
161+
session_maker = get_session_maker()
162+
async with session_maker() as session:
152163
result = await session.execute(
153164
select(User).where(User.email == email)
154165
)
@@ -168,13 +179,17 @@ async def make_superuser(email: str):
168179
async def show_user_info(email: str):
169180
"""Show detailed user information."""
170181
from sqlalchemy import select
182+
from sqlalchemy.orm import selectinload
171183

172-
from writing_assistant.app.database import get_async_session
184+
from writing_assistant.app.database import get_session_maker
173185
from writing_assistant.app.models import User
174186

175-
async for session in get_async_session():
187+
session_maker = get_session_maker()
188+
async with session_maker() as session:
176189
result = await session.execute(
177-
select(User).where(User.email == email)
190+
select(User)
191+
.options(selectinload(User.documents))
192+
.where(User.email == email)
178193
)
179194
user = result.scalar_one_or_none()
180195

@@ -230,7 +245,7 @@ def print_help():
230245

231246
async def async_main():
232247
"""Async main entry point."""
233-
from writing_assistant.app.database import create_db_and_tables
248+
from writing_assistant.app.database import create_db_and_tables, get_engine
234249

235250
# Initialize database
236251
await create_db_and_tables()
@@ -293,6 +308,10 @@ async def async_main():
293308
import traceback
294309
traceback.print_exc()
295310
sys.exit(1)
311+
finally:
312+
# Dispose of the engine to close all connections
313+
engine = get_engine()
314+
await engine.dispose()
296315

297316

298317
def main():

0 commit comments

Comments
 (0)