def add(message, creditor, debtor, amount, currency=None): chat, _ = Chat.get_or_create(id=message.chat.id) creditor, _ = User.get_or_create(username=creditor.upper()) debtor, _ = User.get_or_create(username=debtor.upper()) amount = Decimal(amount) original_amount = amount currency = check_currency(currency, chat.currency) if currency != chat.currency: amount = cr.convert(currency, chat.currency, amount) transaction = Transaction( chat=chat, transaction_id=uuid4(), transaction_type=TransactionType.DEBT.value, date=datetime.now(), debtor=debtor, creditor=creditor, amount=amount, currency=chat.currency, original_amount=original_amount, original_currency=currency ) transaction.save() debt_graph = DebtGraph.load_graph(chat) debt_graph.add_debt(transaction) DebtGraph.save_graph(chat, debt_graph) return str(transaction)
def _handle_start_command(self, bot, update): chat_id = update.message.chat_id chat, created = Chat.get_or_create(chat_id=chat_id) if created: bot.send_message(chat_id=chat_id, text=REGISTRATION_SUCCESS_MSG) else: bot.send_message(chat_id=chat_id, text=ALREADY_REGISTERED_MSG)
def set_default_currency(message, currency='RUB'): chat, _ = Chat.get_or_create(id=message.chat.id) old_currency = chat.currency chat.currency = check_currency(currency, chat.currency) chat.inited = True chat.save() return "Changed the default currency from %s to %s" % (old_currency, chat.currency)
def wrapped(*args, **kwargs): chat, _ = Chat.get_or_create(id=args[0].chat.id) if need_to_be_inited == chat.inited: return foo(*args, **kwargs) if need_to_be_inited: return "Please, /init first" else: return "Already inited"