Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Refactor - signed in separated out of template context
Browse files Browse the repository at this point in the history
template context is a better name. Stuff for all templates (base and
nav) is in base template context. Routes build there own context. If
signed in they have access to the admin client.
  • Loading branch information
jcbashdown committed Aug 6, 2014
1 parent 0bbdefc commit 248f400
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions admin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,24 @@
def requires_authentication(f):
@wraps(f)
def verify_user_logged_in(*args, **kwargs):
session_context = get_context(session)
if 'user' not in session_context:
if not signed_in(session):
return redirect(url_for('root'))
kwargs['session_context'] = session_context
return f(*args, **kwargs)
else:
admin_client = get_admin_client(session)
kwargs['admin_client'] = admin_client
return f(*args, **kwargs)
return verify_user_logged_in


# this is doing too much or should it have the signon_base_url stuff too
def get_context(session):
context = dict()
def get_admin_client(session):
return AdminAPI(app.config['STAGECRAFT_HOST'],
session['oauth_token']['access_token'])

if 'oauth_user' in session and 'oauth_token' in session:
admin_client = AdminAPI(app.config['STAGECRAFT_HOST'],
session['oauth_token']['access_token'])
context = {
'user': session['oauth_user'],
'data_sets': admin_client.list_data_sets()
}
context['environment'] = environment_dict()

return context
def base_template_context():
return {
'environment': environment_dict()

This comment has been minimized.

Copy link
@robyoung

robyoung Aug 7, 2014

Contributor

Do we still need environment_dict now this is separated out. It seems like over structuring.

}


def environment_dict():
Expand All @@ -54,23 +50,31 @@ def oauth_sign_out():

@app.route("/", methods=['GET'])
def root():
session_context = get_context(session)
if 'user' in get_context(session):
if signed_in(session):
return redirect(url_for('data_sets'))
else:
return render_template('index.html', **session_context)
return render_template('index.html', **base_template_context())


@app.route("/data-sets", methods=['GET'])
@requires_authentication
def data_sets(session_context=None):
return render_template('data_sets.html', **session_context)
def data_sets(admin_client):
template_context = base_template_context()
template_context.update({
'user': session['oauth_user'],
'data_sets': admin_client.list_data_sets()
})
return render_template('data_sets.html', **template_context)


@app.route("/upload-error", methods=['GET'])
@requires_authentication
def upload_error(session_context=None):
return render_template('upload_error.html', **session_context)
def upload_error(admin_client):
template_context = base_template_context()
template_context.update({
'user': session['oauth_user']
})
return render_template('upload_error.html', **template_context)


@app.route("/_status", methods=['GET'])
Expand Down

0 comments on commit 248f400

Please sign in to comment.