Code cleanup

This commit is contained in:
Jaime van Kessel 2016-07-15 15:05:02 +02:00
parent 097d4c9e6b
commit 479a155b39

View file

@ -391,7 +391,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
if reply.operation() == QNetworkAccessManager.GetOperation: if reply.operation() == QNetworkAccessManager.GetOperation:
if "printer" in reply.url().toString(): # Status update from printer. if "printer" in reply.url().toString(): # Status update from printer.
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 200: if status_code == 200:
if self._connection_state == ConnectionState.connecting: if self._connection_state == ConnectionState.connecting:
self.setConnectionState(ConnectionState.connected) self.setConnectionState(ConnectionState.connected)
self._json_printer_state = json.loads(bytes(reply.readAll()).decode("utf-8")) self._json_printer_state = json.loads(bytes(reply.readAll()).decode("utf-8"))
@ -401,7 +401,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
Logger.log("w", "We got an unexpected status (%s) while requesting printer state", status_code) Logger.log("w", "We got an unexpected status (%s) while requesting printer state", status_code)
pass # TODO: Handle errors pass # TODO: Handle errors
elif "print_job" in reply.url().toString(): # Status update from print_job: elif "print_job" in reply.url().toString(): # Status update from print_job:
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 200: if status_code == 200:
json_data = json.loads(bytes(reply.readAll()).decode("utf-8")) json_data = json.loads(bytes(reply.readAll()).decode("utf-8"))
progress = json_data["progress"] progress = json_data["progress"]
## If progress is 0 add a bit so another print can't be sent. ## If progress is 0 add a bit so another print can't be sent.
@ -412,7 +412,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self.setTimeElapsed(json_data["time_elapsed"]) self.setTimeElapsed(json_data["time_elapsed"])
self.setTimeTotal(json_data["time_total"]) self.setTimeTotal(json_data["time_total"])
self.setJobName(json_data["name"]) self.setJobName(json_data["name"])
elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 404: elif status_code == 404:
self.setProgress(0) # No print job found, so there can't be progress or other data. self.setProgress(0) # No print job found, so there can't be progress or other data.
self._updateJobState("") self._updateJobState("")
self.setTimeElapsed(0) self.setTimeElapsed(0)
@ -421,18 +421,18 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
else: else:
Logger.log("w", "We got an unexpected status (%s) while requesting print job state", status_code) Logger.log("w", "We got an unexpected status (%s) while requesting print job state", status_code)
elif "snapshot" in reply.url().toString(): # Status update from image: elif "snapshot" in reply.url().toString(): # Status update from image:
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 200: if status_code == 200:
self._camera_image.loadFromData(reply.readAll()) self._camera_image.loadFromData(reply.readAll())
self.newImage.emit() self.newImage.emit()
elif "auth/verify" in reply.url().toString(): # Answer when requesting authentication elif "auth/verify" in reply.url().toString(): # Answer when requesting authentication
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 401: if status_code == 401:
if self._authentication_state != AuthState.AuthenticationRequested: if self._authentication_state != AuthState.AuthenticationRequested:
# Only request a new authentication when we have not already done so. # Only request a new authentication when we have not already done so.
Logger.log("i", "Not authenticated. Attempting to request authentication") Logger.log("i", "Not authenticated. Attempting to request authentication")
self._requestAuthentication() self._requestAuthentication()
elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 403: elif status_code == 403:
pass pass
elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 200: elif status_code == 200:
self.setAuthenticationState(AuthState.Authenticated) self.setAuthenticationState(AuthState.Authenticated)
global_container_stack = Application.getInstance().getGlobalContainerStack() global_container_stack = Application.getInstance().getGlobalContainerStack()
## Save authentication details. ## Save authentication details.
@ -478,8 +478,8 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
reply.uploadProgress.disconnect(self._onUploadProgress) reply.uploadProgress.disconnect(self._onUploadProgress)
self._progress_message.hide() self._progress_message.hide()
elif reply.operation() == QNetworkAccessManager.PutOperation: elif reply.operation() == QNetworkAccessManager.PutOperation:
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 204: if status_code == 204:
pass # Request was sucesfull! pass # Request was successful!
else: else:
Logger.log("d", "Something went wrong when trying to update data of API (%s). Message: %s Statuscode: %s", reply.url().toString(), reply.readAll(), reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)) Logger.log("d", "Something went wrong when trying to update data of API (%s). Message: %s Statuscode: %s", reply.url().toString(), reply.readAll(), reply.attribute(QNetworkRequest.HttpStatusCodeAttribute))
else: else: