-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
103 lines (89 loc) · 3.58 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import db_handler as db
from telebot import types
from datetime import datetime
help_msg = '''
This bot helps you to track your money usage
There are commands below:
/help - show this help page
/add_record - add new income or expense record
/show_report - generate report (will be avalible in v2 and above)
'''
def get_back_to_start_btn(divider):
''' Return btn with callback data that tells to go to start '''
return types.InlineKeyboardButton(text='Back to start', callback_data=enc_callback_data(divider, 'back_to_start', '.'))
def get_help_msg():
''' Just return help msg '''
return help_msg
def enc_callback_data(divider, *words):
''' Encode data into one line for callback using a divider, return line '''
call_data = ''
for word in words:
call_data += str(word) + str(divider)
return call_data
def dec_callback_data(divider, call_data):
''' Decode data from callback using a divider, return in array '''
data_arr = []
for word in call_data.split(divider):
data_arr.append(word)
return data_arr
def get_cat_btns_by_type(db_filename, cat_type, divider):
''' Return keyboard with needed categories ready to be used in msg '''
btns_arr = []
for cat in db.get_cats_arr(db_filename):
if cat.type == cat_type:
btn_data = enc_callback_data(divider, cat_type, cat.name)
btns_arr.append([cat.name, btn_data])
btns = get_btns_in_rows(2, btns_arr)
btns.add(get_back_to_start_btn(divider))
return btns
def get_currency_btns(db_filename, divider, prefix):
''' Return keyboard with currencies ready to be used in msg '''
btns_arr = []
for curr in db.get_currs_arr(db_filename):
btn_data = enc_callback_data(divider, prefix + 'curr', curr)
btns_arr.append([curr, btn_data])
btns = get_btns_in_rows(6, btns_arr)
btns.add(get_back_to_start_btn(divider))
return btns
def get_start_rec_add_kbrd(divider, user_id=None):
''' Returns default keyboard for /add_record func '''
btns_arr = []
btns_arr.append(['Income', enc_callback_data(divider, 'start', 'income', user_id)])
btns_arr.append(['Expence', enc_callback_data(divider, 'start', 'expense', user_id)])
return get_btns_in_rows(2, btns_arr)
def get_btns_in_rows(columns_num, btn_data_arr):
''' Return keyboard that is sorted in rows and columns '''
counter = 0
btn_arr = []
key = types.InlineKeyboardMarkup()
for btn in btn_data_arr:
if int(counter) >= int(columns_num):
counter = 0
key.row(*btn_arr)
btn_arr = []
counter += 1
btn_arr.append(types.InlineKeyboardButton(text=btn[0], callback_data=btn[1]))
if btn_arr:
key.row(*btn_arr)
return key
def get_curr_setup_kbrd(divider):
''' Returns keyboard for currencies setup '''
btns_arr = []
btns_arr.append(['add', enc_callback_data(divider, 'curr_setup', 'add')])
btns_arr.append(['delete', enc_callback_data(divider, 'curr_setup', 'del')])
return get_btns_in_rows(2, btns_arr)
def make_rec_readable(rec):
''' Make readable line with record info '''
line_template = 'id = {id}\n' \
'type: {type}\n' \
'category: {cat}\n' \
'date UTC: {date}\n' \
'currency: {curr}\n' \
'ammount: {amount}\n' \
'amount USD: {amount_usd}\n' \
'comment: {comment}'
line = line_template.format(id=rec.id, type=rec.type, cat=rec.category,
date=datetime.utcfromtimestamp(rec.date_ts).strftime('%Y-%m-%d %H:%M:%S'),
curr=rec.currency, amount=rec.amount, amount_usd=rec.amount_usd,
comment=rec.comment )
return line