async def notify_attachment(appeal_id: int, user: UserTable) -> None: appeal = await get_appeal_db(appeal_id) message = f"В обращении №{appeal_id} - «{appeal.topic}» были изменены вложения" if user.is_superuser: author = await get_or_404(UUID4(appeal.author_id)) to_addr = author.email await notify_user(to_addr, message) elif appeal.responsible_id: developer = await get_or_404(UUID4(appeal.responsible_id)) to_addr = developer.email await notify_user(to_addr, message) return None
async def notify_update_appeal(appeal_id: int, user: UserTable) -> None: appeal = await get_appeal_db(appeal_id) message = f"Обращение №{appeal_id} - «{appeal.topic}» было измененно" if user.is_superuser: author = await get_or_404(UUID4(appeal.author_id)) to_addr = author.email message += " разработчиком" await notify_user(to_addr, message) elif appeal.responsible_id: developer = await get_or_404(UUID4(appeal.responsible_id)) to_addr = developer.email message += " представителем заказчика" await notify_user(to_addr, message) return None
async def get_appeal(appeal_id: int, user: UserTable) -> Appeal: appeal = await check_access(appeal_id, user, status.HTTP_404_NOT_FOUND) client = await get_client_db(appeal.client_id) author = await get_user(UUID4(appeal.author_id)) responsible = None if appeal.responsible_id: responsible = await get_user(UUID4(appeal.responsible_id)) software = await get_software(appeal.software_id) module = await get_module(appeal.module_id) comment = await get_comments(appeal_id, user) result = Appeal(**dict({ **dict(appeal), "client": client, "author": author, "responsible": responsible, "software": software, "module": module, "comments": comment })) return result
async def get_clients(last_id: int = 0, limit: int = 9) -> List[ClientShort]: query = clients.select().where(clients.c.id > last_id).limit(limit) result = await database.fetch_all(query=query) clients_list = [] for client in result: client = dict(client) owner = await get_or_404(UUID4(client["owner_id"])) count_employees = await get_count_employees(client["id"]) clients_list.append(ClientShort(**dict({**client, "owner": owner, "count_employees": count_employees}))) return clients_list
async def get_client(client_id: int) -> Optional[Client]: query = clients.select().where(clients.c.id == client_id) client = await database.fetch_one(query=query) if client is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=Errors.CLIENT_NOT_FOUND) client = dict(client) owner = await get_or_404(UUID4(str(client["owner_id"]))) licences_list = await get_client_licences(client_id) return Client(**dict({**client, "owner": owner, "licences": licences_list}))
async def update_client_owner(client_id: int, new_owner_id: UUID4) -> Optional[UserDB]: client = await get_client_db(client_id) if client and await user_is_active(new_owner_id): new_client = ClientUpdate(owner_id=str(new_owner_id)) if client.owner_id == "undefined": client = await update_client(client_id, new_client) new_owner = await get_or_404(new_owner_id) else: update_old = EmployeeUpdate(is_owner=False) update_new = EmployeeUpdate(is_owner=True) old_owner = await pre_update_user(UUID4(client.owner_id), update_old) new_owner = await pre_update_user(new_owner_id, update_new) client = await update_client(client_id, new_client) return new_owner return None
async def endpoint_for_bots(request: Request, userid: UUID4, token: str): user = await user_db.find_one({"id": UUID4(f"{userid}".replace("-", ""))}) user_tokens = lambda: list(map(lambda x: x["token"], user["bots"])) if (user is not None) and (token in user_tokens()): sas = await request.body() decoded = json.loads(sas.decode()) telegram_bot = TeleBot(token) @telegram_bot.message_handler() def echo(m: Message): telegram_bot.send_message(m.chat.id, m.text) telegram_bot.process_new_updates([Update.de_json(decoded)]) return sas.decode() else: raise HTTPException(status_code=403, detail="This bot is not allowed here!")
auth_backends.append(jwt_authentication) all_users = FastAPIUsers( user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, ) any_user = all_users.current_user(active=True) employee = all_users.current_user(active=True, superuser=False) developer_user = all_users.current_user(active=True, superuser=True) default_uuid = UUID4("00000000-0000-0000-0000-000000000000") async def get_owner(client_id: int, user: UserTable = Depends(any_user)): if not (user.client_id == client_id and user.is_owner): raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) return user async def get_owner_with_superuser(client_id: int, user: UserTable = Depends(any_user)): if not user.is_superuser and not (user.client_id == client_id and user.is_owner): raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) return user