Skip to content

Commit

Permalink
auth fomr - displaying messages in a bar
Browse files Browse the repository at this point in the history
#257  bonus
+ handling more API request errors
+ modularizing the display of information for the user about API requesting or authentication file
  • Loading branch information
SimonSAMPERE committed Oct 3, 2019
1 parent cf1f044 commit 37dcecb
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 41 deletions.
63 changes: 22 additions & 41 deletions isogeo.py
Expand Up @@ -66,6 +66,7 @@
IsogeoPlgTools,
SharesParser,
SearchFormManager,
UserInformer
)

# ############################################################################
Expand Down Expand Up @@ -205,11 +206,14 @@ def __init__(self, iface):

# SUBMODULES
# instanciating
self.informer = UserInformer(message_bar = msgBar, trad = self.tr)

self.md_display = MetadataDisplayer()

self.approps_mng = SharesParser()
self.approps_mng.tr = self.tr


self.authenticator = Authenticator()

self.api_requester = ApiRequester()
Expand All @@ -220,7 +224,8 @@ def __init__(self, iface):
self.form_mng.qs_mng.lang = self.lang

# connecting
self.api_requester.token_sig.connect(self.token_slot)
self.api_requester.api_sig.connect(self.token_slot)
self.api_requester.api_sig.connect(self.informer.request_slot)
self.api_requester.search_sig.connect(self.search_slot)
self.api_requester.details_sig.connect(self.md_display.show_complete_md)
self.api_requester.shares_sig.connect(self.approps_mng.send_share_info)
Expand Down Expand Up @@ -356,52 +361,38 @@ def user_authentication(self):
their default value, it asks for them.
If not, it tries to send a request.
"""
self.authenticator.auth_sig.disconnect()
self.savedSearch = "first"
self.form_mng.switch_widgets_on_and_off(0)
api_init = self.authenticator.manage_api_initialization()
if api_init[0]:
self.api_requester.setup_api_params(api_init[1])
else :
pass
self.authenticator.auth_sig.connect(self.user_authentication)

# def write_ids_and_test(self):
# """Store the id & secret and launch the test function.
# Called when the authentification window is closed,
# it stores the values in the file, then call the
# user_authentification function to test them.
# """
# self.authenticator.auth_sig.disconnect()

# if self.authenticator.credentials_uploader() is True:
# # launch authentication
# self.user_authentication()
# else :
# pass

# self.authenticator.auth_sig.connect(
# self.write_ids_and_test
# )

def auth_slot(self, auth_signal: str):
if auth_signal == "ok":
self.user_authentication()
else :
pass

def token_slot(self, token_signal: str):
""" Slot connected to ApiRequester.token_sig signal emitted when a response to
""" Slot connected to ApiRequester.api_sig signal emitted when a response to
a token request has been received from Isogeo's API or when the content of
a response to any type of request can't be parsed. The 'token_sig' parameter
a response to any type of request can't be parsed. The 'api_sig' parameter
correspond to the string passed by ApiRequester.handle_reply method (see
modules/api/request.py). The value of this parameter depend on the response's
content received from Isogeo's API.
:param str token_signal: a string passed by the signal whose value determines
what will be done. Options :
- "tokenOK" : Authentication has succeeded, the token is stored so it sends
- "ok" : Authentication has succeeded, the token is stored so it sends
a search request to the API.
- "credIssue" : User's credentials are wrong so it displays the authentication
- "creds_issue" : User's credentials are wrong so it displays the authentication
form to provide good ones.
- "NoInternet" : Asks to user to check his Internet connection.
"""
logger.debug(token_signal)
if token_signal == "tokenOK":
logger.debug("*=====* token_signal : {}".format(token_signal))
if token_signal == "ok":
if self.savedSearch == "first":
self.authenticator.ui_auth_form.btn_ok_cancel.buttons()[0].setEnabled(True)
self.authenticator.first_auth = False
Expand All @@ -411,17 +402,10 @@ def token_slot(self, token_signal: str):
self.api_requester.send_request(request_type="shares")
else:
self.api_requester.send_request()
elif token_signal == "credIssue":
self.authenticator.show_error("creds")

elif token_signal == "creds_issue":
self.authenticator.ui_auth_form.btn_ok_cancel.buttons()[0].setEnabled(False)
else:
msgBar.pushMessage(
self.tr(
"Request to Isogeo failed: please check your Internet"
" connection and your proxy configuration"
),
duration=10,
level=1,
)
self.pluginIsActive = False

# --- SEARCH --------------------------------------------------------------
Expand Down Expand Up @@ -863,11 +847,8 @@ def run(self):
self.form_mng.btn_credits.pressed.connect(self.credits_dialog.show)

# -- Authentication form ----------------------------------------------
# self.authenticator.auth_sig.connect(
# self.write_ids_and_test
# )
self.authenticator.auth_sig.connect(
self.user_authentication
self.auth_slot
)


Expand Down
1 change: 1 addition & 0 deletions modules/__init__.py
Expand Up @@ -4,3 +4,4 @@
from .tools import IsogeoPlgTools
from .quick_search import QuickSearchManager
from .search_form import SearchFormManager
from .user_inform import UserInformer
107 changes: 107 additions & 0 deletions modules/user_inform.py
@@ -0,0 +1,107 @@
# -*- coding: utf-8 -*-
#! python3 # noqa: E265

# Standard library
import logging
import json
from pathlib import Path

# PyQGIS
from qgis.utils import iface
from qgis.gui import QgsMessageBar

# PyQT
from qgis.PyQt.QtGui import QIcon


# ############################################################################
# ########## Globals ###############
# ##################################

logger = logging.getLogger("IsogeoQgisPlugin")

# ############################################################################
# ########## Classes ###############
# ##################################


class UserInformer:
""" A basic class to manage the displaying of message to the user.
"""

def __init__(self, message_bar: object, trad: object):
if isinstance(message_bar, QgsMessageBar):
self.bar = message_bar
else:
raise TypeError
self.tr = trad

def show(self, message: str, duration: int = 6, level: int = 1):
self.bar.pushMessage(message, duration=duration, level=level)

def authentication_slot(self, auth_sig: str):
msg_dict = {
"path": [
self.tr("The specified file does not exist.", "UserInformer"),
5,
1
],
"file": [
self.tr(
"The selected credentials file's format is not valid.",
"UserInformer"
),
5,
1
],
"ok": [
self.tr(
"Authentication file is valid. Asking for authorization to Isogeo's API.",
"UserInformer"
),
5,
3
]
}
if auth_sig in list(msg_dict.keys()):
msg_type = msg_dict.get(auth_sig)
self.show(message=msg_type[0], duration=msg_type[1], level=msg_type[2])
else :
pass

def request_slot(self, api_sig: str):
msg_dict = {
"creds_issue": self.tr(
"Redirecting code received. ID and SECRET could be invalid. Provide them again."
" If this error keeps happening, please report it in the bug tracker.",
"UserInformer"
),
"proxy_issue": self.tr(
"Proxy error found. Check your OS and QGIS proxy configuration."
"If this error keeps happening, please report it in the bug tracker.",
"UserInformer"
),
"shares_issue": self.tr(
"The script is looping. Make sure you shared a catalog with the plugin."
"If this error keeps happening, please report it in the bug tracker.",
"UserInformer"
),
"unkown_error": self.tr(
"Request to Isogeo's API failed : unkown error found. Please, report it in the bug tracker.",
"UserInformer"
),
"unkonw_reply": self.tr(
"API authentication failed : unexpected API's reply. Please, report it in the bug tracker.",
"UserInformer"
),
"internet_issue" : self.tr(
"Request to Isogeo's API failed : please check your Internet connection and your proxy configuration."
"If this error keeps happening, please report it in the bug tracker.",
"UserInformer"
)
}
if api_sig in list(msg_dict.keys()):
msg = msg_dict.get(api_sig)
self.show(message = msg, duration = 10)
else:
pass
3 changes: 3 additions & 0 deletions ui/auth/ui_authentication.ui
Expand Up @@ -37,6 +37,9 @@
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QVBoxLayout" name="msgbar_vlayout"/>
</item>
<item>
<widget class="QGroupBox" name="grb_connection_ready">
<property name="minimumSize">
Expand Down

0 comments on commit 37dcecb

Please sign in to comment.