Skip to content

Commit 2589811

Browse files
committed
chore: format
1 parent 4292358 commit 2589811

55 files changed

Lines changed: 638 additions & 489 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/open_webui/env.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,7 @@ def parse_section(section):
431431
# enabled, the kernel sends TCP keepalive probes on idle connections so
432432
# half-closed sockets (e.g. after a silent firewall/LB reset or a NIC
433433
# flap) are detected before the next command lands on them.
434-
REDIS_SOCKET_KEEPALIVE = (
435-
os.environ.get('REDIS_SOCKET_KEEPALIVE', 'False').lower() == 'true'
436-
)
434+
REDIS_SOCKET_KEEPALIVE = os.environ.get('REDIS_SOCKET_KEEPALIVE', 'False').lower() == 'true'
437435

438436
# How often (in seconds) redis-py should PING an idle pooled connection
439437
# before reusing it. Opt-in: defaults to unset (empty string) so behavior

backend/open_webui/internal/db.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def get_session():
214214
)
215215

216216
if DATABASE_ENABLE_SQLITE_WAL:
217+
217218
@event.listens_for(async_engine.sync_engine, 'connect')
218219
def _set_sqlite_wal(dbapi_connection, connection_record):
219220
cursor = dbapi_connection.cursor()

backend/open_webui/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ async def lifespan(app: FastAPI):
723723

724724
# Shutdown: clean up shared resources
725725
from open_webui.utils.session_pool import close_session
726+
726727
await close_session()
727728

728729
if hasattr(app.state, 'redis_task_command_listener'):
@@ -1397,7 +1398,6 @@ async def dispatch(self, request: Request, call_next):
13971398
app.add_middleware(SecurityHeadersMiddleware)
13981399

13991400

1400-
14011401
@app.middleware('http')
14021402
async def commit_session_after_request(request: Request, call_next):
14031403
response = await call_next(request)
@@ -1992,7 +1992,7 @@ async def list_tasks_endpoint(request: Request, user=Depends(get_admin_user)):
19921992
@app.get('/api/tasks/chat/{chat_id:path}')
19931993
async def list_tasks_by_chat_id_endpoint(request: Request, chat_id: str, user=Depends(get_verified_user)):
19941994
if chat_id.startswith('local:'):
1995-
socket_id = chat_id[len('local:'):]
1995+
socket_id = chat_id[len('local:') :]
19961996
owner_id = get_user_id_from_session_pool(socket_id)
19971997
if owner_id != user.id and user.role != 'admin':
19981998
return {'task_ids': []}
@@ -2010,7 +2010,7 @@ async def list_tasks_by_chat_id_endpoint(request: Request, chat_id: str, user=De
20102010
@app.post('/api/tasks/chat/{chat_id:path}/stop')
20112011
async def stop_tasks_by_chat_id_endpoint(request: Request, chat_id: str, user=Depends(get_verified_user)):
20122012
if chat_id.startswith('local:'):
2013-
socket_id = chat_id[len('local:'):]
2013+
socket_id = chat_id[len('local:') :]
20142014
owner_id = get_user_id_from_session_pool(socket_id)
20152015
if owner_id != user.id and user.role != 'admin':
20162016
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND)

backend/open_webui/models/access_grants.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ async def grant_access(
295295
async with get_async_db_context(db) as db:
296296
# Check for existing grant
297297
result = await db.execute(
298-
select(AccessGrant)
299-
.filter_by(
298+
select(AccessGrant).filter_by(
300299
resource_type=resource_type,
301300
resource_id=resource_id,
302301
principal_type=principal_type,
@@ -334,8 +333,7 @@ async def revoke_access(
334333
"""Remove a single access grant."""
335334
async with get_async_db_context(db) as db:
336335
result = await db.execute(
337-
delete(AccessGrant)
338-
.filter_by(
336+
delete(AccessGrant).filter_by(
339337
resource_type=resource_type,
340338
resource_id=resource_id,
341339
principal_type=principal_type,
@@ -355,8 +353,7 @@ async def revoke_all_access(
355353
"""Remove all access grants for a resource."""
356354
async with get_async_db_context(db) as db:
357355
result = await db.execute(
358-
delete(AccessGrant)
359-
.filter_by(
356+
delete(AccessGrant).filter_by(
360357
resource_type=resource_type,
361358
resource_id=resource_id,
362359
)
@@ -451,8 +448,7 @@ async def get_access_control(
451448
"""
452449
async with get_async_db_context(db) as db:
453450
result = await db.execute(
454-
select(AccessGrant)
455-
.filter_by(
451+
select(AccessGrant).filter_by(
456452
resource_type=resource_type,
457453
resource_id=resource_id,
458454
)
@@ -470,8 +466,7 @@ async def get_grants_by_resource(
470466
"""Get all grants for a specific resource."""
471467
async with get_async_db_context(db) as db:
472468
result = await db.execute(
473-
select(AccessGrant)
474-
.filter_by(
469+
select(AccessGrant).filter_by(
475470
resource_type=resource_type,
476471
resource_id=resource_id,
477472
)
@@ -490,8 +485,7 @@ async def get_grants_by_resources(
490485
return {}
491486
async with get_async_db_context(db) as db:
492487
result = await db.execute(
493-
select(AccessGrant)
494-
.filter(
488+
select(AccessGrant).filter(
495489
AccessGrant.resource_type == resource_type,
496490
AccessGrant.resource_id.in_(resource_ids),
497491
)
@@ -634,8 +628,7 @@ async def get_users_with_access(
634628

635629
async with get_async_db_context(db) as db:
636630
result = await db.execute(
637-
select(AccessGrant)
638-
.filter_by(
631+
select(AccessGrant).filter_by(
639632
resource_type=resource_type,
640633
resource_id=resource_id,
641634
permission=permission,

backend/open_webui/models/auths.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ async def authenticate_user(
141141
except Exception:
142142
return None
143143

144-
async def authenticate_user_by_api_key(self, api_key: str, db: Optional[AsyncSession] = None) -> Optional[UserModel]:
144+
async def authenticate_user_by_api_key(
145+
self, api_key: str, db: Optional[AsyncSession] = None
146+
) -> Optional[UserModel]:
145147
log.info(f'authenticate_user_by_api_key')
146148
# if no api_key, return None
147149
if not api_key:
@@ -159,9 +161,7 @@ async def authenticate_user_by_email(self, email: str, db: Optional[AsyncSession
159161
async with get_async_db_context(db) as db:
160162
# Single JOIN query instead of two separate queries
161163
result = await db.execute(
162-
select(Auth, User)
163-
.join(User, Auth.id == User.id)
164-
.filter(Auth.email == email, Auth.active == True)
164+
select(Auth, User).join(User, Auth.id == User.id).filter(Auth.email == email, Auth.active == True)
165165
)
166166
row = result.first()
167167
if row:

backend/open_webui/models/automations.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ async def insert(
145145

146146
async def count_by_user(self, user_id: str, db: Optional[AsyncSession] = None) -> int:
147147
async with get_async_db_context(db) as db:
148-
result = await db.execute(
149-
select(func.count()).select_from(Automation).filter_by(user_id=user_id)
150-
)
148+
result = await db.execute(select(func.count()).select_from(Automation).filter_by(user_id=user_id))
151149
return result.scalar()
152150

153151
async def get_by_id(self, id: str, db: Optional[AsyncSession] = None) -> Optional[AutomationModel]:
@@ -185,9 +183,7 @@ async def search_automations(
185183
stmt = stmt.order_by(Automation.created_at.desc())
186184

187185
# Get total count
188-
count_result = await db.execute(
189-
select(func.count()).select_from(stmt.subquery())
190-
)
186+
count_result = await db.execute(select(func.count()).select_from(stmt.subquery()))
191187
total = count_result.scalar()
192188

193189
if skip:
@@ -343,18 +339,14 @@ async def get_latest_batch(
343339
.subquery()
344340
)
345341
result = await db.execute(
346-
select(AutomationRun)
347-
.join(
342+
select(AutomationRun).join(
348343
subq,
349344
(AutomationRun.automation_id == subq.c.automation_id)
350345
& (AutomationRun.created_at == subq.c.max_created),
351346
)
352347
)
353348
rows = result.scalars().all()
354-
return {
355-
row.automation_id: AutomationRunModel.model_validate(row)
356-
for row in rows
357-
}
349+
return {row.automation_id: AutomationRunModel.model_validate(row) for row in rows}
358350

359351
async def get_by_automation(
360352
self,

backend/open_webui/models/channels.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,13 @@ async def get_channels_by_user_id(self, user_id: str, db: Optional[AsyncSession]
414414
all_channels = list(membership_channels) + list(standard_channels)
415415
channel_ids = [c.id for c in all_channels]
416416
grants_map = await AccessGrants.get_grants_by_resources('channel', channel_ids, db=db)
417-
return [await self._to_channel_model(c, access_grants=grants_map.get(c.id, []), db=db) for c in all_channels]
417+
return [
418+
await self._to_channel_model(c, access_grants=grants_map.get(c.id, []), db=db) for c in all_channels
419+
]
418420

419-
async def get_dm_channel_by_user_ids(self, user_ids: list[str], db: Optional[AsyncSession] = None) -> Optional[ChannelModel]:
421+
async def get_dm_channel_by_user_ids(
422+
self, user_ids: list[str], db: Optional[AsyncSession] = None
423+
) -> Optional[ChannelModel]:
420424
async with get_async_db_context(db) as db:
421425
# Ensure uniqueness in case a list with duplicates is passed
422426
unique_user_ids = list(set(user_ids))
@@ -462,9 +466,7 @@ async def add_members_to_channel(
462466
# 1. Collect all user_ids including groups + inviter
463467
requested_users = await self._collect_unique_user_ids(invited_by, user_ids, group_ids)
464468

465-
result = await db.execute(
466-
select(ChannelMember.user_id).filter(ChannelMember.channel_id == channel_id)
467-
)
469+
result = await db.execute(select(ChannelMember.user_id).filter(ChannelMember.channel_id == channel_id))
468470
existing_users = {row[0] for row in result.all()}
469471

470472
new_user_ids = requested_users - existing_users
@@ -512,7 +514,9 @@ async def is_user_channel_manager(self, channel_id: str, user_id: str, db: Optio
512514
membership = result.scalars().first()
513515
return membership is not None
514516

515-
async def join_channel(self, channel_id: str, user_id: str, db: Optional[AsyncSession] = None) -> Optional[ChannelMemberModel]:
517+
async def join_channel(
518+
self, channel_id: str, user_id: str, db: Optional[AsyncSession] = None
519+
) -> Optional[ChannelMemberModel]:
516520
async with get_async_db_context(db) as db:
517521
# Check if the membership already exists
518522
result = await db.execute(
@@ -581,11 +585,11 @@ async def get_member_by_channel_and_user_id(
581585
membership = result.scalars().first()
582586
return ChannelMemberModel.model_validate(membership) if membership else None
583587

584-
async def get_members_by_channel_id(self, channel_id: str, db: Optional[AsyncSession] = None) -> list[ChannelMemberModel]:
588+
async def get_members_by_channel_id(
589+
self, channel_id: str, db: Optional[AsyncSession] = None
590+
) -> list[ChannelMemberModel]:
585591
async with get_async_db_context(db) as db:
586-
result = await db.execute(
587-
select(ChannelMember).filter(ChannelMember.channel_id == channel_id)
588-
)
592+
result = await db.execute(select(ChannelMember).filter(ChannelMember.channel_id == channel_id))
589593
memberships = result.scalars().all()
590594
return [ChannelMemberModel.model_validate(membership) for membership in memberships]
591595

@@ -613,7 +617,9 @@ async def pin_channel(
613617
await db.commit()
614618
return True
615619

616-
async def update_member_last_read_at(self, channel_id: str, user_id: str, db: Optional[AsyncSession] = None) -> bool:
620+
async def update_member_last_read_at(
621+
self, channel_id: str, user_id: str, db: Optional[AsyncSession] = None
622+
) -> bool:
617623
async with get_async_db_context(db) as db:
618624
result = await db.execute(
619625
select(ChannelMember).filter(
@@ -658,11 +664,13 @@ async def update_member_active_status(
658664
async def is_user_channel_member(self, channel_id: str, user_id: str, db: Optional[AsyncSession] = None) -> bool:
659665
async with get_async_db_context(db) as db:
660666
result = await db.execute(
661-
select(ChannelMember).filter(
667+
select(ChannelMember)
668+
.filter(
662669
ChannelMember.channel_id == channel_id,
663670
ChannelMember.user_id == user_id,
664671
ChannelMember.is_active.is_(True),
665-
).limit(1)
672+
)
673+
.limit(1)
666674
)
667675
membership = result.scalars().first()
668676
return membership is not None
@@ -726,11 +734,13 @@ async def get_channels_by_file_id_and_user_id(
726734
# --- Case A: group or dm => user must be an active member ---
727735
if channel.type in ['group', 'dm']:
728736
result = await db.execute(
729-
select(ChannelMember).filter(
737+
select(ChannelMember)
738+
.filter(
730739
ChannelMember.channel_id == channel.id,
731740
ChannelMember.user_id == user_id,
732741
ChannelMember.is_active.is_(True),
733-
).limit(1)
742+
)
743+
.limit(1)
734744
)
735745
membership = result.scalars().first()
736746
if membership:
@@ -774,11 +784,13 @@ async def get_channel_by_id_and_user_id(
774784
# If the channel is a group or dm, read access requires membership (active)
775785
if channel.type in ['group', 'dm']:
776786
result = await db.execute(
777-
select(ChannelMember).filter(
787+
select(ChannelMember)
788+
.filter(
778789
ChannelMember.channel_id == id,
779790
ChannelMember.user_id == user_id,
780791
ChannelMember.is_active.is_(True),
781-
).limit(1)
792+
)
793+
.limit(1)
782794
)
783795
membership = result.scalars().first()
784796
if membership:
@@ -863,9 +875,7 @@ async def set_file_message_id_in_channel_by_id(
863875
) -> bool:
864876
try:
865877
async with get_async_db_context(db) as db:
866-
result = await db.execute(
867-
select(ChannelFile).filter_by(channel_id=channel_id, file_id=file_id)
868-
)
878+
result = await db.execute(select(ChannelFile).filter_by(channel_id=channel_id, file_id=file_id))
869879
channel_file = result.scalars().first()
870880
if not channel_file:
871881
return False
@@ -878,7 +888,9 @@ async def set_file_message_id_in_channel_by_id(
878888
except Exception:
879889
return False
880890

881-
async def remove_file_from_channel_by_id(self, channel_id: str, file_id: str, db: Optional[AsyncSession] = None) -> bool:
891+
async def remove_file_from_channel_by_id(
892+
self, channel_id: str, file_id: str, db: Optional[AsyncSession] = None
893+
) -> bool:
882894
try:
883895
async with get_async_db_context(db) as db:
884896
await db.execute(delete(ChannelFile).filter_by(channel_id=channel_id, file_id=file_id))
@@ -921,13 +933,17 @@ async def insert_webhook(
921933
await db.commit()
922934
return webhook
923935

924-
async def get_webhooks_by_channel_id(self, channel_id: str, db: Optional[AsyncSession] = None) -> list[ChannelWebhookModel]:
936+
async def get_webhooks_by_channel_id(
937+
self, channel_id: str, db: Optional[AsyncSession] = None
938+
) -> list[ChannelWebhookModel]:
925939
async with get_async_db_context(db) as db:
926940
result = await db.execute(select(ChannelWebhook).filter(ChannelWebhook.channel_id == channel_id))
927941
webhooks = result.scalars().all()
928942
return [ChannelWebhookModel.model_validate(w) for w in webhooks]
929943

930-
async def get_webhook_by_id(self, webhook_id: str, db: Optional[AsyncSession] = None) -> Optional[ChannelWebhookModel]:
944+
async def get_webhook_by_id(
945+
self, webhook_id: str, db: Optional[AsyncSession] = None
946+
) -> Optional[ChannelWebhookModel]:
931947
async with get_async_db_context(db) as db:
932948
result = await db.execute(select(ChannelWebhook).filter(ChannelWebhook.id == webhook_id))
933949
webhook = result.scalars().first()

0 commit comments

Comments
 (0)