<s> from datetime import datetime , timedelta <EOL> from django . contrib . auth . decorators import user_passes_test <EOL> from django . views . generic . simple import direct_to_template <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponse <EOL> from google . appengine . api . labs import taskqueue <EOL> from app . utils import render_plain , safe_int <EOL> from stats . utils import count <EOL> from stats . models import Stats <EOL> registered = { } <EOL> def start ( request ) : <EOL> days = safe_int ( request . GET . get ( "<STR_LIT>" , <NUM_LIT:1> ) ) <EOL> if not days : <EOL> days = <NUM_LIT:1> <EOL> date = ( datetime . today ( ) - timedelta ( days = days ) ) . date ( ) <EOL> create ( date ) <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> def create ( date ) : <EOL> existing = Stats . all ( ) . filter ( "<STR_LIT>" , date ) <EOL> try : <EOL> stats = existing [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> stats = Stats ( ) <EOL> stats . date = date <EOL> data = dict ( [ ( key , None ) for key in registered . keys ( ) ] ) <EOL> stats . set_stats ( data ) <EOL> stats . save ( ) <EOL> for key in registered . keys ( ) : <EOL> taskqueue . add ( url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT:action>" : key , "<STR_LIT>" : stats . id } ) ) <EOL> def get_action ( request , action , pk ) : <EOL> stats = Stats . get ( pk ) <EOL> current = stats . get_stats ( ) <EOL> if action not in registered : <EOL> raise ValueError , "<STR_LIT>" % action <EOL> current [ action ] = registered [ action ] ( stats ) <EOL> stats . set_stats ( current ) <EOL> stats . save ( ) <EOL> return render_plain ( "<STR_LIT>" % current ) <EOL> def get_total ( stats ) : <EOL> return count ( [ "<STR_LIT>" , stats . date ] , ) <EOL> def get_status ( stats ) : <EOL> return { <EOL> "<STR_LIT>" : count ( [ "<STR_LIT>" , "<STR_LIT>" ] , [ "<STR_LIT>" , stats . date ] ) , <EOL> "<STR_LIT>" : count ( [ "<STR_LIT>" , "<STR_LIT>" ] , [ "<STR_LIT>" , stats . date ] ) , <EOL> "<STR_LIT>" : count ( [ "<STR_LIT>" , "<STR_LIT>" ] , [ "<STR_LIT>" , stats . date ] ) , <EOL> } <EOL> registered [ "<STR_LIT>" ] = get_total <EOL> registered [ "<STR_LIT:status>" ] = get_status <EOL> @ user_passes_test ( lambda u : u . is_staff ) <EOL> def view ( request ) : <EOL> stats = Stats . all ( ) . filter ( "<STR_LIT>" , True ) . order ( "<STR_LIT:date>" ) [ : <NUM_LIT:30> ] <EOL> stats = [ { "<STR_LIT:object>" : s , "<STR_LIT>" : s . get_stats ( ) } for s in stats ] <EOL> return direct_to_template ( request , "<STR_LIT>" , { <EOL> "<STR_LIT>" : stats , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" , } <EOL> } ) </s>
