def controlJob(): if not printer.is_operational(): return make_response("Printer is not operational", 409) valid_commands = { "start": [], "restart": [], "pause": [], "cancel": [] } command, data, response = get_json_command_from_request(request, valid_commands) if response is not None: return response activePrintjob = printer.is_printing() or printer.is_paused() if command == "start": if activePrintjob: return make_response("Printer already has an active print job, did you mean 'restart'?", 409) printer.start_print() elif command == "restart": if not printer.is_paused(): return make_response("Printer does not have an active print job or is not paused", 409) printer.start_print() elif command == "pause": if not activePrintjob: return make_response("Printer is neither printing nor paused, 'pause' command cannot be performed", 409) printer.toggle_pause_print() elif command == "cancel": if not activePrintjob: return make_response("Printer is neither printing nor paused, 'cancel' command cannot be performed", 409) printer.cancel_print() return NO_CONTENT
def controlJob(): if not printer.is_operational(): abort(409, description="Printer is not operational") valid_commands = {"start": [], "restart": [], "pause": [], "cancel": []} command, data, response = get_json_command_from_request( request, valid_commands) if response is not None: return response activePrintjob = printer.is_printing() or printer.is_paused() tags = {"source:api", "api:job"} user = current_user.get_name() with Permissions.PRINT.require(403): if command == "start": if activePrintjob: abort( 409, description= "Printer already has an active print job, did you mean 'restart'?", ) printer.start_print(tags=tags, user=user) elif command == "restart": if not printer.is_paused(): abort( 409, description= "Printer does not have an active print job or is not paused", ) printer.start_print(tags=tags, user=user) elif command == "pause": if not activePrintjob: abort( 409, description= "Printer is neither printing nor paused, 'pause' command cannot be performed", ) action = data.get("action", "toggle") if action == "toggle": printer.toggle_pause_print(tags=tags, user=user) elif action == "pause": printer.pause_print(tags=tags, user=user) elif action == "resume": printer.resume_print(tags=tags, user=user) else: abort(400, description="Unknown action") elif command == "cancel": if not activePrintjob: abort( 409, description= "Printer is neither printing nor paused, 'cancel' command cannot be performed", ) printer.cancel_print(tags=tags, user=user) return NO_CONTENT
def controlJob(): if not printer.is_operational(): return make_response("Printer is not operational", 409) valid_commands = {"start": [], "restart": [], "pause": [], "cancel": []} command, data, response = get_json_command_from_request( request, valid_commands) if response is not None: return response activePrintjob = printer.is_printing() or printer.is_paused() tags = {"source:api", "api:job"} user = current_user.get_name() with Permissions.PRINT.require(403): if command == "start": if activePrintjob: return make_response( "Printer already has an active print job, did you mean 'restart'?", 409) printer.start_print(tags=tags, user=user) elif command == "restart": if not printer.is_paused(): return make_response( "Printer does not have an active print job or is not paused", 409) printer.start_print(tags=tags, user=user) elif command == "pause": if not activePrintjob: return make_response( "Printer is neither printing nor paused, 'pause' command cannot be performed", 409) action = data.get("action", "toggle") if action == "toggle": printer.toggle_pause_print(tags=tags, user=user) elif action == "pause": printer.pause_print(tags=tags, user=user) elif action == "resume": printer.resume_print(tags=tags, user=user) else: return make_response( "Unknown action '{}', allowed values for action parameter are 'pause', 'resume' and 'toggle'" .format(action), 400) # Safety first, everyone can cancel a print, but log who canceled the print # to prevent fraudulent use if command == "cancel": if not activePrintjob: return make_response( "Printer is neither printing nor paused, 'cancel' command cannot be performed", 409) printer.cancel_print(tags=tags, user=user) return NO_CONTENT
def controlJob(): if not printer.is_operational(): return make_response("Printer is not operational", 409) valid_commands = { "start": [], "restart": [], "pause": [], "cancel": [], "shutdown": [] } command, data, response = get_json_command_from_request(request, valid_commands) if response is not None: return response activePrintjob = printer.is_printing() or printer.is_paused() or printer.is_preparing_print() or printer.is_shutdown() if command == "start": if activePrintjob: return make_response("Printer already has an active print job, did you mean 'restart'?", 409) printer.start_print() elif command == "restart": if not printer.is_paused(): return make_response("Printer does not have an active print job or is not paused", 409) printer.start_print() elif command == "pause": if not activePrintjob: return make_response("Printer is neither printing nor paused, 'pause' command cannot be performed", 409) action = data.get("action", "toggle") if action == "toggle": printer.toggle_pause_print() elif action == "pause": printer.pause_print() elif action == "resume": printer.resume_print() else: return make_response("Unknown action '{}', allowed values for action parameter are 'pause', 'resume' and 'toggle'".format(action), 400) elif command == "cancel": if not activePrintjob: printer.unselect_file() else: printer.cancel_print() elif command == "shutdown": if not printer.is_paused(): return make_response("Printer does not have an active print job or is not paused", 409) printer.enter_shutdown_mode() return NO_CONTENT
def controlJob(): if not printer.is_operational(): return make_response("Printer is not operational", 409) valid_commands = {"start": [], "restart": [], "pause": [], "cancel": []} command, data, response = get_json_command_from_request( request, valid_commands) if response is not None: return response activePrintjob = printer.is_printing() or printer.is_paused() if command == "start": if activePrintjob: return make_response( "Printer already has an active print job, did you mean 'restart'?", 409) printer.start_print() elif command == "restart": if not printer.is_paused(): return make_response( "Printer does not have an active print job or is not paused", 409) printer.start_print() elif command == "pause": if not activePrintjob: return make_response( "Printer is neither printing nor paused, 'pause' command cannot be performed", 409) action = data.get("action", "toggle") if action == "toggle": printer.toggle_pause_print() elif action == "pause": printer.pause_print() elif action == "resume": printer.resume_print() else: return make_response( "Unknown action '{}', allowed values for action parameter are 'pause', 'resume' and 'toggle'" .format(action), 400) elif command == "cancel": if not activePrintjob: return make_response( "Printer is neither printing nor paused, 'cancel' command cannot be performed", 409) printer.cancel_print() return NO_CONTENT