Skip to content

Commit

Permalink
Better typing (still WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
f213 committed Mar 10, 2022
1 parent 85bc14e commit ff434e3
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .env.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TINKOFF_INVOICE_TOKEN=
DADATA_TOKEN=a8e63351030617182e7946799cef566d0aefa212
DADATA_SECRET=426700434498025666995ca9f3272bc819765b09

1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[flake8]
max-line-length = 240
ignore = PT001
exclude=.git,venv
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ lint:
cd src && mypy

dev:
watchmedo auto-restart --directory . --patterns '*.py' --recursive -- python -m bot.bot
watchmedo auto-restart --directory . --patterns '*.py' --recursive -- python -m src.bot
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# invoice bot

## Installtion

Python 3.10 required.

Для начала нужно получить токен тестового бота и записать его в файл `.env`, типа так:

```
$ cp .env.ci .env
$ echo TELEGRAM_TOKEN=100500:secret-from-botfather >> .env
```

```
$ pip install --upgrade pip pip-tools
$ pip-sync dev-requirements.txt requirements.txt
$ make dev
```

1 change: 0 additions & 1 deletion dev-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pytest-freezegun
pytest-mock
pytest-randomly
pytest-httpx
flake8-absolute-import
flake8-bugbear
flake8-commas
flake8-eradicate
Expand Down
3 changes: 0 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ eradicate==2.0.0
# via flake8-eradicate
flake8==4.0.1
# via
# flake8-absolute-import
# flake8-bugbear
# flake8-commas
# flake8-eradicate
Expand All @@ -45,8 +44,6 @@ flake8==4.0.1
# flake8-quotes
# flake8-simplify
# flake8-use-fstring
flake8-absolute-import==1.0.0.1
# via -r dev-requirements.in
flake8-bugbear==22.1.11
# via -r dev-requirements.in
flake8-cognitive-complexity==0.1.0
Expand Down
Empty file added src/__init__.py
Empty file.
55 changes: 31 additions & 24 deletions bot/bot.py → src/bot.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import logging
import os
from typing import TYPE_CHECKING

from telegram import ReplyKeyboardMarkup
from telegram import Update
from telegram.ext import CallbackContext
from telegram.ext import CommandHandler
from telegram.ext import ConversationHandler
from telegram.ext import Dispatcher
from telegram.ext import Filters
from telegram.ext import MessageHandler
from telegram.ext import Updater

from clients.dadata import get_by_inn
from models.item import DATABASE
from models.item import parse_input
from .clients.dadata import get_by_inn
from .models.item import DATABASE
from .models.item import parse_input

INN, CONFIRM_LEGAL_ENTITY, ADD_ITEM, CHANGE_PRICE_OR_AMOUNT_OF_LAST_ITEM = range(4)

if TYPE_CHECKING:
from .t import CallbackContext


def enable_logging() -> None:
logging.basicConfig(
Expand All @@ -25,35 +28,39 @@ def enable_logging() -> None:
)


def start(update: Update, context: CallbackContext) -> int:
def start(update: Update, context: 'CallbackContext') -> int:
update.message.reply_text('Введите ИНН')

context.user_data['invoice'] = {}
context.user_data['invoice'] = {
'legal_entity': None, # type: ignore
'items': [],
}

return INN


def inn(update: Update, context: CallbackContext) -> int | None:
def inn(update: Update, context: 'CallbackContext') -> int | None:
inn: str = update.message.text

entities = get_by_inn(inn)

match len(entities):
case 0:
update.message.reply_text('Чё-т ничего не нашлось :( Попробуйте ещё раз или напишите Феде')
return
case 1:
context.user_data['invoice']['legal_entity'] = entities[0]
update.message.reply_text(
text=f'{entities[0]}. Если ошиблись — наберите /start',
reply_markup=ReplyKeyboardMarkup([[i['user_name']] for i in DATABASE.values()]),
)
return ADD_ITEM
if len(entities) == 1:
context.user_data['invoice']['legal_entity'] = entities[0]
update.message.reply_text(
text=f'{entities[0]}. Если ошиблись — наберите /start',
reply_markup=ReplyKeyboardMarkup([[i['user_name']] for i in DATABASE.values()]), # type: ignore
)
return ADD_ITEM

if len(entities) == 0:
update.message.reply_text('Чё-т ничего не нашлось :( Попробуйте ещё раз или напишите Феде')
elif len(entities) > 2:
update.message.reply_text('WIP, пока не умеем работать с несколькими юрлицами')


def add_item(update: Update, context: CallbackContext) -> int:
def add_item(update: Update, context: 'CallbackContext') -> int:
item = parse_input(update.message.text)
context.user_data['invoice']['item'] = item
context.user_data['invoice']['items'] = [item]

legal_entity = context.user_data['invoice']['legal_entity']

Expand All @@ -65,15 +72,15 @@ def add_item(update: Update, context: CallbackContext) -> int:
return CHANGE_PRICE_OR_AMOUNT_OF_LAST_ITEM


def change_price_or_amount_of_last_item(update: Update, context: CallbackContext) -> None:
def change_price_or_amount_of_last_item(update: Update, context: 'CallbackContext') -> None:
number = int(update.message.text)

if number < 100:
context.user_data['invoice']['item'].amount = number
context.user_data['invoice']['items'][0].amount = number
else:
context.user_data['invoice']['item'].price = number
context.user_data['invoice']['items'][0].price = number

item = context.user_data['invoice']['item']
item = context.user_data['invoice']['items'][0]
legal_entity = context.user_data['invoice']['legal_entity']

update.message.reply_text(
Expand Down
2 changes: 1 addition & 1 deletion clients/dadata.py → src/clients/dadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dadata import Dadata
from dotenv import load_dotenv

from models.legal_entity import LegalEntity
from ..models.legal_entity import LegalEntity

load_dotenv()

Expand Down
4 changes: 2 additions & 2 deletions clients/tinkoff.py → src/clients/tinkoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from dotenv import load_dotenv
import httpx

from item import Item
from legal_entity import LegalEntity
from ..models.item import Item
from ..models.legal_entity import LegalEntity

load_dotenv()

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion mypy.ini → src/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
python_version = 3.10
files = .
namespace_packages = on
explicit_package_bases = on
#explicit_package_bases = on
warn_no_return = off
warn_unused_configs = on
warn_unused_ignores = on
Expand Down
31 changes: 31 additions & 0 deletions src/t.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import TypedDict

from telegram import Message
from telegram import Update
from telegram.ext import CallbackContext as _CallbackContext

from .models.item import Item
from .models.legal_entity import LegalEntity


class InvoiceData(TypedDict):
legal_entity: LegalEntity
items: list[Item]


class UserData(TypedDict):
invoice: InvoiceData


class CallbackContext(_CallbackContext[UserData, dict, dict]):
user_data: UserData


class MessageUpdate(Update):
message: Message


__all__ = [
'CallbackContext',
'MessageUpdate',
]

0 comments on commit ff434e3

Please sign in to comment.