def link_telegram(message): username = message.text if username == 'New': create_new_user(message) telegram_id = message.from_user.id server_conn('link_telegram_id', [username, telegram_id]) bot.reply_to(message, 'Telegram linked to {}'.format(username), reply_markup=types.ReplyKeyboardRemove())
def balance(self, counterpart_id=-1): # todo currently I/O happens on every balance query - maybe, create attribute with all possible balances in case of slow I/O if counterpart_id == -1: user_pair = {'debtor_id': self.user_id, 'creditor_id': None} debt = server_conn('amount_check', user_pair) user_pair = {'debtor_id': None, 'creditor_id': self.user_id} receivables = server_conn('amount_check', user_pair) else: user_pair = {'debtor_id': self.user_id, 'creditor_id': counterpart_id} debt = server_conn('amount_check', user_pair) user_pair = {'debtor_id': counterpart_id, 'creditor_id': self.user_id} receivables = server_conn('amount_check', user_pair) return [debt, receivables]
def write(self): user = {'user_id': self.user_id, 'f_name': self.f_name, 'l_name': self.l_name, 'username': self.username, 'telegram_id': self.telegram_id} self.user_id = server_conn('write_user', user)
def add_member_to_split(dic, message): print('TEXT', message.text) if message.text == 'Nobody': server_conn('new_trx_with_equal_split', {'amount': dic['amount'], 'creditor': dic['creditor'], 'debtors': dic['debtors'], 'trx_name': dic['trx_name']}) bot.send_message(message.chat.id, '''{0}'s transaction of {1} has been equally split among {2}.'''.format( dic['creditor'], dic['amount'], ', '.join(dic['debtors'])), reply_markup=types.ReplyKeyboardRemove()) else: print('DIC', dic['debtors']) print('TEXT', message.text) dic['debtors'].append(message.text) print('DIC', dic['debtors']) process_transaction_split(dic, dic['split_type'])
def load(self): credentials = server_conn('check_user', self.username) if credentials is not None: self.user_id = credentials['user_id'] self.f_name = credentials['f_name'] self.l_name = credentials['l_name'] self.telegram_id = credentials['telegram_id'] # else: # self.create_new_user() self.description = self.describe(short=False)
def load(self): details = server_conn('check_trx', self.trx_id) if details is not None: self.trx_name = details['trx_name'] self.creditor_id = details['creditor_id'] self.debtors_id = details['debtors_id'] self.currency = details['currency'] self.full_amount = details['amount'] self.date = details['date'] else: print('No transaction found with this ID')
def create_new_user(self): # todo maybe remove from server side y_n = None while y_n not in ['y', 'Y', 'n', 'N']: y_n = input('No such user found. Create new user? (y/n) ') if y_n in ['n', 'N']: print('Leaving without user creation.') if y_n in ['y', 'Y']: self.user_id = max(server_conn('list_users')) + 1 good_username = False while not good_username: test_username = input('Enter username: '******'Username should be at least 4 symbols long') elif server_conn('check_username_availability', test_username): good_username = True self.username = test_username self.f_name = input('Enter user first name: ') self.l_name = input('Enter user last name: ') self.write()
def __init__(self, trx_id=0, creditor_id=0, debtor_id=1, currency='RUB', iou_amount=100.00, date=datetime.datetime.now()): self.trx_id = trx_id self.creditor_id = creditor_id self.debtor_id = debtor_id self.currency = currency self.iou_amount = iou_amount self.date = date print('allocating') self.iou_id = server_conn('allocate_iou_id') print('allocated')
def balance_overview(message): # todo update with a keyboard & usernames if message.text == '': users = -1 else: users = [message.text] print('users', users) to_print = '' counterparts = server_conn('list_users') counterpart_usernames = counterparts if users == -1: users = counterpart_usernames for username in users: print('user', username) to_print += '\n' to_print += ('=' * 50) user1 = User(username=username) user1.load() to_print += '\n' to_print += user1.f_name user1_balance = user1.balance() to_print += '\n' to_print += ('Accumulated debt: {0} RUB'.format(user1_balance[0])) to_print += '\n' to_print += ('Accumulated receivables: {0} RUB'.format(user1_balance[1])) remaining_users = [i for i in counterpart_usernames if i != user1.user_id] for counterpart_id in remaining_users: user2 = User(username=counterpart_id) counterpart_balance = user1.balance(counterpart_id=user2.user_id) debt_to_counterpart = counterpart_balance[0] receivables_from_counterpart = counterpart_balance[1] if debt_to_counterpart != 0 or receivables_from_counterpart != 0: user2.load() user2.describe() to_print += '\n' to_print += ('-' * 20) to_print += '\n' to_print += ('Debt to {0}: {1} RUB:'.format(user2.f_name, debt_to_counterpart)) to_print += '\n' to_print += ('Receivables from {0}: {1} RUB'.format(user2.f_name, receivables_from_counterpart)) bot.send_message(message.chat.id, to_print, reply_markup=types.ReplyKeyboardRemove())
def __init__(self, creditor_id=-1, trx_id=-1, trx_name='Empty name', currency='RUB', full_amount=100.00, date=datetime.datetime.now(), debtors_id=None): if trx_id == -1: self.trx_id = server_conn('allocate_trx_id') else: self.trx_id = trx_id self.trx_name = trx_name self.creditor_id = creditor_id self.debtors_id = debtors_id self.currency = currency self.full_amount = full_amount self.date = date self.save()
def save(self): server_conn('update_iou', {'trx_id': self.trx_id, 'IOU_id': self.iou_id, 'creditor_id': self.creditor_id, 'debtor_id': self.debtor_id, 'currency': self.currency, 'amount': self.iou_amount, 'date': self.date})
def save(self): server_conn('update_trx', {'trx_id': self.trx_id, 'trx_name': self.trx_name, 'creditor_id': self.creditor_id, 'debtors_id': self.debtors_id, 'currency': self.currency, 'amount': self.full_amount, 'date': self.date})
def check_username(message): username = message.text if server_conn('check_user', username) is None: get_f_name(message) else: create_new_user(message, first_try=False)