def machine_json(machineid):
    m = Machine.get(machineid)
    if m:
        status = m.sys_status
        return dumps(status)
    else:
        return abort(404)
def machine_more(machineid):
    m = Machine.get(machineid)
    if not m.nickname == "":
        title = f"Machine (#{m.id}): {m.nickname}"
    else:
        title = f"Machine: #{m.id}"
    u = User.query().join(users_machines) \
        .join(Machine).filter(Machine.id == machineid).all()
    return render_template("dashboard/machine.html",
                           m=m,
                           u=u,
                           user=current_user,
                           title=title)
def machine_add_user(machineid):
    username = request.form.get("data")
    user = User.query() \
        .filter(User.username == username).first()
    if not user:
        return {
            "error": f"Error: User with username '{username}' does not exist."
        }, 415
    machine = Machine.get(machineid)
    for m in user.machines:
        if machine.id == m.id:
            return {
                "error": "Error: This user already has access to this machine."
            }, 415
    user.machines.append(machine)
    user.save()
    return {"username": username}, 200
def machine_command(machineid):
    m = Machine.get(machineid)
    command = request.form.get("data")
    m.command = command
    while True:
        for out in m.output:
            if out[0] == command:
                if out[:4] == "<br>":
                    out = out[4:]
                return {
                    "output":
                    out[1].replace("\r\n", "<br>").replace("\n",
                                                           "<br>").replace(
                                                               "\r", "<br>")
                }, 200
        sleep(1)
        m = m.update()