Source code for luna_configurator.app.handlers.dump_handler
# -*- coding: utf-8 -*-
"""Dump Handler
Module realize dump handler.
"""
from datetime import datetime
import ujson
from sanic.response import HTTPResponse
from app.handlers.base_handler import BaseRequestHandler
[docs]class DumpHandler(BaseRequestHandler):
"""
Handler to operate with dump-file.
"""
[docs] async def get(self) -> HTTPResponse:
"""
Get json file with custom limitations and settings. See `spec_getDumpFile`_.
.. _spec_getDumpFile:
_static/api.html#operation/getDumpFile
Resource is reached by address '/dump'
"""
deletionNames = ("id", "create_time", "is_default", "last_update_time")
limitations = sorted(
(await self.dbContext.getLimitations(returnGroupNames=True)), key=lambda column: column["limitation_name"]
)
limitationNames = [limitation["limitation_name"] for limitation in limitations]
settingsList = (await self.dbContext.getSettingsByFilters(limitationNames, pageSize=-1))[0]
for setting in settingsList:
for name in deletionNames:
if name in setting:
del setting[name]
settings = sorted(settingsList, key=lambda column: (column["name"], column["tags"]))
groups = await self.dbContext.getGroups()
currentDateTime = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
version = await self.dbContext.getRevision(component="platform")
outputJson = ujson.dumps(
{"limitations": limitations, "settings": settings, "groups": groups, "version": version},
ensure_ascii=False,
indent=4,
)
return self.success(
extraHeaders={"Content-Disposition": f"attachment; filename=luna-configurator-dump-{currentDateTime}.json"},
contentType="application/json; charset=utf-8",
body=outputJson,
)