<s> import urllib <EOL> from flask import Flask <EOL> from flask import render_template , abort , request , url_for , redirect , jsonify , make_response <EOL> from flask . ext . seasurf import SeaSurf <EOL> from flask . ext . assets import Environment , Bundle <EOL> from flask_debugtoolbar import DebugToolbarExtension <EOL> from markdown import markdown <EOL> from . import __version__ <EOL> from config import CONFIG as cfg <EOL> import db <EOL> from models import Experiment <EOL> from analysis import ExportExperiment <EOL> import utils <EOL> import re <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = cfg . get ( '<STR_LIT>' , False ) <EOL> csrf = SeaSurf ( app ) <EOL> js = Bundle ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> output = "<STR_LIT>" . format ( cfg . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> css = Bundle ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> output = "<STR_LIT>" . format ( cfg . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> assets = Environment ( app ) <EOL> assets . register ( '<STR_LIT>' , js ) <EOL> assets . register ( '<STR_LIT>' , css ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ utils . service_unavailable_on_connection_error <EOL> def status ( ) : <EOL> db . REDIS . ping ( ) <EOL> return utils . json_success ( { '<STR_LIT:version>' : __version__ } , request ) <EOL> @ app . route ( "<STR_LIT:/>" ) <EOL> def hello ( ) : <EOL> experiments = Experiment . all ( exclude_archived = True , redis = db . REDIS ) <EOL> experiments = [ exp . name for exp in experiments ] <EOL> return render_template ( '<STR_LIT>' , experiments = experiments , page = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def archived ( ) : <EOL> experiments = Experiment . all ( exclude_archived = False , redis = db . REDIS ) <EOL> experiments = [ exp . name for exp in experiments if exp . is_archived ( ) ] <EOL> return render_template ( '<STR_LIT>' , experiments = experiments , page = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def experiment_list ( ) : <EOL> experiments = Experiment . all ( redis = db . REDIS ) <EOL> period = determine_period ( ) <EOL> experiments = [ simple_markdown ( exp . objectify_by_period ( period ) ) for exp in experiments ] <EOL> return jsonify ( { '<STR_LIT>' : experiments } ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def details ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> return render_template ( '<STR_LIT>' , experiment = experiment ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def json_details ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> period = determine_period ( ) <EOL> obj = simple_markdown ( experiment . objectify_by_period ( period ) ) <EOL> return jsonify ( obj ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def export ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> export = ExportExperiment ( experiment = experiment ) <EOL> response = make_response ( export ( ) ) <EOL> response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> filename = "<STR_LIT>" . format ( experiment_name ) <EOL> response . headers [ "<STR_LIT>" ] = "<STR_LIT>" . format ( filename ) <EOL> return response <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def set_winner ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> experiment . set_winner ( request . form [ '<STR_LIT>' ] ) <EOL> return redirect ( url_for ( '<STR_LIT>' , experiment_name = experiment . name ) ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def reset_experiment ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> experiment . reset ( ) <EOL> return redirect ( url_for ( '<STR_LIT>' , experiment_name = experiment . name ) ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def reset_winner ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> experiment . reset_winner ( ) <EOL> return redirect ( url_for ( '<STR_LIT>' , experiment_name = experiment . name ) ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def delete_experiment ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> experiment . delete ( ) <EOL> return redirect ( url_for ( '<STR_LIT:hello>' ) ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def toggle_experiment_archive ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> if experiment . is_archived ( ) : <EOL> experiment . unarchive ( ) <EOL> else : <EOL> experiment . archive ( ) <EOL> return redirect ( url_for ( '<STR_LIT>' , experiment_name = experiment . name ) ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def update_experiment_description ( experiment_name ) : <EOL> experiment = find_or_404 ( experiment_name ) <EOL> experiment . update_description ( request . form [ '<STR_LIT:description>' ] ) <EOL> return redirect ( url_for ( '<STR_LIT>' , experiment_name = experiment . name ) ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def favicon ( ) : <EOL> return '<STR_LIT>' <EOL> @ app . errorhandler ( <NUM_LIT> ) <EOL> def page_not_found ( e ) : <EOL> return render_template ( '<STR_LIT>' ) , <NUM_LIT> <EOL> @ app . errorhandler ( <NUM_LIT> ) <EOL> def internal_server_error ( e ) : <EOL> return render_template ( '<STR_LIT>' ) , <NUM_LIT> <EOL> def find_or_404 ( experiment_name ) : <EOL> try : <EOL> experiment_name = url = urllib . unquote ( experiment_name ) . decode ( '<STR_LIT:utf8>' ) <EOL> exp = Experiment . find ( experiment_name , db . REDIS ) <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> exp . set_kpi ( request . args . get ( '<STR_LIT>' ) ) <EOL> return exp <EOL> except ValueError : <EOL> abort ( <NUM_LIT> ) <EOL> def determine_period ( ) : <EOL> period = request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if period not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> err = { '<STR_LIT:error>' : '<STR_LIT>' . format ( period ) , '<STR_LIT:status>' : <NUM_LIT> } <EOL> abort ( <NUM_LIT> , jsonify ( err ) ) <EOL> return period <EOL> def simple_markdown ( experiment ) : <EOL> description = experiment [ '<STR_LIT:description>' ] <EOL> if description and description != '<STR_LIT>' : <EOL> experiment [ '<STR_LIT>' ] = markdown ( description ) <EOL> return experiment <EOL> app . secret_key = cfg . get ( '<STR_LIT>' ) <EOL> app . jinja_env . filters [ '<STR_LIT>' ] = utils . number_to_percent <EOL> app . jinja_env . filters [ '<STR_LIT>' ] = utils . number_format <EOL> app . jinja_env . filters [ '<STR_LIT>' ] = utils . sanitize_experiment <EOL> app . jinja_env . filters [ '<STR_LIT>' ] = utils . regex_replace <EOL> toolbar = DebugToolbarExtension ( app ) <EOL> def start ( environ , start_response ) : <EOL> return app ( environ , start_response ) </s>
