diff --git a/jupyterhub_rest_server/jupyterhub_rest_server/handlers.py b/jupyterhub_rest_server/jupyterhub_rest_server/handlers.py index 546b556..540b93b 100644 --- a/jupyterhub_rest_server/jupyterhub_rest_server/handlers.py +++ b/jupyterhub_rest_server/jupyterhub_rest_server/handlers.py @@ -11,34 +11,39 @@ args = [ - ['husername', - 'HydroShare username. This is used to build an isolated userspace on the JupyterHub server', - 'Yes', - 'Example: TonyCastronova'], - ['resourcetype', - 'The type of HydroShare resource being sent to JupyterHub. Typically used when launching JupyterHub instances from HydroShare', - 'No', - 'Example: GenericResource'], - ['resourceid', - 'The unique id of the a HydroShare resource. Typically used when launching JupyterHub instances from HydroShare', - 'No', - 'Example: 97add6638f7841278c73519e7192b252'], - ['target', - 'Target notebook to launch relative to http://[jupyteraddress]/user/[husername]/tree/. This argument is designed to support applications that wish to provide a more customized user experience.', - 'No', - 'Example: notebooks/Welcome.ipynb'], + ['husername', + 'HydroShare username. This is used to build an ' + 'isolated userspace on the JupyterHub server', + 'Yes', + 'Example: TonyCastronova'], + ['resourcetype', + 'The type of HydroShare resource being sent to JupyterHub. ' + 'Typically used when launching JupyterHub instances from HydroShare', + 'No', + 'Example: GenericResource'], + ['resourceid', + 'The unique id of the a HydroShare resource. ' + 'Typically used when launching JupyterHub instances from HydroShare', + 'No', + 'Example: 97add6638f7841278c73519e7192b252'], + ['target', + 'Target notebook to launch relative to ' + 'http://[jupyteraddress]/user/[husername]/tree/. This argument is ' + 'designed to support applications that wish to provide a ' + 'more customized user experience.', + 'No', + 'Example: notebooks/Welcome.ipynb'], ] header = ['Function', 'Description', 'Required'] class RequestHandler(tornado.web.RequestHandler): -# def __init_(self): errors = [] -# super(RequestHandler, self) def get_or_error(self, argname, strip=True): """ - This function gets a REST input argument or returns an error message if the argument is not found + This function gets a REST input argument or returns an error message + if the argument is not found Arguments: argname -- the name of the argument to get strip -- indicates if the whitespace will be stripped from the argument @@ -55,20 +60,24 @@ def get_arg_value(self, argname, isrequired, strip=True): error = 'Could not find required parameter "%s"' % argname self.errors.append(error) return arg - + def check_for_errors(self): if len(self.errors) > 0: - self.render("index.html", header=header, args=args, error=self.errors) + self.render("index.html", header=header, + args=args, error=self.errors) return 1 else: return 0 class IndexHandler(RequestHandler, tornado.auth.OAuth2Mixin): + def get(self): self.render("index.html", header=header, args=args) + class JupyterHandler(RequestHandler, tornado.auth.OAuth2Mixin): + def get(self): # get arguments from the query string. @@ -89,15 +98,12 @@ def get(self): # build userspace try: msg = '%s -> building userspace' % husername - print(msg) utilities.build_userspace(username) except Exception as e: print('ERROR %s: %s' % (msg, e)) - print('HERE') try: msg = '%s -> writing .env' % husername - print(msg) utilities.set_hydroshare_args(husername, resourceid, resourcetype) except Exception as e: print('ERROR %s: %s' % (msg, e), flush=True) @@ -105,27 +111,27 @@ def get(self): # generate the redirect url baseurl = os.environ['JUPYTER_HUB_IP'] port = os.environ['JUPYTER_PORT'] - - # build the redirect url + + # build the redirect url if port == '443': proto = 'https' port = '' else: proto = 'http' port = ':'+port - + if target is not None: - url = "%s://%s%s/user/%s/tree/%s" % (proto, baseurl, port, username, target) + url = "%s://%s%s/user/%s/tree/%s" % \ + (proto, baseurl, port, username, target) else: - url = "%s://%s%s/user/%s/tree/notebooks/Welcome.ipynb" % (proto, baseurl, port, username) - - print("URL:" + url) + url = "%s://%s%s/user/%s/tree/notebooks/Welcome.ipynb" % \ + (proto, baseurl, port, username) # save the next url to ensure that the redirect will work - p = os.path.join(os.environ['HYDROSHARE_REDIRECT_COOKIE_PATH'], '.redirect_%s' % username) + p = os.path.join(os.environ['HYDROSHARE_REDIRECT_COOKIE_PATH'], + '.redirect_%s' % username) with open(p, 'w') as f: f.write(url) # redirect to the desired page self.redirect(url, status=303) -