2
from typing import Annotated, Optional
4
from fastapi import APIRouter, Form
5
from fastapi import Request
6
from fastapi.responses import HTMLResponse
7
from pydantic import BaseModel
8
from starlette.responses import Response
10
from app.front.front import ExceptionResponseSchema
11
from app.front.template_spec import templates
12
from app.front.utills import BasePermit
14
user_router = APIRouter()
17
class UserPermit(BasePermit):
18
permits = ['user_list']
21
@user_router.post("/company_change/{company_id}", response_class=HTMLResponse)
22
async def company_change(request: Request, company_id: uuid.UUID):
23
"""Смена компании юзерану """
25
async with request.scope['env']['user'].adapter as a:
26
data = await a.user_company_change(user_id=request.user.user_id, company_id=company_id)
27
message = "Company changed"
28
data = await a.dropdown_ids('company', data['company_id'], '/basic/user/company_change', message=message)
29
return templates.TemplateResponse(request, 'widgets/widgets/dropdown-ids-named-htmx.html', context=data)
32
@user_router.get("/login", responses={"404": {"model": ExceptionResponseSchema}}, )
33
async def login(request: Request, response: Response):
35
referer = request.headers.get('referer').split('next=')[-1]
38
next = request.query_params.get('next') or referer
39
return templates.TemplateResponse(request, 'basic/login-full.html',context={'next': next})
42
class LoginSchema(BaseModel):
45
next: Optional[str] = None
50
responses={"404": {"model": ExceptionResponseSchema}},
52
async def login(request: Request, schema: LoginSchema, ):
54
async with request.scope['env']['user'].adapter as a:
55
data = await a.login(schema.username, schema.password)
56
return templates.TemplateResponse(
58
'components/write_ls.html',
60
'token': data['token'],
61
'refresh_token': data['refresh_token'],
66
@user_router.get("/logout", responses={"404": {"model": ExceptionResponseSchema}}, )
67
async def logout(request: Request, response: Response):
68
async with request.scope['env']['user'].adapter as a:
69
data = await a.logout(request.user.user_id)
71
class RefreshTokenSchema(BaseModel):
76
@user_router.post("/refresh", responses={"404": {"model": ExceptionResponseSchema}}, )
77
async def refresh_token(request: Request, refresh_schema: RefreshTokenSchema):
78
async with request.scope['env']['user'].adapter as a:
79
return await a.refresh_token(refresh_schema)