From d4981b23dece46d3911120f13011f719a7e14855 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Oct 2025 04:24:32 +0300 Subject: [PATCH] add bots --- common_lib/utils/auth.py | 3 ++- common_lib/utils/validators.py | 21 ++++++++++++++++++++- pyproject.toml | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/common_lib/utils/auth.py b/common_lib/utils/auth.py index e98a2c2..5de047d 100644 --- a/common_lib/utils/auth.py +++ b/common_lib/utils/auth.py @@ -157,6 +157,7 @@ def validate_username(value: str, field_name: str = "login", with_httpexception=False, need_back=False, + is_bot=False, min_length=3, max_length=32, error_status_code: int = status.HTTP_401_UNAUTHORIZED, @@ -169,7 +170,7 @@ def validate_username(value: str, - no consecutive underscores - only [A-Za-z0-9_] """ - valid, result = core_validate_username(value, field_name, need_back=True, min_length=min_length, max_length=max_length) + valid, result = core_validate_username(value, field_name, need_back=True, is_bot=is_bot, min_length=min_length, max_length=max_length) if not valid: if with_httpexception: raise HTTPException(status_code=error_status_code, detail=error_detail) diff --git a/common_lib/utils/validators.py b/common_lib/utils/validators.py index 6615ec2..4646539 100644 --- a/common_lib/utils/validators.py +++ b/common_lib/utils/validators.py @@ -3,6 +3,7 @@ import re def validate_username(value: str, field_name: str = "login", need_back=False, + is_bot=False, min_length=3, max_length=32) -> str: """ @@ -38,7 +39,25 @@ def validate_username(value: str, msg = f"{field_name.capitalize()} must contain only English letters, digits, and underscores" return (False, msg) if need_back else False - return (True, value.lower()) if need_back else value.lower() + val_lower = value.lower() + + # Проверка окончания имени в зависимости от is_bot + if is_bot and not val_lower.endswith("bot"): + msg = f"{field_name.capitalize()} must end with 'bot' for bot accounts" + return (False, msg) if need_back else False + + if not is_bot and val_lower.endswith("bot"): + msg = f"{field_name.capitalize()} must not end with 'bot' for non-bot accounts" + return (False, msg) if need_back else False + + # Если заканчивается на 'bot', то перед ним должно быть >=3 символов (кроме '_') + if val_lower.endswith("bot"): + prefix = re.sub(r'_+', '', val_lower[:-3]) # убираем все подчёркивания + if len(prefix) < 3: + msg = f"{field_name.capitalize()} must contain at least 3 non-underscore characters before 'bot'" + return (False, msg) if need_back else False + + return (True, val_lower) if need_back else val_lower def validate_password(value: str, diff --git a/pyproject.toml b/pyproject.toml index 329b5e0..dcb7510 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "common-lib" -version = "0.0.34" +version = "0.0.35" description = "Библиотека общих компонентов для микросервисов yobble" authors = [{ name = "cheykrym", email = "you@example.com" }] license = "MIT"