def command_edit_post(id): command = Command.get(id=id) data = request.forms if ' ' in data['command']: danger(_("Spaces are not allowed in commands.")) # TODO Redirect to the edit page return { 'apis': get_apis(), 'data': data, } with db.atomic(): command.command = data['command'] command.save() command.clear_actions() for i in range(int(data['actions_number'])): # Each action has actionX prepended to its inputs namespace = 'action{0}'.format(i) module, method = data['{0}_action_type'.format(namespace)].split('|') parameters = { key[len(namespace):]: value for key, value in data.items() if key.startswith(namespace) and not key.endswith('_action_type') } action = Action.create(module=module, method=method, parameters=json.dumps(parameters)) CommandAction.create(command=command, action=action, order=i) success(_("Command edited.")) return redirect(app.get_url('commands:list'))
def update_users_money(): from utils.modules import modules users_ids = [user.id for user in modules['users'].current_users.values()] # First create the money for the user missing it in_db = set(user.user_id for user in Money.select(Money.user).filter(Money.user << users_ids)) missing_ids = set(users_ids) ^ in_db missing_moneys = [{'user': user_id, 'amount': 0} for user_id in missing_ids] with db.atomic(): for idx in range(0, len(missing_moneys), 100): Money.insert_many(missing_moneys[idx:idx + 100]).execute() # Next, update everything config = get_configuration() active_time = datetime.now() - timedelta() active_ids = [user.id for user in User.select(User.id).where(User.id << users_ids, User.last_message >= active_time.isoformat(' '))] inactive_ids = list(set(users_ids) ^ set(active_ids)) with db.atomic(): Money.update(amount=Money.amount + config.amount_gain_active).where(Money.user << active_ids).execute() Money.update(amount=Money.amount + config.amount_gain_inactive).where(Money.user << inactive_ids).execute()
def commands_add_post(): data = request.forms with db.atomic(): command = Command.create(command=data['command']) for i in range(int(data['actions_number'])): # Each action has actionX prepended to its inputs namespace = 'action{0}'.format(i) module, method = data['{0}_action_type'.format(namespace)].split('|') parameters = { key[len(namespace):]: value for key, value in data.items() if key.startswith(namespace) and not key.endswith('_action_type') } action = Action.create(module=module, method=method, parameters=json.dumps(parameters)) CommandAction.create(command=command, action=action, order=i) success(_("Command created.")) return redirect(app.get_url('commands:list'))