<s> from flask import render_template , send_from_directory , request , abort , g , redirect <EOL> from flask . ext . login import user_logged_in , current_user <EOL> from flask . ext . migrate import Migrate <EOL> from flask . ext . security import Security , SQLAlchemyUserDatastore <EOL> from flask_mail import Mail <EOL> from admin import admin <EOL> from db import User , Role , Admiral <EOL> from forms import * <EOL> from util import generate_api_token <EOL> import datetime <EOL> import logging <EOL> modules = { <EOL> "<STR_LIT>" : None , "<STR_LIT>" : None , "<STR_LIT>" : None <EOL> } <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def init ( app ) : <EOL> app . jinja_env . globals . update ( __builtins__ = __builtins__ ) <EOL> app . config [ "<STR_LIT>" ] = "<STR_LIT>" . format ( app . config [ "<STR_LIT>" ] , app . config [ "<STR_LIT>" ] , <EOL> app . config [ "<STR_LIT>" ] , app . config [ "<STR_LIT>" ] , <EOL> app . config [ "<STR_LIT>" ] ) <EOL> db . init_app ( app ) <EOL> admin . init_app ( app ) <EOL> mail = Mail ( ) <EOL> mail . init_app ( app ) <EOL> modules [ "<STR_LIT>" ] = Migrate ( app , db ) <EOL> modules [ "<STR_LIT>" ] = SQLAlchemyUserDatastore ( db , User , Role ) <EOL> modules [ "<STR_LIT>" ] = Security ( app , modules [ "<STR_LIT>" ] , confirm_register_form = MyRegisterForm ) <EOL> def admiral_load ( ) : <EOL> api_token = request . values . get ( '<STR_LIT>' , None ) <EOL> if api_token is None : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> abort ( <NUM_LIT> ) <EOL> user = db . session . query ( User ) . filter ( User . api_token == api_token ) . first ( ) <EOL> if user is None : <EOL> logger . warning ( "<STR_LIT>" . format ( api_token ) ) <EOL> abort ( <NUM_LIT> ) <EOL> g . admiral = user . admiral if user . admiral else Admiral ( ) . create ( user ) <EOL> curr_t = datetime . datetime . utcnow ( ) <EOL> lastaction = g . admiral . last_action <EOL> if not lastaction : <EOL> lastaction = curr_t <EOL> delta = ( curr_t - lastaction ) . total_seconds ( ) / <NUM_LIT> <EOL> if delta < <NUM_LIT:0> : <EOL> logger . warn ( "<STR_LIT>" . format ( user . nickname ) ) <EOL> return <EOL> delta = int ( delta ) <EOL> g . admiral . resources . add ( * ( ( delta * <NUM_LIT:3> ) for _ in range ( <NUM_LIT:3> ) ) , baux = delta * <NUM_LIT:2> ) <EOL> g . admiral . last_action = curr_t <EOL> db . session . add ( g . admiral ) <EOL> db . session . commit ( ) <EOL> from kancolle . api import api_actions <EOL> api_actions . before_request ( admiral_load ) <EOL> app . register_blueprint ( api_actions , url_prefix = '<STR_LIT>' ) <EOL> from kancolle . api import api_member <EOL> api_member . before_request ( admiral_load ) <EOL> app . register_blueprint ( api_member , url_prefix = '<STR_LIT>' ) <EOL> from kancolle . api import api_init <EOL> api_init . before_request ( admiral_load ) <EOL> app . register_blueprint ( api_init , url_prefix = '<STR_LIT>' ) <EOL> from kancolle . api import api_user <EOL> api_user . before_request ( admiral_load ) <EOL> app . register_blueprint ( api_user , url_prefix = '<STR_LIT>' ) <EOL> from kancolle . api import api_mission <EOL> api_mission . before_request ( admiral_load ) <EOL> app . register_blueprint ( api_mission , url_prefix = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def p_index ( ) : <EOL> if hasattr ( current_user , '<STR_LIT>' ) : <EOL> api_token = current_user . api_token <EOL> return render_template ( '<STR_LIT>' , api_token = api_token ) <EOL> else : <EOL> return redirect ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def kcs ( path ) : <EOL> return send_from_directory ( '<STR_LIT>' , path ) <EOL> @ user_logged_in . connect_via ( app ) <EOL> def u_logged_in ( sender , user ) : <EOL> """<STR_LIT>""" <EOL> user . api_token = generate_api_token ( ) </s>
