def printerProfilesUpdate(identifier): if not "application/json" in request.headers["Content-Type"]: return None, None, make_response("Expected content-type JSON", 400) json_data = request.json if not "profile" in json_data: make_response("No profile included in request", 400) profile = printerProfileManager.get(identifier) if profile is None: profile = printerProfileManager.get_default() new_profile = json_data["profile"] new_profile = dict_merge(profile, new_profile) make_default = False if "default" in new_profile: make_default = True del new_profile["default"] new_profile["id"] = identifier if not _validate_profile(new_profile): make_response("Combined profile is invalid, missing obligatory values", 400) try: saved_profile = printerProfileManager.save(new_profile, allow_overwrite=True, make_default=make_default) except Exception as e: make_response("Could not save profile: %s" % e.message) return jsonify(dict(profile=_convert_profile(saved_profile)))
def printerProfilesUpdate(identifier): if not "application/json" in request.headers["Content-Type"]: return None, None, make_response("Expected content-type JSON", 400) json_data = request.json if not "profile" in json_data: make_response("No profile included in request", 400) profile = printerProfileManager.get(identifier) if profile is None: profile = printerProfileManager.get_default() new_profile = json_data["profile"] new_profile = dict_merge(profile, new_profile) make_default = False if "default" in new_profile: make_default = True del new_profile["default"] new_profile["id"] = identifier if not _validate_profile(new_profile): make_response("Combined profile is invalid, missing obligatory values", 400) try: saved_profile = printerProfileManager.save(new_profile, allow_overwrite=True, make_default=make_default) except Exception as e: make_response("Could not save profile: %s" % e.message) return jsonify(dict(profile=_convert_profile(saved_profile)))
def _get_options(): connection_options = get_connection_options() profile_options = printerProfileManager.get_all() default_profile = printerProfileManager.get_default() options = dict( ports=connection_options["ports"], baudrates=connection_options["baudrates"], printerProfiles=[ dict(id=printer_profile["id"], name=printer_profile["name"] if "name" in printer_profile else printer_profile["id"]) for printer_profile in profile_options.values() if "id" in printer_profile ], projectProfiles=[ "OPTOMA", "acer_IR", "viewsonic_IR", "acer", "viewsonic", "user_define" ], portPreference=connection_options["portPreference"], portPreference1=connection_options["portPreference1"], baudratePreference=connection_options["baudratePreference"], printerProfilePreference=default_profile["id"] if "id" in default_profile else None, projector=connection_options["projector"], ) return options
def _get_options(): connection_options = printer.__class__.get_connection_options() profile_options = printerProfileManager.get_all() default_profile = printerProfileManager.get_default() options = { "ports": connection_options["ports"], "baudrates": connection_options["baudrates"], "printerProfiles": [{ "id": printer_profile["id"], "name": printer_profile["name"] if "name" in printer_profile else printer_profile["id"], } for printer_profile in profile_options.values() if "id" in printer_profile], "portPreference": connection_options["portPreference"], "baudratePreference": connection_options["baudratePreference"], "printerProfilePreference": default_profile["id"] if "id" in default_profile else None, } return options
def printerProfilesAdd(): if "application/json" not in request.headers["Content-Type"]: return make_response("Expected content-type JSON", 400) try: json_data = request.get_json() except BadRequest: return make_response("Malformed JSON body in request", 400) if json_data is None: return make_response("Malformed JSON body in request", 400) if "profile" not in json_data: return make_response("No profile included in request", 400) base_profile = printerProfileManager.get_default() if "basedOn" in json_data and isinstance(json_data["basedOn"], basestring): other_profile = printerProfileManager.get(json_data["basedOn"]) if other_profile is not None: base_profile = other_profile if "id" in base_profile: del base_profile["id"] if "name" in base_profile: del base_profile["name"] if "default" in base_profile: del base_profile["default"] new_profile = json_data["profile"] make_default = False if "default" in new_profile: make_default = True del new_profile["default"] profile = dict_merge(base_profile, new_profile) if "id" not in profile: return make_response("Profile does not contain mandatory 'id' field", 400) if "name" not in profile: return make_response("Profile does not contain mandatory 'name' field", 400) try: saved_profile = printerProfileManager.save( profile, allow_overwrite=False, make_default=make_default, trigger_event=True ) except InvalidProfileError: return make_response("Profile is invalid", 400) except CouldNotOverwriteError: return make_response( "Profile {} already exists and overwriting was not allowed".format( profile["id"] ), 400, ) except Exception as e: return make_response("Could not save profile: %s" % str(e), 500) else: return jsonify({"profile": _convert_profile(saved_profile)})
def _convert_profile(profile): default = printerProfileManager.get_default()["id"] current = printerProfileManager.get_current_or_default()["id"] converted = copy.deepcopy(profile) converted["resource"] = url_for(".printerProfilesGet", identifier=profile["id"], _external=True) converted["default"] = (profile["id"] == default) converted["current"] = (profile["id"] == current) return converted
def _convert_profile(profile): default = printerProfileManager.get_default()["id"] current = printerProfileManager.get_current_or_default()["id"] converted = copy.deepcopy(profile) converted["resource"] = url_for(".printerProfilesGet", identifier=profile["id"], _external=True) converted["default"] = (profile["id"] == default) converted["current"] = (profile["id"] == current) return converted
def _etag(lm=None): if lm is None: lm = _lastmodified() import hashlib hash = hashlib.sha1() hash.update(str(lm)) hash.update(repr(printerProfileManager.get_default())) hash.update(repr(printerProfileManager.get_current())) return hash.hexdigest()
def _etag(lm=None): if lm is None: lm = _lastmodified() import hashlib hash = hashlib.sha1() hash.update(str(lm)) hash.update(repr(printerProfileManager.get_default())) hash.update(repr(printerProfileManager.get_current())) return hash.hexdigest()
def printerProfilesDelete(identifier): current_profile = printerProfileManager.get_current() if current_profile and current_profile["id"] == identifier: return make_response("Cannot delete currently selected profile: {}".format(identifier), 409) default_profile = printerProfileManager.get_default() if default_profile and default_profile["id"] == identifier: return make_response("Cannot delete default profile: {}".format(identifier), 409) printerProfileManager.remove(identifier) return NO_CONTENT
def printerProfilesAdd(): if "application/json" not in request.headers["Content-Type"]: abort(400, description="Expected content-type JSON") json_data = request.get_json() if json_data is None: abort(400, description="Malformed JSON body in request") if "profile" not in json_data: abort(400, description="profile is missing") base_profile = printerProfileManager.get_default() if "basedOn" in json_data and isinstance(json_data["basedOn"], str): other_profile = printerProfileManager.get(json_data["basedOn"]) if other_profile is not None: base_profile = other_profile if "id" in base_profile: del base_profile["id"] if "name" in base_profile: del base_profile["name"] if "default" in base_profile: del base_profile["default"] new_profile = json_data["profile"] make_default = False if "default" in new_profile: make_default = True del new_profile["default"] profile = dict_merge(base_profile, new_profile) if "id" not in profile: abort(400, description="profile.id is missing") if "name" not in profile: abort(400, description="profile.name is missing") try: saved_profile = printerProfileManager.save(profile, allow_overwrite=False, make_default=make_default, trigger_event=True) except InvalidProfileError: abort(400, description="profile is invalid") except CouldNotOverwriteError: abort( 400, description="Profile already exists and overwriting was not allowed" ) except Exception as e: abort(500, description="Could not save profile: %s" % str(e)) else: return jsonify({"profile": _convert_profile(saved_profile)})
def printerProfilesDelete(identifier): current_profile = printerProfileManager.get_current() if current_profile and current_profile["id"] == identifier: abort(409, description="Cannot delete currently selected profile") default_profile = printerProfileManager.get_default() if default_profile and default_profile["id"] == identifier: abort(409, description="Cannot delete default profile") printerProfileManager.remove(identifier, trigger_event=True) return NO_CONTENT
def _get_options(): connection_options = getConnectionOptions() profile_options = printerProfileManager.get_all() default_profile = printerProfileManager.get_default() options = dict( ports=connection_options["ports"], baudrates=connection_options["baudrates"], printerProfiles=[dict(id=printer_profile["id"], name=printer_profile["name"] if "name" in printer_profile else printer_profile["id"]) for printer_profile in profile_options.values() if "id" in printer_profile], portPreference=connection_options["portPreference"], baudratePreference=connection_options["baudratePreference"], printerProfilePreference=default_profile["id"] if "id" in default_profile else None ) return options
def _get_options(): connection_options = printer.__class__.get_connection_options() profile_options = printerProfileManager.get_all() default_profile = printerProfileManager.get_default() options = dict( ports=connection_options["ports"], baudrates=connection_options["baudrates"], printerProfiles=[dict(id=printer_profile["id"], name=printer_profile["name"] if "name" in printer_profile else printer_profile["id"]) for printer_profile in profile_options.values() if "id" in printer_profile], portPreference=connection_options["portPreference"], baudratePreference=connection_options["baudratePreference"], printerProfilePreference=default_profile["id"] if "id" in default_profile else None ) return options
def printerProfilesAdd(): if not "application/json" in request.headers["Content-Type"]: return make_response("Expected content-type JSON", 400) try: json_data = request.json except BadRequest: return make_response("Malformed JSON body in request", 400) if not "profile" in json_data: return make_response("No profile included in request", 400) base_profile = printerProfileManager.get_default() if "basedOn" in json_data and isinstance(json_data["basedOn"], basestring): other_profile = printerProfileManager.get(json_data["basedOn"]) if other_profile is not None: base_profile = other_profile if "id" in base_profile: del base_profile["id"] if "name" in base_profile: del base_profile["name"] if "default" in base_profile: del base_profile["default"] new_profile = json_data["profile"] make_default = False if "default" in new_profile: make_default = True del new_profile["default"] profile = dict_merge(base_profile, new_profile) if not "id" in profile: return make_response("Profile does not contain mandatory 'id' field", 400) if not "name" in profile: return make_response("Profile does not contain mandatory 'name' field", 400) try: saved_profile = printerProfileManager.save(profile, allow_overwrite=False, make_default=make_default) except InvalidProfileError: return make_response("Profile is invalid", 400) except CouldNotOverwriteError: return make_response("Profile {} already exists and overwriting was not allowed".format(profile["id"]), 400) except Exception as e: return make_response("Could not save profile: %s" % str(e), 500) else: return jsonify(dict(profile=_convert_profile(saved_profile)))
def printerProfilesUpdate(identifier): if "application/json" not in request.headers["Content-Type"]: return make_response("Expected content-type JSON", 400) try: json_data = request.get_json() except BadRequest: return make_response("Malformed JSON body in request", 400) if json_data is None: return make_response("Malformed JSON body in request", 400) if "profile" not in json_data: return make_response("No profile included in request", 400) profile = printerProfileManager.get(identifier) if profile is None: profile = printerProfileManager.get_default() new_profile = json_data["profile"] merged_profile = dict_merge(profile, new_profile) make_default = False if "default" in merged_profile: make_default = True del new_profile["default"] merged_profile["id"] = identifier try: saved_profile = printerProfileManager.save( merged_profile, allow_overwrite=True, make_default=make_default, trigger_event=True, ) except InvalidProfileError: return make_response("Profile is invalid", 400) except CouldNotOverwriteError: return make_response( "Profile already exists and overwriting was not allowed", 400 ) except Exception as e: return make_response("Could not save profile: %s" % str(e), 500) else: return jsonify({"profile": _convert_profile(saved_profile)})
def printerProfilesAdd(): if not "application/json" in request.headers["Content-Type"]: return None, None, make_response("Expected content-type JSON", 400) json_data = request.json if not "profile" in json_data: return None, None, make_response("No profile included in request", 400) base_profile = printerProfileManager.get_default() if "basedOn" in json_data and isinstance(json_data["basedOn"], basestring): other_profile = printerProfileManager.get(json_data["basedOn"]) if other_profile is not None: base_profile = other_profile if "id" in base_profile: del base_profile["id"] if "name" in base_profile: del base_profile["name"] profile = dict_merge(base_profile, json_data["profile"]) if not _validate_profile(profile): return None, None, make_response("Profile is invalid, missing obligatory values", 400) return _overwrite_profile(profile)
def printerProfilesAdd(): if not "application/json" in request.headers["Content-Type"]: return None, None, make_response("Expected content-type JSON", 400) json_data = request.json if not "profile" in json_data: return None, None, make_response("No profile included in request", 400) base_profile = printerProfileManager.get_default() if "basedOn" in json_data and isinstance(json_data["basedOn"], basestring): other_profile = printerProfileManager.get(json_data["basedOn"]) if other_profile is not None: base_profile = other_profile if "id" in base_profile: del base_profile["id"] if "name" in base_profile: del base_profile["name"] profile = dict_merge(base_profile, json_data["profile"]) if not _validate_profile(profile): return None, None, make_response( "Profile is invalid, missing obligatory values", 400) return _overwrite_profile(profile)
def printerProfilesUpdate(identifier): if not "application/json" in request.headers["Content-Type"]: return make_response("Expected content-type JSON", 400) try: json_data = request.json except JSONBadRequest: return make_response("Malformed JSON body in request", 400) if not "profile" in json_data: return make_response("No profile included in request", 400) profile = printerProfileManager.get(identifier) if profile is None: profile = printerProfileManager.get_default() new_profile = json_data["profile"] new_profile = dict_merge(profile, new_profile) make_default = False if "default" in new_profile: make_default = True del new_profile["default"] new_profile["id"] = identifier try: saved_profile = printerProfileManager.save(new_profile, allow_overwrite=True, make_default=make_default) except InvalidProfileError: return make_response("Profile is invalid", 400) except CouldNotOverwriteError: return make_response("Profile already exists and overwriting was not allowed", 400) except Exception as e: return make_response("Could not save profile: %s" % e.message, 500) else: return jsonify(dict(profile=_convert_profile(saved_profile)))