Skip to content

Commit 3b8dae5

Browse files
committed
backend: fix ruff issues
1 parent d9baef0 commit 3b8dae5

File tree

5 files changed

+35
-36
lines changed

5 files changed

+35
-36
lines changed

backend/app/admin_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def get_admin_session(
5252
select(AdminSession).where(
5353
and_(
5454
AdminSession.session_token == session_token,
55-
AdminSession.is_active == True,
55+
AdminSession.is_active.is_(True),
5656
AdminSession.expires_at > datetime.now(UTC).replace(tzinfo=None),
5757
)
5858
)
@@ -77,7 +77,7 @@ async def cleanup_expired_sessions(db: AsyncSession) -> None:
7777
select(AdminSession).where(
7878
and_(
7979
AdminSession.expires_at <= datetime.now(UTC).replace(tzinfo=None),
80-
AdminSession.is_active == True,
80+
AdminSession.is_active.is_(True),
8181
)
8282
)
8383
)

backend/app/crud.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ async def get_auth_token_by_token(
411411
"""Get an auth token by its token value."""
412412
result = await db.execute(
413413
select(models.AuthToken).where(
414-
and_(models.AuthToken.token == token, models.AuthToken.is_active == True)
414+
and_(
415+
models.AuthToken.token == token,
416+
models.AuthToken.is_active.is_(True),
417+
)
415418
)
416419
)
417420
return result.scalars().first()
@@ -465,7 +468,7 @@ async def get_admin_users(db: AsyncSession) -> List[models.AdminUser]:
465468
"""Get all admin users."""
466469
result = await db.execute(
467470
select(models.AdminUser)
468-
.where(models.AdminUser.is_active == True)
471+
.where(models.AdminUser.is_active.is_(True))
469472
.order_by(models.AdminUser.added_at)
470473
)
471474
return result.scalars().all()
@@ -479,7 +482,7 @@ async def get_admin_user_by_username(
479482
select(models.AdminUser).where(
480483
and_(
481484
models.AdminUser.github_username == username,
482-
models.AdminUser.is_active == True,
485+
models.AdminUser.is_active.is_(True),
483486
)
484487
)
485488
)

backend/app/database.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from contextlib import asynccontextmanager
2+
from typing import AsyncGenerator
3+
14
import logging
25
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
36
from sqlalchemy.orm import sessionmaker
@@ -85,11 +88,6 @@ async def drop_tables():
8588
async with engine.begin() as conn:
8689
await conn.run_sync(Base.metadata.drop_all)
8790

88-
89-
from contextlib import asynccontextmanager
90-
from typing import AsyncGenerator
91-
92-
9391
@asynccontextmanager
9492
async def transaction_scope() -> AsyncGenerator[AsyncSession, None]:
9593
"""

backend/scripts/manage_tokens.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ async def list_tokens() -> None:
142142
async def deactivate_token(token_id: int) -> None:
143143
"""Deactivate an authentication token."""
144144
# Ensure database tables exist
145-
await create_tables()
145+
await db_manager.create_tables()
146146

147-
async with AsyncSessionLocal() as db:
147+
async with db_manager.AsyncSession() as db:
148148
try:
149149
success = await crud.deactivate_auth_token(db, token_id)
150150
if success:
@@ -160,9 +160,9 @@ async def deactivate_token(token_id: int) -> None:
160160

161161
async def reactivate_token(token_id: int) -> None:
162162
"""Reactivate a deactivated authentication token."""
163-
await create_tables()
163+
await db_manager.create_tables()
164164

165-
async with AsyncSessionLocal() as db:
165+
async with db_manager.AsyncSession() as db:
166166
try:
167167
result = await db.execute(
168168
select(models.AuthToken).where(models.AuthToken.id == token_id)
@@ -190,9 +190,9 @@ async def update_token_info(
190190
token_id: int, name: str = None, description: str = None
191191
) -> None:
192192
"""Update token name and/or description."""
193-
await create_tables()
193+
await db_manager.create_tables()
194194

195-
async with AsyncSessionLocal() as db:
195+
async with db_manager.AsyncSession() as db:
196196
try:
197197
result = await db.execute(
198198
select(models.AuthToken).where(models.AuthToken.id == token_id)
@@ -233,9 +233,9 @@ async def search_tokens(
233233
inactive_only: bool = False,
234234
) -> None:
235235
"""Search tokens by name or description patterns."""
236-
await create_tables()
236+
await db_manager.create_tables()
237237

238-
async with AsyncSessionLocal() as db:
238+
async with db_manager.AsyncSession() as db:
239239
try:
240240
query = select(models.AuthToken)
241241

@@ -251,9 +251,9 @@ async def search_tokens(
251251
)
252252

253253
if active_only:
254-
conditions.append(models.AuthToken.is_active == True)
254+
conditions.append(models.AuthToken.is_active.is_(True))
255255
elif inactive_only:
256-
conditions.append(models.AuthToken.is_active == False)
256+
conditions.append(models.AuthToken.is_active.is_(False))
257257

258258
if conditions:
259259
query = query.where(and_(*conditions))
@@ -308,9 +308,9 @@ async def search_tokens(
308308

309309
async def show_token_details(token_id: int) -> None:
310310
"""Show detailed information about a specific token."""
311-
await create_tables()
311+
await db_manager.create_tables()
312312

313-
async with AsyncSessionLocal() as db:
313+
async with db_manager.AsyncSession() as db:
314314
try:
315315
result = await db.execute(
316316
select(models.AuthToken).where(models.AuthToken.id == token_id)
@@ -349,17 +349,17 @@ async def show_token_details(token_id: int) -> None:
349349

350350
async def show_token_analytics() -> None:
351351
"""Show analytics and statistics about token usage."""
352-
await create_tables()
352+
await db_manager.create_tables()
353353

354-
async with AsyncSessionLocal() as db:
354+
async with db_manager.AsyncSession() as db:
355355
try:
356356
# Get basic counts
357357
total_result = await db.execute(select(func.count(models.AuthToken.id)))
358358
total_tokens = total_result.scalar()
359359

360360
active_result = await db.execute(
361361
select(func.count(models.AuthToken.id)).where(
362-
models.AuthToken.is_active == True
362+
models.AuthToken.is_active.is_(True)
363363
)
364364
)
365365
active_tokens = active_result.scalar()
@@ -456,11 +456,11 @@ async def show_token_analytics() -> None:
456456

457457
async def cleanup_old_tokens(days: int = 90, dry_run: bool = True) -> None:
458458
"""Clean up unused tokens older than specified days."""
459-
await create_tables()
459+
await db_manager.create_tables()
460460

461461
cutoff_date = datetime.utcnow() - timedelta(days=days)
462462

463-
async with AsyncSessionLocal() as db:
463+
async with db_manager.AsyncSession() as db:
464464
try:
465465
# Find tokens that are either never used and old, or inactive and old
466466
result = await db.execute(
@@ -476,7 +476,7 @@ async def cleanup_old_tokens(days: int = 90, dry_run: bool = True) -> None:
476476
result2 = await db.execute(
477477
select(models.AuthToken).where(
478478
and_(
479-
models.AuthToken.is_active == False,
479+
models.AuthToken.is_active.is_(False),
480480
models.AuthToken.created_at < cutoff_date,
481481
)
482482
)
@@ -547,13 +547,13 @@ async def export_tokens(
547547
format_type: str = "json", include_inactive: bool = True
548548
) -> None:
549549
"""Export token information (excluding actual token values)."""
550-
await create_tables()
550+
await db_manager.create_tables()
551551

552-
async with AsyncSessionLocal() as db:
552+
async with db_manager.AsyncSession() as db:
553553
try:
554554
query = select(models.AuthToken)
555555
if not include_inactive:
556-
query = query.where(models.AuthToken.is_active == True)
556+
query = query.where(models.AuthToken.is_active.is_(True))
557557

558558
query = query.order_by(models.AuthToken.created_at)
559559
result = await db.execute(query)
@@ -672,9 +672,7 @@ def main():
672672
)
673673

674674
# List tokens command
675-
list_parser = subparsers.add_parser(
676-
"list", help="List all existing authentication tokens"
677-
)
675+
subparsers.add_parser("list", help="List all existing authentication tokens")
678676

679677
# Show token details command
680678
details_parser = subparsers.add_parser(
@@ -724,7 +722,7 @@ def main():
724722
)
725723

726724
# Analytics command
727-
analytics_parser = subparsers.add_parser(
725+
subparsers.add_parser(
728726
"analytics", help="Show token usage analytics and statistics"
729727
)
730728

backend/scripts/populate_binaries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async def populate_binaries(force: bool = False, database_url: str = None):
183183
display_order=binary_data.get("display_order", 0),
184184
)
185185

186-
new_binary = await crud.create_binary(db, binary_create)
186+
await crud.create_binary(db, binary_create)
187187
print(f"✅ Created binary '{binary_id}': {binary_data['name']}")
188188
print(f" Flags: {binary_data['flags']}")
189189
print(f" Description: {binary_data['description']}")

0 commit comments

Comments
 (0)