mirror of
https://git.vectorsigma.ru/public/tubearchivist.git
synced 2026-08-08 13:39:38 +00:00
add config to appsettings
This commit is contained in:
@@ -103,15 +103,19 @@ class AppConfig:
|
|||||||
|
|
||||||
return response["_source"]
|
return response["_source"]
|
||||||
|
|
||||||
def update_config(self, key, value):
|
def update_config(self, data: dict) -> AppConfigType:
|
||||||
"""update single config value"""
|
"""update single config value"""
|
||||||
key_map = key.split(".")
|
for key, value in data.items():
|
||||||
self._validate_key(key_map)
|
key_map = key.split(".")
|
||||||
self.config[key_map[0]][key_map[1]] = value
|
self._validate_key(key_map)
|
||||||
|
self.config[key_map[0]][key_map[1]] = value
|
||||||
|
|
||||||
response, status_code = ElasticWrap(self.ES_PATH).post(self.config)
|
response, status_code = ElasticWrap(self.ES_PATH).post(self.config)
|
||||||
if not status_code == 200:
|
if not status_code == 200:
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
return self.config
|
||||||
|
|
||||||
def _validate_key(self, key_map: list[str]) -> None:
|
def _validate_key(self, key_map: list[str]) -> None:
|
||||||
"""raise valueerror on invalid key"""
|
"""raise valueerror on invalid key"""
|
||||||
exists = self.CONFIG_DEFAULTS.get(key_map[0], {}).get(key_map[1]) # type: ignore # noqa: E501
|
exists = self.CONFIG_DEFAULTS.get(key_map[0], {}).get(key_map[1]) # type: ignore # noqa: E501
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ from appsettings import views
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
path(
|
||||||
|
"config/",
|
||||||
|
views.AppConfigApiView.as_view(),
|
||||||
|
name="api-config",
|
||||||
|
),
|
||||||
path(
|
path(
|
||||||
"snapshot/",
|
"snapshot/",
|
||||||
views.SnapshotApiListView.as_view(),
|
views.SnapshotApiListView.as_view(),
|
||||||
|
|||||||
@@ -11,6 +11,36 @@ from task.src.task_manager import TaskCommand
|
|||||||
from task.tasks import run_restore_backup
|
from task.tasks import run_restore_backup
|
||||||
|
|
||||||
|
|
||||||
|
class AppConfigApiView(ApiBaseView):
|
||||||
|
"""resolves to /api/appsettings/config/
|
||||||
|
GET: return app settings
|
||||||
|
POST: update app settings
|
||||||
|
"""
|
||||||
|
|
||||||
|
permission_classes = [AdminOnly]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get(request):
|
||||||
|
"""get config"""
|
||||||
|
response = AppConfig().config
|
||||||
|
return Response(response)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def post(request):
|
||||||
|
"""
|
||||||
|
update config values
|
||||||
|
data object where key is flatted CONFIG_DEFAULTS separated by '.', e.g.
|
||||||
|
{"subscriptions.channel_size": 5, "subscriptions.live_channel_size": 5}
|
||||||
|
"""
|
||||||
|
data = request.data
|
||||||
|
try:
|
||||||
|
config = AppConfig().update_config(data)
|
||||||
|
except ValueError as err:
|
||||||
|
return Response({"error": str(err)}, status=400)
|
||||||
|
|
||||||
|
return Response(config)
|
||||||
|
|
||||||
|
|
||||||
class SnapshotApiListView(ApiBaseView):
|
class SnapshotApiListView(ApiBaseView):
|
||||||
"""resolves to /api/appsettings/snapshot/
|
"""resolves to /api/appsettings/snapshot/
|
||||||
GET: returns snapshot config plus list of existing snapshots
|
GET: returns snapshot config plus list of existing snapshots
|
||||||
|
|||||||
Reference in New Issue
Block a user