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_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 conv_transaction_detail_add(update, context): """Add transaction detail""" command = update.message.text.split(' ')[0] LOGGER.info('%s: CONV add_transaction, CMD %s', update.message.from_user.username, command) try: item_id = ITEMS[context.args[0]] except (IndexError, KeyError): LOGGER.warning('%s: CONV add_transaction, CMD %s, incorrect item name', update.message.from_user.username, command) update.message.reply_text('Probleem met <name>.') update.message.reply_text( '{} <item> <amount> <price_each>'.format(command)) return DETAIL try: if command == '/sell': amount = Value(-abs(Value(context.args[1]))) elif command == '/buy': amount = Value(abs(Value(context.args[1]))) except (IndexError, ValueError): LOGGER.warning('%s: CONV add_transaction, CMD %s, incorrect amount', update.message.from_user.username, command) update.message.reply_text('Probleem met <amount>.') update.message.reply_text( '{} <item> <amount> <price_each>'.format(command)) return DETAIL try: price = Value(context.args[2]) except (IndexError, ValueError): LOGGER.warning( '%s: CONV add_transaction, CMD %s, incorrect price each', update.message.from_user.username, command) update.message.reply_text('Probleem met <price_each>.') update.message.reply_text( '{} <item> <amount> <price_each>'.format(command)) return DETAIL if command == '/sell': price = abs(amount * price) elif command == '/buy': price = -abs(amount * price) context.user_data['transaction']['details'].append({ 'item_id': item_id, 'amount': amount, 'money': Value(price), }) print_transaction(update, context) update.message.reply_text( 'Voeg meer details toe ' 'of verwijder details met: `/remove <index>`. ' 'Sla de transactie op met: `/save`.', parse_mode=ParseMode.MARKDOWN) return DETAIL
def check_permission(update, roles, action): """Check permissions""" executor = database.get_user_by_telegram_id(update.message.from_user.id) if not executor: executor = database.get_user_by_telegram_username( update.message.from_user.username) if executor: executor.telegram_id = update.message.from_user.id executor = database.save_user(executor) else: executor = database.add_user(update.message.from_user.first_name, update.message.from_user.id, update.message.from_user.username) if TESTING: return True for role in executor.get_roles(): if role in roles: return True LOGGER.warning('%s: %s, not allowed', update.message.from_user.username, action) update.message.reply_text( 'Rollen die recht hebben op dit command: {}'.format(', '.join(roles))) return False
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)