2024-08-15 00:57:30 +03:00
|
|
|
import asyncio
|
2024-08-14 01:09:41 +03:00
|
|
|
import os
|
|
|
|
|
2024-08-13 23:57:26 +03:00
|
|
|
from fastapi import FastAPI
|
2024-08-14 01:09:41 +03:00
|
|
|
from fastapi.responses import HTMLResponse
|
2024-08-15 00:57:30 +03:00
|
|
|
from fastapi.websockets import WebSocket, WebSocketDisconnect
|
2024-08-14 01:09:41 +03:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2024-08-13 23:57:26 +03:00
|
|
|
|
|
|
|
import config
|
|
|
|
|
|
|
|
|
2024-08-15 00:57:30 +03:00
|
|
|
class ConnectionManager:
|
|
|
|
connections: list[WebSocket]
|
|
|
|
|
|
|
|
class StateError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
self.connections = []
|
|
|
|
|
|
|
|
async def connect(
|
|
|
|
self,
|
|
|
|
websocket: WebSocket,
|
|
|
|
):
|
|
|
|
await websocket.accept()
|
|
|
|
self.connections.append(websocket)
|
|
|
|
|
|
|
|
def disconnect(
|
|
|
|
self,
|
|
|
|
websocket: WebSocket,
|
|
|
|
):
|
|
|
|
self.connections.remove(websocket)
|
|
|
|
|
|
|
|
async def broadcast(
|
|
|
|
self,
|
|
|
|
data: dict,
|
|
|
|
):
|
|
|
|
for connection in self.connections:
|
|
|
|
asyncio.ensure_future(connection.send_json(data))
|
|
|
|
|
|
|
|
|
|
|
|
connection_manager = ConnectionManager()
|
|
|
|
|
|
|
|
|
2024-08-17 15:29:45 +03:00
|
|
|
api = FastAPI(
|
2024-08-14 01:09:41 +03:00
|
|
|
title=config.Main.app_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
env = Environment(
|
|
|
|
loader=FileSystemLoader(
|
|
|
|
searchpath=os.path.join(
|
|
|
|
config.Main.cwd,
|
|
|
|
'templates',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
enable_async=True,
|
2024-08-13 23:57:26 +03:00
|
|
|
)
|
2024-08-14 01:09:41 +03:00
|
|
|
env.globals['app_name'] = config.Main.app_name
|
2024-08-13 23:57:26 +03:00
|
|
|
|
|
|
|
|
2024-08-17 15:29:45 +03:00
|
|
|
@api.get(
|
2024-08-13 23:57:26 +03:00
|
|
|
path='/',
|
|
|
|
)
|
|
|
|
async def _():
|
2024-08-14 01:09:41 +03:00
|
|
|
template = env.get_template('main.jinja2')
|
|
|
|
return HTMLResponse(
|
|
|
|
content=await template.render_async(
|
|
|
|
title='Дашборд',
|
|
|
|
),
|
|
|
|
)
|
2024-08-15 00:57:30 +03:00
|
|
|
|
|
|
|
|
2024-08-17 00:52:52 +03:00
|
|
|
polls = [
|
|
|
|
{
|
|
|
|
'id': 1,
|
|
|
|
'name': 'Текущее состояние сотрудников',
|
2024-08-17 15:29:45 +03:00
|
|
|
'daysOfWeek': 'ПН-ПТ',
|
2024-08-17 00:52:52 +03:00
|
|
|
'time': '11:00',
|
2024-08-17 15:29:45 +03:00
|
|
|
'questionNumber': '1 вопрос',
|
|
|
|
'isEnabled': True,
|
2024-08-17 00:52:52 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'id': 2,
|
|
|
|
'name': 'Планы на обед',
|
2024-08-17 15:29:45 +03:00
|
|
|
'daysOfWeek': 'ПН-ПТ',
|
2024-08-17 00:52:52 +03:00
|
|
|
'time': '11:45-12:00',
|
2024-08-17 15:29:45 +03:00
|
|
|
'questionNumber': '2 вопроса',
|
|
|
|
'isEnabled': False,
|
2024-08-17 00:52:52 +03:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2024-08-17 15:29:45 +03:00
|
|
|
@api.get(
|
2024-08-17 00:52:52 +03:00
|
|
|
path='/api/polls',
|
|
|
|
)
|
|
|
|
async def _():
|
|
|
|
return polls
|
|
|
|
|
|
|
|
|
2024-08-17 15:29:45 +03:00
|
|
|
@api.put(
|
2024-08-17 00:52:52 +03:00
|
|
|
path='/api/polls',
|
2024-08-15 00:57:30 +03:00
|
|
|
)
|
|
|
|
async def _(
|
2024-08-17 00:52:52 +03:00
|
|
|
poll: dict,
|
2024-08-15 00:57:30 +03:00
|
|
|
):
|
2024-08-17 15:29:45 +03:00
|
|
|
for i, p in enumerate(polls):
|
|
|
|
if p['id'] == poll['id']:
|
|
|
|
polls[i] = poll
|
2024-08-17 00:52:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
# @app.websocket(
|
|
|
|
# path='/ws/sync',
|
|
|
|
# )
|
|
|
|
# async def _(
|
|
|
|
# websocket: WebSocket,
|
|
|
|
# ):
|
|
|
|
# await connection_manager.connect(websocket)
|
|
|
|
# try:
|
|
|
|
# while True:
|
|
|
|
# await connection_manager.broadcast(await websocket.receive_json())
|
|
|
|
# except WebSocketDisconnect:
|
|
|
|
# connection_manager.disconnect(websocket)
|