-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feat(settings): settings service and consistent naming
- Loading branch information
1 parent
86cf4a6
commit eff7831
Showing
10 changed files
with
223 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
166 changes: 0 additions & 166 deletions
166
fai-rag-app/fai-backend/fai_backend/config_v2/routes.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from typing import Dict | ||
|
||
from pydantic import BaseModel | ||
|
||
SettingsDict = Dict[str, bool | float | int | str] # Note: order of types is important! | ||
|
||
|
||
class Config(BaseModel): | ||
config: SettingsDict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
from fastapi import APIRouter, Security, Depends | ||
from pydantic import BaseModel, SecretStr | ||
|
||
from fai_backend.auth.security import check_permissions | ||
from fai_backend.dependencies import get_authenticated_user, get_project_user_permissions, \ | ||
get_page_template_for_logged_in_users | ||
from fai_backend.framework import components as c | ||
from fai_backend.logger.route_class import APIRouter as LoggingAPIRouter | ||
from fai_backend.phrase import phrase as _ | ||
from fai_backend.settings.models import SettingsDict | ||
from fai_backend.settings.service import SettingsServiceFactory | ||
|
||
router = APIRouter( | ||
prefix='/api', | ||
tags=['Settings'], | ||
route_class=LoggingAPIRouter, | ||
dependencies=[] | ||
) | ||
|
||
|
||
class GetSettingsResponseModel(BaseModel): | ||
settings: SettingsDict | ||
|
||
|
||
@router.get( | ||
'/settings', | ||
response_model=GetSettingsResponseModel, | ||
dependencies=[Security(get_authenticated_user)] | ||
) | ||
async def get_settings( | ||
permissions=Depends(get_project_user_permissions) | ||
): | ||
check_permissions(['can_edit_settings'], permissions) | ||
service = SettingsServiceFactory().get_service() | ||
settings = (await service.get_all()).model_dump() | ||
for key in settings.keys(): | ||
if isinstance(settings[key], SecretStr): | ||
secret: SecretStr = settings[key] | ||
settings[key] = secret.get_secret_value() | ||
return GetSettingsResponseModel(settings=settings) | ||
|
||
|
||
class SetSettingsRequestModel(BaseModel): | ||
settings: SettingsDict | ||
|
||
|
||
@router.post( | ||
'/settings', | ||
dependencies=[Security(get_authenticated_user)] | ||
) | ||
async def set_settings( | ||
body: SetSettingsRequestModel, | ||
permissions=Depends(get_project_user_permissions) | ||
): | ||
check_permissions(['can_edit_settings'], permissions) | ||
service = SettingsServiceFactory.get_service() | ||
await service.set_all(body.settings) | ||
|
||
|
||
@router.post( | ||
'/settings/form', | ||
response_model=list, | ||
response_model_exclude_none=True | ||
) | ||
async def set_settings_from_form( | ||
_=Depends(set_settings), | ||
view=Depends(get_page_template_for_logged_in_users) | ||
): | ||
return view( | ||
[c.Div(components=[ | ||
c.Div(components=[ | ||
c.Text(text='Settings updated!'), | ||
], class_name='card-body'), | ||
], class_name='card')], | ||
'Edit settings', | ||
) | ||
|
||
|
||
@router.get( | ||
'/view/settings', | ||
dependencies=[Security(get_authenticated_user)], | ||
response_model=list, | ||
response_model_exclude_none=True | ||
) | ||
async def edit_settings( | ||
permissions=Depends(get_project_user_permissions), | ||
view=Depends(get_page_template_for_logged_in_users), | ||
): | ||
check_permissions(['can_edit_settings'], permissions) | ||
settings = await SettingsServiceFactory.get_service().get_all() | ||
|
||
return view( | ||
[c.Div(components=[ | ||
c.Div(components=[ | ||
c.Form( | ||
submit_url='/api/settings/form', | ||
method='POST', | ||
submit_text=_('edit_settings_submit_button', 'Submit'), | ||
components=[ | ||
c.Heading(text='Basic', class_name='font-bold'), | ||
c.InputField( | ||
name='settings.FIXED_PIN', | ||
label=_('Fixed pin (leave blank to disable)'), | ||
placeholder='', | ||
required=False, | ||
html_type='text', | ||
size='sm', | ||
value=str(settings.FIXED_PIN), | ||
), | ||
|
||
c.Heading(text='AI Settings', class_name='font-bold'), | ||
c.InputField( | ||
name='settings.OPENAI_API_KEY', | ||
label=_('OpenAI API Key'), | ||
placeholder='', | ||
required=False, | ||
html_type='text', | ||
size='sm', | ||
value=settings.OPENAI_API_KEY.get_secret_value(), | ||
), | ||
c.Textarea( | ||
name='settings.VLLM_CONFIG', | ||
label=_('VLLM Config'), | ||
placeholder='', | ||
required=False, | ||
size='sm', | ||
value=settings.VLLM_CONFIG, | ||
), | ||
|
||
c.Heading(text='E-mail (Brevo)', class_name='font-bold'), | ||
c.InputField( | ||
name='settings.BREVO_API_URL', | ||
label=_('Brevo API URL'), | ||
placeholder='', | ||
required=False, | ||
html_type='text', | ||
size='sm', | ||
value=settings.BREVO_API_URL, | ||
), | ||
c.InputField( | ||
name='settings.BREVO_API_KEY', | ||
label=_('Brevo API Key'), | ||
placeholder=_(''), | ||
required=False, | ||
html_type='text', | ||
size='sm', | ||
value=settings.BREVO_API_KEY.get_secret_value(), | ||
) | ||
] | ||
) | ||
], class_name='card-body') | ||
], class_name='card') | ||
], | ||
_('Edit settings') | ||
) |
Oops, something went wrong.