2
from collections import defaultdict
3
from lib2to3.fixes.fix_input import context
4
from typing import Optional
6
from fastapi import APIRouter, Query
7
from fastapi import Request
8
from fastapi.responses import HTMLResponse
9
from pydantic import BaseModel
10
from starlette.responses import RedirectResponse
11
from starlette.websockets import WebSocket
12
from urllib3 import request
14
from app.front.apps.basic.company.company import company
15
from app.front.template_spec import templates
16
from app.front.utills import BasePermit
17
from distributed_websocket import WebSocketProxy
19
from app.front.front_config import config
22
class ExceptionResponseSchema(BaseModel):
26
index_router = APIRouter(
27
responses={"400": {"model": ExceptionResponseSchema}},
31
@index_router.get("/front/topbar_vertical", response_class=HTMLResponse)
32
async def topbar(request: Request):
33
return templates.TemplateResponse(request, 'partials/topbar_vertical.html', context={})
36
@index_router.get("/front/topbar_horizontal", response_class=HTMLResponse)
37
async def topbar(request: Request):
38
return templates.TemplateResponse(request, 'partials/topbar_horizontal.html', context={})
41
@index_router.get("/front/theme_editor", response_class=HTMLResponse)
42
async def topbar(request: Request):
43
return templates.TemplateResponse(request, 'partials/theme_editor.html', context={})
46
@index_router.get("/front/search_bar", response_class=HTMLResponse)
47
async def topbar(request: Request):
48
return templates.TemplateResponse(request, 'partials/search_bar.html', context={})
51
@index_router.get("/front/sidebar_vertical", response_class=HTMLResponse)
52
async def sidebar(request: Request):
53
"""Строит левое меню Админки, на базе tags в роутах и пермишенах Пользователя"""
55
env = request.scope['env']
56
for domain_name, domain in env.domains.items():
58
for model_name, model in domain.models.items():
60
model_name: f'/{domain_name}/{model_name}',
63
domain_name: model_list
65
return templates.TemplateResponse(request, 'partials/sidebar_vertical.html',
66
context={'nav': admin_nav})
69
@index_router.get("/front/sidebar_horizontal", response_class=HTMLResponse)
70
async def sidebar(request: Request):
71
"""Строит левое меню Админки, на базе tags в роутах и пермишенах Пользователя"""
72
return templates.TemplateResponse(request, 'partials/sidebar_horizontal.html')
75
@index_router.get("/front/sidebar", response_class=HTMLResponse)
76
async def sidebar(request: Request):
77
"""Строит левое меню Админки, на базе tags в роутах и пермишенах Пользователя"""
78
return templates.TemplateResponse(request, 'partials/sidebar.html')
81
@index_router.get("/front/sidebar_scroll", response_class=HTMLResponse)
82
async def sidebar(request: Request):
83
"""Строит левое меню Админки, на базе tags в роутах и пермишенах Пользователя"""
84
return templates.TemplateResponse(request, 'partials/sidebar_scroll.html')
87
@index_router.get("/front/search_bar", response_class=HTMLResponse)
88
async def sidebar(request: Request):
89
"""Строит левое меню Админки, на базе tags в роутах и пермишенах Пользователя"""
90
return templates.TemplateResponse(request, 'partials/search_bar.html')
93
@index_router.get("/front/theme_editor", response_class=HTMLResponse)
94
async def sidebar(request: Request):
95
"""Строит левое меню Админки, на базе tags в роутах и пермишенах Пользователя"""
96
return templates.TemplateResponse(request, 'partials/theme_editor.html')
99
@index_router.get("/", response_class=HTMLResponse)
100
async def root_page(request: Request):
101
return templates.TemplateResponse(request, 'index-full.html')
104
async def get_user_companies(request: Request) -> dict:
105
async with request.scope['env']['company'].adapter as a:
106
res = await a.list(params={'id__in': request.user.company_ids})
107
companies = {i['id']: i for i in res['data']}
108
company = companies.get(request.user.company_id.__str__())
109
companies.pop(request.user.company_id.__str__())
111
'companies': companies,
116
@index_router.get("/front/company_changer_widget", response_class=HTMLResponse)
117
async def company_changer_widget(request: Request) -> dict:
118
data = await get_user_companies(request)
119
return templates.TemplateResponse(request, 'widgets/widget_company_changer.html', context=data)
122
class CompanyId(BaseModel):
123
company_id: uuid.UUID
126
@index_router.post("/front/company_changer_widget", response_class=HTMLResponse)
127
async def company_changer_widget(request: Request, schema: CompanyId) -> dict:
128
async with request.scope['env']['user'].adapter as a:
129
data = await a.user_company_change(user_id=request.user.user_id,
130
company_id=schema.company_id)
131
data = await get_user_companies(request)
132
return templates.TemplateResponse(request, 'widgets/widget_company_changer.html', context=data)
145
@index_router.get("/front/widget_locale_changer", response_class=HTMLResponse)
146
async def company_changer_widget(request: Request, locale: str=None) -> dict:
148
'current_locale': locale or request.cookies.get('locale') or request.user.locale,
155
if locale and locale != request.user.locale:
156
async with request.scope['env']['user'].adapter as a:
157
await a.user_locale_change(user_id=request.user.user_id, locale=locale)
158
return templates.TemplateResponse(
160
'widgets/widget_locale_changer.html',
165
@index_router.get("/front/widget_user_profile", response_class=HTMLResponse)
166
async def widget_user_profile(request: Request) -> dict:
167
return templates.TemplateResponse(
169
'widgets/widget_user_profile.html'