Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
- Change some icons and function names
- Handle logout errors correctly
- Put login window on top when activated
- Print warnings of OSF token to console instead of using notifications
  • Loading branch information
dschreij committed May 4, 2016
1 parent 702bff4 commit 5c6bf34
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
20 changes: 12 additions & 8 deletions QOpenScienceFramework/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def show_login_window(self):

self.browser.load(browser_url)
self.browser.show()
self.browser.raise_()
self.browser.activateWindow()

def logout(self):
""" Logs out from OSF """
Expand All @@ -149,13 +151,18 @@ def logout(self):
osf.logout_url,
self.__logout_succeeded,
{'token':osf.session.access_token},
errorCallback=self.__logout_failed
)

def __logout_succeeded(self,data,*args):
""" Callback for logout(). Called when logout has succeeded. This function
def __logout_succeeded(self, data, *args):
""" Callback for logout(). Called when logout has failed. This function
will then dispatch the logout signal to all other connected elements. """
self.dispatcher.dispatch_logout()

def __logout_failed(self, data, *args):
""" Callback for logout(). Called when logout has failed. """
self.dispatcher.dispatch_login()

def check_for_stored_token(self, tokenfile):
""" Checks if valid token information is stored in a token.json file at
the supplied location. If not, or if the oken is invalid/expired, it returns
Expand Down Expand Up @@ -321,8 +328,7 @@ def get(self, url, callback, *args, **kwargs):

# Add OAuth2 token
if not self.add_token(request):
self.warning_message.emit('Warning',
_(u"Token could not be added to the request"))
warnings.warn(_(u"Token could not be added to the request"))

# Check if this is a redirect and keep a count to prevent endless
# redirects. If redirect_count is not set, init it to 0
Expand Down Expand Up @@ -466,8 +472,7 @@ def put(self, url, callback, *args, **kwargs):

# Add OAuth2 token
if not self.add_token(request):
self.warning_message.emit('Warning',
_(u"Token could not be added to the request"))
warnings.warn(_(u"Token could not be added to the request"))

reply = super(ConnectionManager, self).put(request, data_to_send)
reply.finished.connect(lambda: self.__reply_finished(callback, *args, **kwargs))
Expand Down Expand Up @@ -521,8 +526,7 @@ def delete(self, url, callback, *args, **kwargs):

# Add OAuth2 token
if not self.add_token(request):
self.warning_message.emit('Warning',
_(u"Token could not be added to the request"))
warnings.warn(_(u"Token could not be added to the request"))

# Check if this is a redirect and keep a count to prevent endless
# redirects. If redirect_count is not set, init it to 0
Expand Down
2 changes: 1 addition & 1 deletion QOpenScienceFramework/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"base_url" : "https://accounts.osf.io/",
"api_base_url" : "https://api.osf.io/v2/",
"website_url" : "http://osf.io",
"website_url" : "https://osf.io",
"scope" : ["osf.full_read", "osf.full_write"]
}
43 changes: 24 additions & 19 deletions QOpenScienceFramework/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ def __init__(self, manager, icon_size=None):

self.login_button.setContentsMargins(0, 0, 0, 0)
self.user_button.setContentsMargins(0, 0, 0, 0)
self.setContentsMargins(0, 0, 0, 0)
self.layout().setContentsMargins(0, 0, 0, 0)
self.layout().setSpacing(0)

def current_user(self):
""" Checks the current status of the user.
Expand Down Expand Up @@ -439,7 +439,7 @@ def __create_buttonbar(self):
self.new_folder_button.setDisabled(True)

self.delete_icon = QtGui.QIcon.fromTheme(
'user-trash-symbolic',
'edit-delete',
qta.icon('fa.trash')
)
self.delete_button = QtWidgets.QPushButton(self.delete_icon, _('Delete'))
Expand Down Expand Up @@ -737,7 +737,7 @@ def set_file_properties(self, data):
else:
# Get the ID part of the filter parameter and generate the url
web_id = target.split("=")[1]
web_url = u"https://osf.io/{}".format(web_id)
web_url = u"{}/{}".format(osf.settings['website_url'], web_id)
a = u"<a href=\"{0}\">{0}</a>".format(web_url)
# Set the URL in the field
self.properties["Link"][1].setText(a)
Expand Down Expand Up @@ -1357,8 +1357,10 @@ def __set_collapsed_icon(self,item):
item.setIcon(0,self.get_icon('folder',data['attributes']['name']))
self.expanded_items.discard(data['id'])

def __populate_error(self, reply):
""" Callback for when an error occured while populating the tree. """
def __cleanup_reply(self, reply):
""" Callback for when an error occured while populating the tree, or when
populate_tree finished successfully. Removes the QNetworkReply
from the list of active HTTP operations. """
# Reset active requests after error
try:
self.active_requests.remove(reply)
Expand Down Expand Up @@ -1611,7 +1613,7 @@ def refresh_children_of_node(self, node):
content_url,
self.populate_tree,
node,
errorCallback=self.__populate_error
errorCallback=self.__cleanup_reply
)

# If something went wrong, req should be None
Expand Down Expand Up @@ -1641,7 +1643,7 @@ def refresh_contents(self):
# If not, query the osf for the user data, and pass get_repo_contents
# ass the callback to which the received data should be sent.
self.manager.get_logged_in_user(
self.process_repo_contents, errorCallback=self.__populate_error)
self.process_repo_contents, errorCallback=self.__cleanup_reply)

def add_item(self, parent, data):
if data['type'] == 'nodes':
Expand Down Expand Up @@ -1700,8 +1702,17 @@ def populate_tree(self, reply, parent=None):
parent = self.invisibleRootItem()

for entry in osf_response["data"]:
# Add item to the tree
item, kind = self.add_item(parent, entry)
# Add item to the tree. Check if object hasn't been deleted in the
# meantime
try:
item, kind = self.add_item(parent, entry)
except RuntimeError as e:
# If a runtime error occured the tree was probably reset or
# another event deleted treeWidgetItems. Not much that can be
# done here, so do some cleanup and quit
warnings.warn(e)
self.__cleanup_reply(reply)
return

if kind in ["project","folder"]:
try:
Expand All @@ -1717,7 +1728,7 @@ def populate_tree(self, reply, parent=None):
next_entrypoint,
self.populate_tree,
item,
errorCallback=self.__populate_error
errorCallback=self.__cleanup_reply
)
# If something went wrong, req should be None
if req:
Expand All @@ -1736,20 +1747,14 @@ def populate_tree(self, reply, parent=None):
next_page_url,
self.populate_tree,
parent,
errorCallback=self.__populate_error
errorCallback=self.__cleanup_reply
)
# If something went wrong, req should be None
if req:
self.active_requests.append(req)

# Remove current reply from list of active requests (assuming it finished)
try:
self.active_requests.remove(reply)
except ValueError:
logging.info("Reply not found in active requests")

if not self.active_requests:
self.refreshFinished.emit()
self.__cleanup_reply(reply)

def process_repo_contents(self, logged_in_user):
""" Processes contents for the logged in user. Starts by listing
Expand All @@ -1776,7 +1781,7 @@ def process_repo_contents(self, logged_in_user):
req = self.manager.get(
user_nodes_api_call,
self.populate_tree,
errorCallback=self.__populate_error,
errorCallback=self.__cleanup_reply,
)
# If something went wrong, req should be None
if req:
Expand Down

0 comments on commit 5c6bf34

Please sign in to comment.