def index(userid):
    m = Machine.query().join(users_machines).join(User) \
        .filter(User.id == userid).all()
    return render_template("dashboard/dashboard.html",
                           title="Dashboard",
                           user=current_user,
                           machines=m)
def remove_pending():
    m = Machine.query().filter(Machine.id == request.form.get("machine_id")) \
        .filter(Machine.pending_owner_id == current_user.id).first()
    if m and str(current_user.id) == request.form.get("user_id"):
        m.delete()
        return dumps("{status: success}")
    return abort(500)
def accept_pending():
    m = Machine.query().filter(
        Machine.id == request.form.get("machine_id")).first()
    if m and str(current_user.id) == request.form.get("user_id"):
        current_user.machines.append(m)
        current_user.update()
        m.update(pending_owner_id=None)
        return dumps("{status: success}")
    return abort(500)
    def handle(self):
        dt = timestamp()
        data_json = self.request.recv(4096).strip()
        data = json.loads(data_json)

        mac = data.get("mac", None)
        if not mac:
            self.request.sendall(
                "err 1: No MAC address supplied.".encode("ascii"))
            return 1

        key = data.pop("secret", None)

        if data["type"] == "setup":
            username = data.get("username", None)
            u = User.query().filter(User.username == username).first()
            if u:
                new = Machine(mac, u.id, data.get("nickname", None))
                new.save()
                return 0
            string = f"err 4: No user exists with username: '******'"
            self.request.sendall(string.encode("ascii"))
            return 4

        else:

            m = Machine.query().filter(Machine.mac_address == mac).first()
            if not m:
                string = "err 2: No machine with supplied mac registered."
                self.request.sendall(string.encode("ascii"))
                return 2

            elif m.secret_key and m.secret_key != key:
                string = "err 3: Key supplied doesn't match the one stored."
                self.request.sendall(string.encode("ascii"))
                return 3

            if data["type"] == "sys_details":
                check_sys_details(data, m)
                command_sender(m, self.request)
                return 0

            elif data["type"] == "sys_status":
                add_sys_status(dt, data, m)
                command_sender(m, self.request)
                return 0

            elif data["type"] == "cmd_out":
                if data.get("output", None):
                    m.output = f'"{data.get("command")},{data.get("output")}"'
                    command_sender(m, self.request)
                    return 0
                self.request.sendall("err 6: No output supplied")
                return 6

        return 5