<s> from datetime import datetime <EOL> import random <EOL> import copy <EOL> from flask . ext import wtf <EOL> from flask . ext . babel import lazy_gettext as _ <EOL> from google . appengine . ext import ndb <EOL> import flask <EOL> import wtforms <EOL> import auth <EOL> import i18n <EOL> import model <EOL> import util <EOL> import iso <EOL> from main import app <EOL> HOURS = [ ] <EOL> for hour in range ( <NUM_LIT> ) : <EOL> HOURS . append ( ( str ( hour ) , '<STR_LIT>' % hour ) ) <EOL> HOURS [ <NUM_LIT:0> ] = ( '<STR_LIT:0>' , '<STR_LIT>' ) <EOL> class EventUpdateForm ( i18n . Form ) : <EOL> search = wtforms . TextField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] ) <EOL> address = wtforms . TextField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . required ( ) ] ) <EOL> place = wtforms . TextField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . required ( ) ] ) <EOL> country = wtforms . TextField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . required ( ) ] ) <EOL> country_code = wtforms . HiddenField ( '<STR_LIT>' , [ wtforms . validators . required ( ) ] ) <EOL> lat = wtforms . HiddenField ( '<STR_LIT>' , [ wtforms . validators . required ( ) ] ) <EOL> lng = wtforms . HiddenField ( '<STR_LIT>' , [ wtforms . validators . required ( ) ] ) <EOL> timestamp = wtforms . TextField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] ) <EOL> notes = wtforms . TextAreaField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] ) <EOL> layover = wtforms . BooleanField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] ) <EOL> add_more = wtforms . BooleanField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] ) <EOL> home = wtforms . BooleanField ( model . Event . home . _verbose_name , [ wtforms . validators . optional ( ) ] ) <EOL> year = wtforms . SelectField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] ) <EOL> month = wtforms . TextField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] ) <EOL> day = wtforms . TextField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] ) <EOL> hour = wtforms . SelectField ( _ ( '<STR_LIT>' ) , [ wtforms . validators . optional ( ) ] , choices = HOURS ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] , endpoint = '<STR_LIT>' ) <EOL> @ auth . login_required <EOL> def event_create ( event_id = <NUM_LIT:0> ) : <EOL> if event_id == <NUM_LIT:0> : <EOL> event_db = model . Event ( user_key = auth . current_user_key ( ) ) <EOL> else : <EOL> event_db = model . Event . get_by_id ( event_id ) <EOL> if not event_db or event_db . user_key != auth . current_user_key ( ) : <EOL> return flask . abort ( <NUM_LIT> ) <EOL> form = EventUpdateForm ( obj = event_db ) <EOL> today = datetime . utcnow ( ) . date ( ) <EOL> today = datetime ( today . year , today . month , today . day ) <EOL> form . year . choices = sorted ( [ ( str ( y ) , y ) for y in range ( <NUM_LIT> , today . year + <NUM_LIT:1> ) ] , reverse = True ) <EOL> if form . validate_on_submit ( ) : <EOL> form . year . data = int ( form . year . data ) <EOL> form . month . data = int ( form . month . data ) <EOL> try : <EOL> form . day . data = int ( form . day . data ) <EOL> except : <EOL> form . day . data = <NUM_LIT:1> <EOL> success = False <EOL> if not form . year . data : <EOL> form . timestamp . errors . append ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> month = <NUM_LIT:1> <EOL> day = <NUM_LIT:1> <EOL> if form . month . data and form . day . data : <EOL> month = form . month . data <EOL> day = form . day . data <EOL> elif form . month . data : <EOL> month = form . month . data <EOL> day = <NUM_LIT:1> <EOL> else : <EOL> month = <NUM_LIT:1> <EOL> day = <NUM_LIT:1> <EOL> hour = int ( form . hour . data ) or <NUM_LIT:0> <EOL> try : <EOL> form . timestamp . data = datetime ( form . year . data , month , day , hour ) <EOL> success = True <EOL> except : <EOL> form . timestamp . errors . append ( _ ( '<STR_LIT>' ) ) <EOL> success = False <EOL> if success : <EOL> form . populate_obj ( event_db ) <EOL> if form . month . data and form . day . data : <EOL> event_db . accuracy = '<STR_LIT>' <EOL> elif form . month . data : <EOL> event_db . accuracy = '<STR_LIT>' <EOL> else : <EOL> event_db . accuracy = '<STR_LIT>' <EOL> event_db . geo_pt = ndb . GeoPt ( form . lat . data , form . lng . data ) <EOL> event_db . put ( ) <EOL> if event_id : <EOL> flask . flash ( '<STR_LIT>' % event_db . address , category = '<STR_LIT:success>' ) <EOL> else : <EOL> flask . flash ( '<STR_LIT>' % event_db . address , category = '<STR_LIT:success>' ) <EOL> if form . add_more . data : <EOL> timestamp = event_db . timestamp . strftime ( '<STR_LIT>' ) <EOL> if event_db . accuracy == '<STR_LIT>' : <EOL> timestamp = event_db . timestamp . strftime ( '<STR_LIT>' ) <EOL> if event_db . accuracy == '<STR_LIT>' : <EOL> timestamp = event_db . timestamp . strftime ( '<STR_LIT>' ) <EOL> return flask . redirect ( flask . url_for ( '<STR_LIT>' , timestamp = timestamp ) ) <EOL> return flask . redirect ( '<STR_LIT>' % ( flask . url_for ( '<STR_LIT>' ) , event_db . key . id ( ) ) ) <EOL> if not form . errors : <EOL> form . add_more . data = event_id == <NUM_LIT:0> <EOL> form . lat . data = '<STR_LIT:0>' <EOL> form . lng . data = '<STR_LIT:0>' <EOL> if event_db . geo_pt : <EOL> form . lat . data = event_db . geo_pt . lat <EOL> form . lng . data = event_db . geo_pt . lon <EOL> elif flask . request . city_lat_lng : <EOL> form . lat . data = flask . request . city_lat_lng . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:0> ] <EOL> form . lng . data = flask . request . city_lat_lng . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:1> ] <EOL> try : <EOL> if event_id == <NUM_LIT:0> : <EOL> timestamp = util . param ( '<STR_LIT>' ) <EOL> form . year . data = timestamp [ : <NUM_LIT:4> ] . lstrip ( '<STR_LIT:0>' ) <EOL> form . hour . data = timestamp [ - <NUM_LIT:2> : ] . lstrip ( '<STR_LIT:0>' ) <EOL> if len ( timestamp ) == <NUM_LIT:8> : <EOL> form . month . data = timestamp [ <NUM_LIT:4> : - <NUM_LIT:2> ] . lstrip ( '<STR_LIT:0>' ) <EOL> if len ( timestamp ) == <NUM_LIT:10> : <EOL> form . month . data = timestamp [ <NUM_LIT:4> : - <NUM_LIT:4> ] . lstrip ( '<STR_LIT:0>' ) <EOL> form . day . data = timestamp [ <NUM_LIT:6> : - <NUM_LIT:2> ] . lstrip ( '<STR_LIT:0>' ) <EOL> else : <EOL> form . year . data = str ( event_db . timestamp . year ) <EOL> if event_db . accuracy == '<STR_LIT>' : <EOL> form . month . data = str ( event_db . timestamp . month ) <EOL> elif event_db . accuracy == '<STR_LIT>' : <EOL> form . month . data = str ( event_db . timestamp . month ) <EOL> form . day . data = str ( event_db . timestamp . day ) <EOL> form . hour . data = str ( event_db . timestamp . hour ) <EOL> except : <EOL> pass <EOL> return flask . render_template ( <EOL> '<STR_LIT>' , <EOL> html_class = '<STR_LIT>' , <EOL> title = u'<STR_LIT:%s>' % ( _ ( '<STR_LIT>' ) if event_id == <NUM_LIT:0> else '<STR_LIT>' % ( event_db . place , event_db . country ) ) , <EOL> form = form , <EOL> today = today , <EOL> event_db = event_db , <EOL> ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ auth . login_required <EOL> def countries ( username = None ) : <EOL> user_db = auth . current_user_db ( ) <EOL> if username and user_db . username != username : <EOL> if not user_db . admin : <EOL> return flask . abort ( <NUM_LIT> ) <EOL> user_db = model . User . get_by ( '<STR_LIT:username>' , username ) <EOL> if not user_db : <EOL> return flask . abort ( <NUM_LIT> ) <EOL> event_dbs , next_cursor = user_db . get_event_dbs ( <EOL> order = '<STR_LIT>' , limit = - <NUM_LIT:1> <EOL> ) <EOL> country_dbs_ = { } <EOL> country = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> last_event_db = None <EOL> for event_db in event_dbs : <EOL> if event_db . layover : <EOL> continue <EOL> if last_event_db : <EOL> if last_event_db . country_code not in country_dbs_ : <EOL> country_dbs_ [ last_event_db . country_code ] = copy . copy ( country ) <EOL> country_dbs_ [ last_event_db . country_code ] [ '<STR_LIT>' ] = last_event_db . country_code <EOL> country_dbs_ [ last_event_db . country_code ] [ '<STR_LIT>' ] += ( event_db . timestamp - last_event_db . timestamp ) . total_seconds ( ) <EOL> last_event_db = event_db <EOL> if last_event_db : <EOL> if last_event_db . country_code not in country_dbs_ : <EOL> country_dbs_ [ last_event_db . country_code ] = copy . copy ( country ) <EOL> country_dbs_ [ last_event_db . country_code ] [ '<STR_LIT>' ] = last_event_db . country_code <EOL> country_dbs_ [ last_event_db . country_code ] [ '<STR_LIT>' ] += ( datetime . utcnow ( ) - last_event_db . timestamp ) . total_seconds ( ) <EOL> country_dbs_ [ last_event_db . country_code ] [ '<STR_LIT>' ] = True <EOL> country_dbs = [ ] <EOL> total = <NUM_LIT:0> <EOL> for country_code in country_dbs_ : <EOL> country = country_dbs_ [ country_code ] <EOL> country [ '<STR_LIT>' ] = int ( country [ '<STR_LIT>' ] ) <EOL> country [ '<STR_LIT>' ] = int ( country [ '<STR_LIT>' ] / <NUM_LIT> / <NUM_LIT> ) <EOL> country [ '<STR_LIT>' ] = int ( round ( country [ '<STR_LIT>' ] / <NUM_LIT> / <NUM_LIT> / <NUM_LIT> ) ) <EOL> country [ '<STR_LIT>' ] = country [ '<STR_LIT>' ] / <NUM_LIT> <EOL> country [ '<STR_LIT>' ] = country [ '<STR_LIT>' ] / <NUM_LIT> <EOL> country [ '<STR_LIT>' ] = iso . ISO_3166 [ country [ '<STR_LIT>' ] ] <EOL> country_dbs . append ( country ) <EOL> total += country [ '<STR_LIT>' ] <EOL> country_dbs = sorted ( country_dbs , key = lambda c : c [ '<STR_LIT>' ] , reverse = True ) <EOL> return flask . render_template ( <EOL> '<STR_LIT>' , <EOL> html_class = '<STR_LIT>' , <EOL> title = _ ( '<STR_LIT>' ) , <EOL> event_dbs = event_dbs , <EOL> country_dbs = country_dbs , <EOL> total = total , <EOL> next_url = util . generate_next_url ( next_cursor ) , <EOL> user_db = user_db , <EOL> ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ auth . login_required <EOL> def trips ( username = None ) : <EOL> user_db = auth . current_user_db ( ) <EOL> if username and user_db . username != username : <EOL> if not user_db . admin : <EOL> return flask . abort ( <NUM_LIT> ) <EOL> user_db = model . User . get_by ( '<STR_LIT:username>' , username ) <EOL> if not user_db : <EOL> return flask . abort ( <NUM_LIT> ) <EOL> event_dbs , next_cursor = user_db . get_event_dbs ( limit = - <NUM_LIT:1> , order = '<STR_LIT>' ) <EOL> return flask . render_template ( <EOL> '<STR_LIT>' , <EOL> html_class = '<STR_LIT>' , <EOL> title = _ ( '<STR_LIT>' ) , <EOL> event_dbs = event_dbs , <EOL> next_url = util . generate_next_url ( next_cursor ) , <EOL> user_db = user_db , <EOL> ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ auth . login_required <EOL> def places ( username = None ) : <EOL> user_db = auth . current_user_db ( ) <EOL> if username and user_db . username != username : <EOL> if not user_db . admin : <EOL> return flask . abort ( <NUM_LIT> ) <EOL> user_db = model . User . get_by ( '<STR_LIT:username>' , username ) <EOL> if not user_db : <EOL> return flask . abort ( <NUM_LIT> ) <EOL> event_dbs , next_cursor = user_db . get_event_dbs ( <EOL> order = '<STR_LIT>' , limit = - <NUM_LIT:1> <EOL> ) <EOL> place_dbs_ = { } <EOL> country = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> last_event_db = None <EOL> for event_db in event_dbs : <EOL> if event_db . layover : <EOL> continue <EOL> if last_event_db : <EOL> if last_event_db . place not in place_dbs_ : <EOL> place_dbs_ [ last_event_db . place ] = copy . copy ( country ) <EOL> place_dbs_ [ last_event_db . place ] [ '<STR_LIT>' ] = last_event_db . country_code <EOL> place_dbs_ [ last_event_db . place ] [ '<STR_LIT>' ] = last_event_db . place <EOL> place_dbs_ [ last_event_db . place ] [ '<STR_LIT>' ] += ( event_db . timestamp - last_event_db . timestamp ) . total_seconds ( ) <EOL> last_event_db = event_db <EOL> if last_event_db : <EOL> if last_event_db . place not in place_dbs_ : <EOL> place_dbs_ [ last_event_db . place ] = copy . copy ( country ) <EOL> place_dbs_ [ last_event_db . place ] [ '<STR_LIT>' ] = last_event_db . country_code <EOL> place_dbs_ [ last_event_db . place ] [ '<STR_LIT>' ] = last_event_db . place <EOL> place_dbs_ [ last_event_db . place ] [ '<STR_LIT>' ] += ( datetime . utcnow ( ) - last_event_db . timestamp ) . total_seconds ( ) <EOL> place_dbs_ [ last_event_db . place ] [ '<STR_LIT>' ] = True <EOL> place_dbs = [ ] <EOL> total = <NUM_LIT:0> <EOL> for place in place_dbs_ : <EOL> place = place_dbs_ [ place ] <EOL> place [ '<STR_LIT>' ] = int ( place [ '<STR_LIT>' ] ) <EOL> place [ '<STR_LIT>' ] = int ( place [ '<STR_LIT>' ] / <NUM_LIT> / <NUM_LIT> ) <EOL> place [ '<STR_LIT>' ] = int ( round ( place [ '<STR_LIT>' ] / <NUM_LIT> / <NUM_LIT> / <NUM_LIT> ) ) <EOL> place [ '<STR_LIT>' ] = place [ '<STR_LIT>' ] / <NUM_LIT> <EOL> place [ '<STR_LIT>' ] = place [ '<STR_LIT>' ] / <NUM_LIT> <EOL> place [ '<STR_LIT>' ] = iso . ISO_3166 [ place [ '<STR_LIT>' ] ] <EOL> place_dbs . append ( place ) <EOL> total += place [ '<STR_LIT>' ] <EOL> place_dbs = sorted ( place_dbs , key = lambda c : c [ '<STR_LIT>' ] , reverse = True ) <EOL> return flask . render_template ( <EOL> '<STR_LIT>' , <EOL> html_class = '<STR_LIT>' , <EOL> title = _ ( '<STR_LIT>' ) , <EOL> event_dbs = event_dbs , <EOL> place_dbs = place_dbs , <EOL> total = total , <EOL> next_url = util . generate_next_url ( next_cursor ) , <EOL> user_db = user_db , <EOL> ) </s>
