From 6ad2e596d979b476e121c245543e68ec4b68145d Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Thu, 20 Oct 2022 07:58:55 +0700 Subject: [PATCH] database --- database.sqlite | Bin 0 -> 16384 bytes database/base.py | 4 ++++ database/models.py | 28 ++++++++++++++++++++++++++++ locale.py | 6 ++++++ main.py | 35 +++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 6 files changed, 74 insertions(+) create mode 100644 database.sqlite create mode 100644 database/base.py create mode 100644 database/models.py diff --git a/database.sqlite b/database.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..857d18ab649c28ac74b237e0b29a91c2529cecb9 GIT binary patch literal 16384 zcmeI%O)mpM90u?iZB&StOVc>{XL{)-B8t$1gH?lNZP{+UoMg2t*`!_6N;sA;>go$P zo1tY_6GsVoCY{W7=k@vZG->_F4>ueu#Sre!w6IAt$OP-QQ3>eH2RC7`tf=Z&lo-iIqM{ePnOo8Y-ZKF@m(?-2UEt|RH_-=+<1{w=H?9{HVA%5=<4zwJj&H6WGG(06k_ z{le4-1p*L&00bZa0SG_<0uX=z1Rwx`zb=sHSvHqjT`I3tmdY!Y_2-X3`u?x}^hbdJ z1Rwwb2tWV=5P$##AOHafK;Rz>jO%Q;|NrA(7||dA0SG_<0uX=z1Rwwb2tWV=jJ*Nx Cd#(Zi literal 0 HcmV?d00001 diff --git a/database/base.py b/database/base.py new file mode 100644 index 0000000..73f889d --- /dev/null +++ b/database/base.py @@ -0,0 +1,4 @@ +from peewee import SqliteDatabase + +# подключение к базе данных +conn = SqliteDatabase('database.sqlite') diff --git a/database/models.py b/database/models.py new file mode 100644 index 0000000..63994c9 --- /dev/null +++ b/database/models.py @@ -0,0 +1,28 @@ +from peewee import * +from .base import conn + + +# базовая можель от которой наследуются остальные +class BaseModel(Model): + class Meta: + database = conn + + +# модель пользователя +class User(BaseModel): + id = AutoField(column_name='UserId') + username = CharField(column_name='Username', max_length=100, null=False) + balance = FloatField(column_name='Balance', null=False, default=0) + + class Meta: + table_name = 'User' + + +class Transaction(BaseModel): + id = AutoField(column_name='TransactionId') + type = CharField(column_name='TransactionType', max_length=10, null=False, default='incom') + sum = FloatField(column_name='TransactionSum', null=False, default=0) + user = ForeignKeyField(User, unique=True) + + +conn.create_tables([User, Transaction]) diff --git a/locale.py b/locale.py index a9cb67d..44bab3d 100644 --- a/locale.py +++ b/locale.py @@ -7,6 +7,9 @@ ru = { /help - показать этот список. /ru - русский язык /en - английский язык +/balance - текущий баланс +/add или + - добавить доход +/rm или - - добавить расход ''', 'error404_message': 'Я не могу это сделать.' } @@ -18,6 +21,9 @@ Hi i am finance bot and i can do this: /help - open this list /ru - russian language /en - english language +/balance - balance +/add или + - add incom +/rm или - - add consuption ''', 'error404_message': 'I can\'t do this.' } diff --git a/main.py b/main.py index 283e90a..f680e88 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,11 @@ from termcolor import cprint from bot import bot import locale from changelocale import change_locale +from database.models import User + + +def to_fixed(num_obj, digits=0): + return f"{num_obj:.{digits}f}" def main_bot(messages): @@ -12,9 +17,39 @@ def main_bot(messages): mes = locale.ru['help_message'] elif locale.LOCALE == 'en': mes = locale.en['help_message'] + _, user = User.get_or_create(username=message.chat.id) bot.send_message(message.chat.id, mes) elif message.text.split(' ')[0] == '/ru' or message.text.split(' ')[0] == '/en': change_locale(message) + elif message.text.split(' ')[0] == '/balance': + user = User.get(username=message.chat.id) + bot.send_message(message.chat.id, to_fixed(user.balance, 2)) + elif message.text.split(' ')[0] == '/add' or message.text.split(' ')[0] == '+': + if len(message.text.split(' ')) < 2: + mes = None + if locale.LOCALE == 'ru': + mes = 'Используйте /add [сумма] или + [сумма]' + elif locale.LOCALE == 'en': + mes = 'Use /add [sum] or + [sum]' + bot.send_message(message.chat.id, mes) + else: + user = User.get(username=message.chat.id) + user.balance += float(message.text.split(' ')[1]) + user.save() + bot.send_message(message.chat.id, to_fixed(user.balance, 2)) + elif message.text.split(' ')[0] == '/rm' or message.text.split(' ')[0] == '-': + if len(message.text.split(' ')) < 2: + mes = None + if locale.LOCALE == 'ru': + mes = 'Используйте /rm [сумма] или - [сумма]' + elif locale.LOCALE == 'en': + mes = 'Use /rm [sum] or - [sum]' + bot.send_message(message.chat.id, mes) + else: + user = User.get(username=message.chat.id) + user.balance -= float(message.text.split(' ')[1]) + user.save() + bot.send_message(message.chat.id, to_fixed(user.balance, 2)) else: mes = None if locale.LOCALE == 'ru': diff --git a/requirements.txt b/requirements.txt index 8ee58cb..af7311d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ certifi==2022.9.24 charset-normalizer==2.1.1 idna==3.4 +peewee==3.15.3 pyTelegramBotAPI==4.7.0 requests==2.28.1 termcolor==2.0.1