mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Allow to use regular values as the same with another sequences in filters
This commit is contained in:
parent
d37a7f0a0d
commit
b144332287
9 changed files with 53 additions and 23 deletions
|
|
@ -2,7 +2,9 @@ from __future__ import annotations
|
|||
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Match, Optional, Pattern, Union
|
||||
from typing import Any, Dict, Match, Optional, Pattern, Sequence, Union, cast
|
||||
|
||||
from pydantic import validator
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.api.types import Message
|
||||
|
|
@ -12,11 +14,19 @@ CommandPatterType = Union[str, re.Pattern] # type: ignore
|
|||
|
||||
|
||||
class Command(BaseFilter):
|
||||
commands: List[CommandPatterType]
|
||||
commands: Union[Sequence[CommandPatterType], CommandPatterType]
|
||||
commands_prefix: str = "/"
|
||||
commands_ignore_case: bool = False
|
||||
commands_ignore_mention: bool = False
|
||||
|
||||
@validator("commands", always=True)
|
||||
def _validate_commands(
|
||||
cls, value: Union[Sequence[CommandPatterType], CommandPatterType]
|
||||
) -> Sequence[CommandPatterType]:
|
||||
if isinstance(value, (str, re.Pattern)):
|
||||
value = [value]
|
||||
return value
|
||||
|
||||
async def __call__(self, message: Message, bot: Bot) -> Union[bool, Dict[str, Any]]:
|
||||
if not message.text:
|
||||
return False
|
||||
|
|
@ -50,7 +60,7 @@ class Command(BaseFilter):
|
|||
return False
|
||||
|
||||
# Validate command
|
||||
for allowed_command in self.commands:
|
||||
for allowed_command in cast(Sequence[CommandPatterType], self.commands):
|
||||
# Command can be presented as regexp pattern or raw string
|
||||
# then need to validate that in different ways
|
||||
if isinstance(allowed_command, Pattern): # Regexp
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Any, Dict, List, Optional, Union
|
||||
from typing import Any, Dict, Optional, Sequence, Union
|
||||
|
||||
from pydantic import validator
|
||||
|
||||
|
|
@ -8,12 +8,16 @@ from .base import BaseFilter
|
|||
|
||||
|
||||
class ContentTypesFilter(BaseFilter):
|
||||
content_types: Optional[List[str]] = None
|
||||
content_types: Optional[Union[Sequence[str], str]] = None
|
||||
|
||||
@validator("content_types")
|
||||
def _validate_content_types(cls, value: Optional[List[str]]) -> Optional[List[str]]:
|
||||
def _validate_content_types(
|
||||
cls, value: Optional[Union[Sequence[str], str]]
|
||||
) -> Optional[Sequence[str]]:
|
||||
if not value:
|
||||
value = [ContentType.TEXT]
|
||||
if isinstance(value, str):
|
||||
value = [value]
|
||||
allowed_content_types = set(ContentType.all())
|
||||
bad_content_types = set(value) - allowed_content_types
|
||||
if bad_content_types:
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
from typing import Any, Dict, List, Optional, Set, Tuple, Union
|
||||
from typing import Any, Dict, Optional, Sequence, Union
|
||||
|
||||
from pydantic import root_validator
|
||||
|
||||
from aiogram.api.types import CallbackQuery, InlineQuery, Message, Poll
|
||||
from aiogram.dispatcher.filters import BaseFilter
|
||||
|
||||
TextType = str
|
||||
|
||||
|
||||
class Text(BaseFilter):
|
||||
"""
|
||||
|
|
@ -12,14 +14,14 @@ class Text(BaseFilter):
|
|||
InlineQuery or Poll question.
|
||||
"""
|
||||
|
||||
text: Optional[Union[str, List[str], Set[str], Tuple[str]]] = None
|
||||
text_contains: Optional[Union[str, List[str], Set[str], Tuple[str]]] = None
|
||||
text_startswith: Optional[Union[str, List[str], Set[str], Tuple[str]]] = None
|
||||
text_endswith: Optional[Union[str, List[str], Set[str], Tuple[str]]] = None
|
||||
text: Optional[Union[Sequence[TextType], TextType]] = None
|
||||
text_contains: Optional[Union[Sequence[TextType], TextType]] = None
|
||||
text_startswith: Optional[Union[Sequence[TextType], TextType]] = None
|
||||
text_endswith: Optional[Union[Sequence[TextType], TextType]] = None
|
||||
text_ignore_case: bool = False
|
||||
|
||||
@root_validator
|
||||
def validate_constraints(cls, values: Dict[str, Any]) -> Dict[str, Any]:
|
||||
def _validate_constraints(cls, values: Dict[str, Any]) -> Dict[str, Any]:
|
||||
# Validate that only one text filter type is presented
|
||||
used_args = set(
|
||||
key for key, value in values.items() if key != "text_ignore_case" and value is not None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue