Skip to content

Commit

Permalink
#139 cleaned handlers.py to make pep8 compliant'
Browse files Browse the repository at this point in the history
  • Loading branch information
Castronova committed Oct 26, 2018
1 parent 1bc8ce5 commit 1dcd7ed
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions jupyterhub_rest_server/jupyterhub_rest_server/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -89,43 +98,40 @@ 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)

# 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)

0 comments on commit 1dcd7ed

Please sign in to comment.