<s> from __future__ import print_function , unicode_literals <EOL> import traceback <EOL> from flask import render_template , request , flash , redirect , url_for , jsonify , g <EOL> from weblab . core . login . exc import InvalidCredentialsError <EOL> from weblab . core . webclient . helpers import safe_redirect , WebError , json_exc , web_exc <EOL> from weblab . core . webclient . view_labs import labs as labs_view <EOL> from weblab . core . i18n import gettext <EOL> from weblab . core . wl import weblab_api <EOL> from weblab . core . exc import SessionNotFoundError <EOL> @ weblab_api . route_webclient ( "<STR_LIT>" , methods = [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] ) <EOL> def login ( ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:POST>" : <EOL> return handle_login_POST ( ) <EOL> return handle_login_GET ( ) <EOL> @ weblab_api . route_webclient ( "<STR_LIT>" , methods = [ "<STR_LIT:POST>" ] ) <EOL> @ json_exc <EOL> def login_service ( ) : <EOL> contents = request . get_json ( force = True , silent = True ) <EOL> if contents == False or not isinstance ( contents , dict ) : <EOL> return weblab_api . jsonify ( error = True , message = "<STR_LIT>" ) <EOL> username = contents . get ( '<STR_LIT:username>' , '<STR_LIT>' ) <EOL> password = contents . get ( '<STR_LIT:password>' , '<STR_LIT>' ) <EOL> try : <EOL> session_id = weblab_api . api . login ( username , password ) <EOL> except InvalidCredentialsError : <EOL> return weblab_api . jsonify ( error = True , message = gettext ( "<STR_LIT>" ) ) <EOL> return weblab_api . jsonify ( error = False , redirect = get_next_url ( ) ) <EOL> def handle_login_POST ( ) : <EOL> """<STR_LIT>""" <EOL> username = request . values . get ( "<STR_LIT:username>" ) <EOL> password = request . values . get ( "<STR_LIT:password>" ) <EOL> try : <EOL> session_id = weblab_api . api . login ( username , password ) <EOL> except InvalidCredentialsError : <EOL> flash ( gettext ( "<STR_LIT>" ) , category = "<STR_LIT:error>" ) <EOL> return redirect ( url_for ( "<STR_LIT>" , _external = True , _scheme = request . scheme ) ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> flash ( gettext ( "<STR_LIT>" ) , <NUM_LIT> ) <EOL> return weblab_api . make_response ( gettext ( "<STR_LIT>" ) , <NUM_LIT> ) <EOL> else : <EOL> return weblab_api . make_response ( redirect ( get_next_url ( ) ) ) <EOL> def handle_login_GET ( ) : <EOL> """<STR_LIT>""" <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> url_kwargs = dict ( next = request . args . get ( '<STR_LIT>' ) ) <EOL> else : <EOL> url_kwargs = { } <EOL> try : <EOL> weblab_api . api . check_user_session ( ) <EOL> except SessionNotFoundError : <EOL> pass <EOL> else : <EOL> return redirect ( get_next_url ( ) ) <EOL> return render_template ( "<STR_LIT>" , url_kwargs = url_kwargs ) <EOL> @ weblab_api . route_webclient ( "<STR_LIT>" , methods = [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] ) <EOL> def logout ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> weblab_api . api . logout ( ) <EOL> except SessionNotFoundError : <EOL> pass <EOL> return redirect ( url_for ( "<STR_LIT>" , _external = True , _scheme = request . scheme ) ) <EOL> @ weblab_api . route_webclient ( "<STR_LIT>" ) <EOL> def demo ( ) : <EOL> if not weblab_api . config . client . get ( '<STR_LIT>' ) : <EOL> return "<STR_LIT>" , <NUM_LIT> <EOL> username = weblab_api . config . client . get ( '<STR_LIT>' ) <EOL> password = weblab_api . config . client . get ( '<STR_LIT>' ) <EOL> if username is None or password is None : <EOL> return "<STR_LIT>" <EOL> try : <EOL> session_id = weblab_api . api . login ( username , password ) <EOL> except InvalidCredentialsError : <EOL> return "<STR_LIT>" <EOL> if hasattr ( g , '<STR_LIT>' ) or request . args . get ( '<STR_LIT>' ) : <EOL> return weblab_api . make_response ( redirect ( get_next_url ( ) ) ) <EOL> return labs_view ( ) <EOL> def get_next_url ( ) : <EOL> if hasattr ( g , '<STR_LIT>' ) : <EOL> next_url = g . next_url <EOL> else : <EOL> next_url = request . args . get ( "<STR_LIT>" ) <EOL> next_url = safe_redirect ( next_url ) <EOL> return next_url or url_for ( "<STR_LIT>" , _external = True , _scheme = request . scheme ) </s>
