Skip to content

Commit

Permalink
Merge pull request #532 from danangmassandy/fix/show-trends-earth-error
Browse files Browse the repository at this point in the history
Show error on Trends.Earth auth
  • Loading branch information
Samweli authored Nov 11, 2024
2 parents 908e7d2 + 0f7ce05 commit ad60d14
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 47 deletions.
103 changes: 76 additions & 27 deletions src/cplus_plugin/gui/settings/cplus_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,39 @@ def register(self):

if resp:
self.close()
if resp.get("status", 200) == 200:
QtWidgets.QMessageBox.information(
None,
self.tr("Success"),
self.tr(
"User registered. Your password "
f"has been emailed to {self.email.text()}. "
"Enter that password in CPLUS settings "
"to finish setting up the plugin."
),
)
# add a new auth conf that have to be completed with pwd
authConfigId = auth.init_auth_config(
auth.TE_API_AUTH_SETUP, email=self.email.text()
)

if authConfigId:
self.authConfigInitialised.emit(authConfigId)
return authConfigId
else:
QtWidgets.QMessageBox.information(
None,
self.tr("Registration failed"),
self.tr(resp.get("detail", "")),
)
else:
QtWidgets.QMessageBox.information(
None,
self.tr("Success"),
self.tr("Failed"),
self.tr(
"User registered. Your password "
f"has been emailed to {self.email.text()}. "
"Enter that password in CPLUS settings "
"to finish setting up the plugin."
"Failed to register. Please check your internet connection and try again."
),
)

# add a new auth conf that have to be completed with pwd
authConfigId = auth.init_auth_config(
auth.TE_API_AUTH_SETUP, email=self.email.text()
)

if authConfigId:
self.authConfigInitialised.emit(authConfigId)
return authConfigId
else:
return None


Expand Down Expand Up @@ -302,13 +315,29 @@ def update_profile(self):
)

if resp:
if resp.get("status", 200) == 200:
QtWidgets.QMessageBox.information(
None,
self.tr("Saved"),
self.tr("Updated information for {}.").format(self.email.text()),
)
self.close()
self.ok = True
else:
QtWidgets.QMessageBox.information(
None,
self.tr("Failed"),
self.tr(resp.get("detail", "")),
)
self.close()
else:
QtWidgets.QMessageBox.information(
None,
self.tr("Saved"),
self.tr("Updated information for {}.").format(self.email.text()),
self.tr("Failed"),
self.tr(
"Failed to update user information. Please check your internet connection and try again."
),
)
self.close()
self.ok = True


class DlgSettingsEditForgotPassword(
Expand Down Expand Up @@ -360,17 +389,33 @@ def reset_password(self):
resp = self.trends_earth_api_client.recover_pwd(self.email.text())

if resp:
self.close()
if resp.get("status", 200) == 200:
self.close()
QtWidgets.QMessageBox.information(
None,
self.tr("Success"),
self.tr(
f"The password has been reset for {self.email.text()}. "
"Check your email for the new password, and then "
"return to Trends.Earth to enter it."
),
)
self.ok = True
else:
self.close()
QtWidgets.QMessageBox.information(
None,
self.tr("Failed"),
self.tr(resp.get("detail", "")),
)
else:
QtWidgets.QMessageBox.information(
None,
self.tr("Success"),
self.tr("Failed"),
self.tr(
f"The password has been reset for {self.email.text()}. "
"Check your email for the new password, and then "
"return to Trends.Earth to enter it."
"Failed to reset password. Please check your internet connection and try again."
),
)
self.ok = True


class CplusSettings(Ui_DlgSettings, QgsOptionsPageWidget):
Expand Down Expand Up @@ -825,8 +870,12 @@ def delete(self):
# remove currently used config (as set in QSettings) and
# trigger GUI
auth.remove_current_auth_config(auth.TE_API_AUTH_SETUP)
# self.reloadAuthConfigurations()
# self.authConfigUpdated.emit()
else:
QtWidgets.QMessageBox.information(
None,
self.tr("Failed"),
self.tr("Failed to delete user."),
)

def reloadAuthConfigurations(self):
authConfigId = settings.value(
Expand Down
27 changes: 7 additions & 20 deletions src/cplus_plugin/trends_earth/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,10 @@ def login_test(self, email, password):
resp = self.call_api(
"/auth", method="post", payload={"email": email, "password": password}
)

if resp:
return True
else:
if not email or not password:
if "access_token" in resp:
return True
else:
log(
"API unable to login during login test - check " "username/password"
)
Expand All @@ -299,7 +298,7 @@ def login_test(self, email, password):
),
)

return False
return False

# @backoff.on_predicate(
# backoff.expo, lambda x: x is None, max_tries=3, on_backoff=backoff_hdlr
Expand Down Expand Up @@ -361,11 +360,7 @@ def call_api(self, endpoint, method="get", payload=None, use_token=False):
resp = None

if resp is not None:
status_code = resp.attribute(
QtNetwork.QNetworkRequest.HttpStatusCodeAttribute
)

if status_code == 200:
try:
if type(resp) is QtNetwork.QNetworkReply:
ret = resp.readAll()
ret = json.load(io.BytesIO(ret))
Expand All @@ -375,15 +370,7 @@ def call_api(self, endpoint, method="get", payload=None, use_token=False):
else:
err_msg = "Unknown object type: {}.".format(str(resp))
log(err_msg)
else:
desc, status = resp.error(), resp.errorString()
err_msg = "Error: {} (status {}).".format(desc, status)
log(err_msg)
"""
iface.messageBar().pushCritical(
"Trends.Earth", "Error: {} (status {}).".format(desc, status)
)
"""
except json.JSONDecodeError:
ret = None
else:
ret = None
Expand Down Expand Up @@ -440,7 +427,7 @@ def get_user(self, email="me"):
def delete_user(self, email="me"):
resp = self.call_api("/api/v1/user/me", "delete", use_token=True)

if resp:
if "data" in resp:
return True
else:
return None
Expand Down

0 comments on commit ad60d14

Please sign in to comment.