def post(self):
        """Creates or validates printer"""
        args = newPrinterParser.parse_args()
        if args["validate"]:  # validation of printers access data
            args = validateParser.parse_args()
            url = "http://{0}".format(args['ip'])
            auth = OctoprintService.auth(args['apikey'], url)
            if auth is not None:
                return auth, 400
            return "", 200

        else:  # creates multiple printers
            from octoprint_dashboard.app import octoprint_status
            body = request.json
            for args in body:
                url = "http://{0}".format(args['ip'])
                auth = OctoprintService.auth(args['apikey'], url)
                if auth is not None:
                    continue
                printer = Printer(args["name"], args["apikey"], url)
                db.session.add(printer)
                db.session.commit()
                octoprint_status.add_listener(printer)

            socketio.emit("rejoin", broadcast=True, skip_sid=None)
            return "", 201
    def put(self, group_id):
        """Modifies settings of group"""
        group = g.user.get_editable_group_id(group_id)
        if not group:
            return "Missing right for group", 403

        args = request.json
        group.name = args["name"]
        printer_ids = [x["id"] for x in args["printers"]]
        printers = Printer.query.filter(
            Printer.id.in_(printer_ids)) if printer_ids else [
            ]  # associate group with printers in request
        group.printer = printers

        usernames = [x["username"] for x in args["users"]]
        users = User.query.filter(
            User.username.in_(usernames)).all() if usernames else []
        group.group_user = []
        for input in args[
                "users"]:  # associate group with users trough class GroupUser in request, creating new
            # users in database
            found = None
            for user in users:
                if user.username == input["username"]:
                    found = user
            if found is None:
                found = User(input["username"])
            group.group_user.append(GroupUser(group, found, input["role"]))

        db.session.commit()

        socketio.emit("rejoin", broadcast=True, skip_sid=None)
        return "", 200
    def delete(self):
        """Deletes groups by group ids"""
        args = groupIdParser.parse_args()
        groups = g.user.get_editable_groups()
        for group in groups:
            if group.id in args["groupId"]:
                db.session.delete(group)
        db.session.commit()

        socketio.emit("rejoin", broadcast=True, skip_sid=None)
        return "", 204
    def delete(self):
        """Deletes printer according to given printer ids"""
        args = printerIdParser.parse_args()
        printers = g.user.get_accessible_printers_id(args["printerId"])
        for printer in printers:
            db.session.delete(printer)
        db.session.commit()
        for printer_id in args["printerId"]:
            socketio.close_room(str(printer_id))

        socketio.emit("rejoin", broadcast=True, skip_sid=None)
        return "", 204
Beispiel #5
0
    def post(self):
        """Saves OctoPrint settings to printers"""
        args = printerIdParser.parse_args()
        printers = g.user.get_accessible_printers_id(args["printerId"])

        for printer in printers:
            try:
                OctoprintService.save_settings(printer, request.json)
            except (requests.ConnectionError, RuntimeError):
                pass

        socketio.emit("rejoin", broadcast=True, skip_sid=None)
        return "", 200
    def put(self, printer_id):
        """Changes printer access data or name"""
        args = validateParser.parse_args()
        url = "http://{0}".format(args['ip'])
        auth = OctoprintService.auth(args['apikey'], url)
        if auth is not None:
            return "", 400
        printer = Printer.query.get(printer_id)
        printer.name = args['name']
        printer.apikey = args['apikey']
        printer.url = url
        db.session.commit()

        socketio.emit("rejoin", broadcast=True, skip_sid=None)
        return "", 200
def on_message(ws, message, printer_id):
    if message.get("current"):
        data = message.get("current")
        data["id"] = printer_id
        socketio.emit("status", data, room=str(printer_id))
 def on_error(ws, exception):
     data = {"id": printer_id, "state": {"text": "Offline/Unreachable"}}
     socketio.emit("status", data, room=str(printer_id))