def cmd_remove_transaction(update, context): """remove transaction command""" LOGGER.info( '%s: CMD remove transaction', update.message.from_user.username ) if not util.check_permission( update, ['chairman'], 'CMD remove transaction' ): return try: transaction_id = int(context.args[0]) except (ValueError, IndexError): LOGGER.warning( '%s: CMD remove transaction, incorrect <transaction_id>', update.message.from_user.username, ) update.message.reply_text('Probleem met <transaction_id>') update.message.reply_text('/remove_transaction <transaction_id>') return database.remove_transaction(transaction_id) update.message.reply_text( 'Removed transaction {}'.format(transaction_id), parse_mode=ParseMode.MARKDOWN ) cmd_transactions(update, context)
def cmd_total(update, context): """Total command""" LOGGER.info('%s: CMD total', update.message.from_user.username) if not util.check_permission(update, ['trader', 'investor', 'chairman'], 'CMD total'): return total = util.get_total() total_msgs = ['*Totaal:*', '```'] total_money = 0 for item_id, item in total.items(): total_msgs.append('{:>12} {:>12}'.format( ITEMS_INV[item_id], str(util.round_number(item['amount'], 10)), )) total_msgs.append('$ {:>10} $ {:>8}/1\n'.format( str(util.round_number(item['amount'] * item['average'], 10)), str(util.round_number(item['average'], 8)), )) if item_id: total_money += item['amount'] * item['average'] else: total_money += item['amount'] total_msgs.append('total') total_msgs.append('$ {:>10}'.format(str(util.round_number(total_money, 10)))) total_msgs.append('```') update.message.reply_text('\n'.join(total_msgs), parse_mode=ParseMode.MARKDOWN)
def cmd_remove(update, context): """Set limit""" LOGGER.info('%s: CMD remove limit', update.message.from_user.username) if not util.check_permission(update, ['chairman'], 'CMD remove limit'): return try: item_name = context.args[0] except IndexError: LOGGER.info('No item name given') update.message.reply_text('/remove_limit <item_name>') update.message.reply_text('No item name given') return try: item_id = ITEMS[item_name] except IndexError: LOGGER.info('Item name not found') update.message.reply_text('/remove_limit <item_name>') update.message.reply_text('Item name not found') return database.remove_limit(item_id) update.message.reply_text('Limit removed: {}'.format(item_name), parse_mode=ParseMode.MARKDOWN)
def cmd_set_investment(update, context): """Set investment""" LOGGER.info('%s: CMD set investment', update.message.from_user.username) if not util.check_permission(update, ['chairman'], 'CMD set investment'): return try: telegram_username = context.args[0] if telegram_username[:1] == '@': telegram_username = telegram_username[1:] except (IndexError, ValueError): LOGGER.warning( '%s: CMD set investment, incorrect <investment>', update.message.from_user.username, ) update.message.reply_text('Probleem met <username>') update.message.reply_text('/set_investment <username> <amount>') return try: amount = Value(context.args[1]) except (IndexError, ValueError): LOGGER.warning( '%s: CMD set investment, incorrect <amount>', update.message.from_user.username, ) update.message.reply_text('Probleem met <amount>') update.message.reply_text('/set_investment <username> <amount>') return database.set_investment(telegram_username, amount) update.message.reply_text('investment set: {} $ {}'.format( telegram_username, amount), parse_mode=ParseMode.MARKDOWN) cmd_investors(update, context)
def cmd_investors(update, context): """List investors""" LOGGER.info('%s: CMD investors', update.message.from_user.username) if not util.check_permission(update, ['trader', 'investor', 'chairman'], 'CMD investors'): return investors = [] total = 0 for investor in database.get_investors(): investor_dict = { 'telegram_username': investor.telegram_username, 'name': investor.name, 'investment': 0 } investor_dict['investment'] += util.total_investment(investor) total += investor_dict['investment'] investors.append(investor_dict) investors_msgs = ['*Investors:*', '```'] for investor in investors: investors_msgs.append('{:10}: $ {:>8}'.format( investor['name'], str(Value(investor['investment'])), )) if not investors: investors_msgs.append('no investors') investors_msgs.append('Totaal : $ {:>8}'.format(str(Value(total)))) investors_msgs.append('```') update.message.reply_text('\n'.join(investors_msgs), parse_mode=ParseMode.MARKDOWN)
def conv_transaction_start(update, context): """Start message""" LOGGER.info('%s: CONV add_transaction, CMD start', update.message.from_user.username) if not util.check_permission(update, ['trader', 'chairman'], 'CONV add_transaction'): return ConversationHandler.END update.message.reply_text('Stuur de beschrijving voor transactie:') return TRANSACTION
def cmd_transactions(update, context): """transactions command""" LOGGER.info('%s: CMD transactions', update.message.from_user.username) if not util.check_permission( update, ['trader', 'investor', 'chairman'], 'CMD transactions' ): return try: limit = int(context.args[0]) except (IndexError, KeyError): limit = 5 try: item_id = ITEMS[context.args[1]] except (IndexError, KeyError): item_id = None transactions = database.get_transactions(limit, item_id) transactions_msgs = ['*Transacties:*'] for transaction in transactions: transaction_total = 0 transactions_msgs.append( '*{}*: {} {} ({})'.format( transaction.id, transaction.date_time.strftime("%Y-%m-%d %H:%M"), transaction.user.name, transaction.user.telegram_username, ) ) transactions_msgs.append('```') transactions_msgs.append('├ {}'.format(transaction.description)) for detail in transaction.details: transactions_msgs.append( '{} {:>14} {:>14}'.format( '├' if len(transaction.details) - 1 else '└', ITEMS_INV[detail.item_id], str(Value(detail.amount)), ) ) transactions_msgs.append( '{} $ {:>10}/1 $ {:>12}'.format( '│' if len(transaction.details) - 1 else ' ', str(Value(detail.money / detail.amount)), str(Value(detail.money)), ) ) transaction_total += detail.money if len(transaction.details) - 1: transactions_msgs.append('└ totaal $ {:>12}'.format( str(Value(transaction_total)) )) transactions_msgs.append('```') update.message.reply_text( '\n'.join(transactions_msgs), parse_mode=ParseMode.MARKDOWN )
def cmd_set_role(update, context): """Set role""" LOGGER.info('%s: CMD user set role', update.message.from_user.username) if not util.check_permission(update, ['chairman'], 'CMD user set role'): return roles = [ 'chairman', 'trader', 'investor', ] try: telegram_username = context.args[0] if telegram_username[:1] == '@': telegram_username = telegram_username[1:] except (IndexError, ValueError): LOGGER.warning( '%s: CMD user set role, incorrect <username>', update.message.from_user.username, ) update.message.reply_text('Probleem met <username>') update.message.reply_text('/set_role <username> <role> <boolean>') return try: role = context.args[1] if role not in roles: raise ValueError except (IndexError, ValueError): LOGGER.warning( '%s: CMD user set role, incorrect <role>', update.message.from_user.username, ) update.message.reply_text('Probleem met <role>') update.message.reply_text('/set_role <username> <role> <boolean>') return try: boolean = bool(context.args[2].lower() == 'true') except (IndexError, ValueError): LOGGER.warning( '%s: CMD user set role, incorrect <boolean>', update.message.from_user.username, ) update.message.reply_text('Probleem met <boolean>') update.message.reply_text('/set_role <username> <role> <boolean>') return database.set_role(telegram_username, role, boolean) update.message.reply_text('role set: {} {} {}'.format( telegram_username, role, boolean), parse_mode=ParseMode.MARKDOWN) cmd_users(update, context)
def cmd_users(update, context): """users command""" LOGGER.info('%s: CMD users', update.message.from_user.username) if not util.check_permission(update, ['trader', 'chairman'], 'CMD users'): return users = database.get_users() users_msgs = ['*Users:*', '```'] for user in users: users_msgs.append('{:12} {}'.format(user.name, ', '.join(user.get_roles()))) if not users: users_msgs.append('no users') users_msgs.append('```') update.message.reply_text('\n'.join(users_msgs), parse_mode=ParseMode.MARKDOWN)
def cmd_limits(update, context): """limits command""" LOGGER.info('%s: CMD limits', update.message.from_user.username) if not util.check_permission(update, ['trader', 'investor', 'chairman'], 'CMD limits'): return limits = database.get_limits() limits_msgs = ['*Limieten:*', '```'] for resource_id, limit in limits.items(): limits_msgs.append('{:10}: {:>9}'.format(ITEMS_INV[resource_id], str(Value(limit)))) if not limits: limits_msgs.append('no limits') limits_msgs.append('```') update.message.reply_text('\n'.join(limits_msgs), parse_mode=ParseMode.MARKDOWN)
def cmd_set(update, context): """Set limit""" LOGGER.info('%s: CMD set limit', update.message.from_user.username) if not util.check_permission(update, ['chairman'], 'CMD set limit'): return try: item_name = context.args[0] except IndexError: LOGGER.info('No item name given') update.message.reply_text('/set_limit <item_name> <amount>') update.message.reply_text('No item name given') return try: item_id = ITEMS[item_name] except IndexError: LOGGER.info('Item name not found') update.message.reply_text('/set_limit <item_name> <amount>') update.message.reply_text('Item name not found') return try: amount = context.args[1] except IndexError: LOGGER.info('No amount given') update.message.reply_text('/set_limit <item_name> <amount>') update.message.reply_text('No amount given') return try: amount = int(amount) except IndexError: LOGGER.info('Ammount is not an int') update.message.reply_text('/set_limit <item_name> <amount>') update.message.reply_text('Ammount is not an int') return database.set_limit(item_id, amount) update.message.reply_text('Limit created: {} {}'.format(item_name, amount), parse_mode=ParseMode.MARKDOWN)