<s> import os <EOL> from django . utils import simplejson <EOL> from google . appengine . ext import webapp , db <EOL> from google . appengine . ext . webapp import template <EOL> from google . appengine . ext . webapp . util import run_wsgi_app <EOL> from google . appengine . api import users , prospective_search , memcache <EOL> from datetime import datetime <EOL> import time <EOL> import auth <EOL> import models <EOL> import channels <EOL> import stats <EOL> import timestamp <EOL> import logging <EOL> class ConnectedPage ( webapp . RequestHandler ) : <EOL> def post ( self , name = "<STR_LIT>" ) : <EOL> self . response . headers . add_header ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> user = auth . getCurrentUser ( ) <EOL> device = None <EOL> if user : <EOL> try : <EOL> user_data = models . getUser ( user ) <EOL> except models . UserDoesNotExistError : <EOL> user_data = models . UserData ( user = user ) . save ( ) <EOL> try : <EOL> device = models . getDevice ( "<STR_LIT>" % ( user . email ( ) , name ) ) <EOL> except : <EOL> device = models . DeviceData ( user = user_data , name = name ) . save ( ) <EOL> last_links = models . getUnreadLinks ( device ) <EOL> channel = channels . Channel ( device . address , False ) <EOL> for link in last_links : <EOL> channel . queueLink ( link ) <EOL> channel . send ( ) <EOL> stats . record ( "<STR_LIT>" , <EOL> simplejson . dumps ( { "<STR_LIT:user>" : user . email ( ) } ) ) <EOL> self . response . out . write ( device . address ) <EOL> class MainPage ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> user = auth . getCurrentUser ( ) <EOL> name = "<STR_LIT>" <EOL> if user : <EOL> try : <EOL> user_data = models . getUser ( user ) <EOL> except models . UserDoesNotExistError : <EOL> user_data = models . UserData ( user = user ) . save ( ) <EOL> try : <EOL> device = models . getDevice ( "<STR_LIT>" % ( user . email ( ) , name ) ) <EOL> except models . DeviceDoesNotExistError : <EOL> device = models . DeviceData ( user = user_data , name = name ) . save ( ) <EOL> over_quota = False <EOL> try : <EOL> channel = channels . Channel ( device . address , <EOL> override_quota = user_data . immune ( ) ) <EOL> channel_token = channel . token <EOL> except channels . OverQuotaError : <EOL> over_quota = True <EOL> channel_token = '<STR_LIT>' <EOL> template_values = { <EOL> '<STR_LIT>' : channel_token , <EOL> '<STR_LIT>' : device . address , <EOL> '<STR_LIT>' : device . name , <EOL> '<STR_LIT>' : user_data . getDevices ( ) , <EOL> '<STR_LIT>' : over_quota <EOL> } <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' ) <EOL> self . response . out . write ( template . render ( path , template_values ) ) <EOL> else : <EOL> self . redirect ( users . create_login_url ( self . request . uri ) ) <EOL> class AddLinkPage ( webapp . RequestHandler ) : <EOL> def post ( self ) : <EOL> self . response . headers . add_header ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> user = auth . getCurrentUser ( ) <EOL> response = { } <EOL> if user : <EOL> try : <EOL> user_data = models . getUser ( user ) <EOL> except models . UserDoesNotExistError : <EOL> user_data = models . UserData ( user = user ) . save ( ) <EOL> name = self . request . get ( '<STR_LIT:name>' ) <EOL> if not name : <EOL> name = "<STR_LIT>" <EOL> try : <EOL> device = models . getDevice ( "<STR_LIT>" % ( user . email ( ) , name ) ) <EOL> except models . DeviceDoesNotExistError : <EOL> device = models . DeviceData ( name = name , user = user_data ) . save ( ) <EOL> receiver = None <EOL> if self . request . get ( "<STR_LIT>" ) : <EOL> try : <EOL> receiver = models . getDevice ( "<STR_LIT>" % ( user . email ( ) , <EOL> self . request . get ( "<STR_LIT>" ) ) ) <EOL> except models . DeviceDoesNotExistError : <EOL> receiver = models . DeviceData ( <EOL> name = self . request . get ( "<STR_LIT>" ) , <EOL> user = user_data ) . save ( ) <EOL> if receiver == None : <EOL> receiver = device <EOL> link = models . LinkData ( url = self . request . get ( '<STR_LIT>' ) , <EOL> sender = device , receiver = receiver ) . save ( ) <EOL> if models . getQuota ( ) . amount >= models . getStats ( <EOL> '<STR_LIT>' ) . count or user_data . immune ( ) : <EOL> channel = channels . Channel ( receiver . address , False ) <EOL> channel . sendLink ( link ) <EOL> response [ '<STR_LIT:code>' ] = <NUM_LIT:200> <EOL> response [ '<STR_LIT>' ] = link . url <EOL> else : <EOL> response [ '<STR_LIT:code>' ] = <NUM_LIT> <EOL> response [ '<STR_LIT>' ] = link . url <EOL> else : <EOL> response [ '<STR_LIT:code>' ] = <NUM_LIT> <EOL> response [ '<STR_LIT>' ] = self . request . get ( '<STR_LIT>' ) <EOL> self . response . out . write ( simplejson . dumps ( response ) ) <EOL> class TokenPage ( webapp . RequestHandler ) : <EOL> def get ( self , name = False ) : <EOL> self . response . headers . add_header ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> user = auth . getCurrentUser ( ) <EOL> response = { } <EOL> if user : <EOL> try : <EOL> user_data = models . getUser ( user ) <EOL> except models . UserDoesNotExistError : <EOL> user_data = models . UserData ( user = user ) . save ( ) <EOL> if not name : <EOL> name = "<STR_LIT>" <EOL> try : <EOL> device = models . getDevice ( "<STR_LIT>" % ( user . email ( ) , name ) ) <EOL> except models . DeviceDoesNotExistError : <EOL> device = models . DeviceData ( user = user_data , name = name ) . save ( ) <EOL> try : <EOL> channel = channels . Channel ( device . address , <EOL> override_quota = user_data . immune ( ) ) <EOL> response [ '<STR_LIT>' ] = channel . token <EOL> if channel . cached : <EOL> response [ '<STR_LIT:code>' ] = <NUM_LIT> <EOL> else : <EOL> response [ '<STR_LIT:code>' ] = <NUM_LIT:200> <EOL> except channels . OverQuotaError : <EOL> response [ '<STR_LIT:code>' ] = <NUM_LIT> <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> response [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> else : <EOL> response [ '<STR_LIT:code>' ] = <NUM_LIT> <EOL> response [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> self . response . out . write ( simplejson . dumps ( response ) ) <EOL> class MarkAsReadHandler ( webapp . RequestHandler ) : <EOL> def post ( self , json = False ) : <EOL> self . response . headers . add_header ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> json = self . request . get ( '<STR_LIT>' ) <EOL> if json != False : <EOL> sent_data = simplejson . loads ( json ) <EOL> for link in sent_data : <EOL> link_data = models . getLink ( link ) <EOL> link_data . markRead ( ) <EOL> class SetQuotaHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> quota = models . getQuota ( ) <EOL> logging . info ( "<STR_LIT>" % quota ) <EOL> cur_quota = <NUM_LIT:0> <EOL> if type ( quota ) == type ( db . Key ( ) ) : <EOL> cur_quota = quota . amount <EOL> vars = { <EOL> '<STR_LIT>' : cur_quota <EOL> } <EOL> logging . debug ( quota . amount ) <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> self . response . out . write ( template . render ( path , vars ) ) <EOL> def post ( self ) : <EOL> quota = self . request . POST [ '<STR_LIT>' ] <EOL> newQuota = models . updateQuota ( quota ) <EOL> self . response . out . write ( "<STR_LIT>" % <EOL> newQuota . amount ) <EOL> class SubscribeHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> self . response . out . write ( template . render ( path , { } ) ) <EOL> def post ( self ) : <EOL> event = self . request . POST [ '<STR_LIT>' ] <EOL> datapoint = self . request . POST [ '<STR_LIT>' ] <EOL> subscription_id = models . StatsSubscription ( event = event , <EOL> datapoint = datapoint ) . put ( ) <EOL> prospective_search . subscribe ( stats . StatsRecord , '<STR_LIT>' % event , <EOL> subscription_id ) <EOL> self . response . out . write ( "<STR_LIT>" % <EOL> ( datapoint , event ) ) <EOL> class StatsHandler ( webapp . RequestHandler ) : <EOL> def post ( self ) : <EOL> record = prospective_search . get_document ( self . request ) <EOL> record_value = simplejson . loads ( record . value ) <EOL> subscriber_keys = map ( db . Key , self . request . get_all ( '<STR_LIT:id>' ) ) <EOL> subscribers = db . get ( subscriber_keys ) <EOL> datapoints = [ ] <EOL> stats_json = [ ] <EOL> for subscriber_key , subscriber in zip ( subscriber_keys , subscribers ) : <EOL> if not subscriber : <EOL> prospective_search . unsubscribe ( stats . StatsRecord , <EOL> subscriber_key ) <EOL> else : <EOL> datapoints . append ( models . getStats ( subscriber . datapoint , <EOL> record . timestamp , duration = '<STR_LIT>' ) ) <EOL> datapoints . append ( models . getStats ( subscriber . datapoint , <EOL> record . timestamp , duration = '<STR_LIT>' ) ) <EOL> for datapoint in datapoints : <EOL> if datapoint . datapoint == '<STR_LIT>' : <EOL> try : <EOL> user = models . getUser ( record_value [ '<STR_LIT:user>' ] , False ) <EOL> except models . UserDoesNotExistError : <EOL> continue <EOL> last_seen = user . last_seen <EOL> new_last_seen = timestamp . now ( ) <EOL> if datapoint . duration == "<STR_LIT>" : <EOL> last_seen = last_seen . replace ( hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , <EOL> microsecond = <NUM_LIT:0> ) <EOL> new_last_seen = new_last_seen . replace ( hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , <EOL> second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> if last_seen < new_last_seen : <EOL> user . updateLastSeen ( new_last_seen ) <EOL> user . save ( ) <EOL> else : <EOL> continue <EOL> if datapoint . datapoint == '<STR_LIT>' : <EOL> datapoint . count = models . getQuota ( ) . amount <EOL> else : <EOL> datapoint . increment ( ) <EOL> json = { '<STR_LIT>' : datapoint . datapoint , '<STR_LIT:value>' : datapoint . count , <EOL> '<STR_LIT:date>' : datapoint . date . strftime ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : int ( time . mktime ( <EOL> datapoint . date . date ( ) . timetuple ( ) ) ) * <NUM_LIT:1000> , <EOL> '<STR_LIT>' : datapoint . date . hour } <EOL> if datapoint . duration == "<STR_LIT>" : <EOL> json [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> stats_json . append ( json ) <EOL> db . put ( datapoints ) <EOL> push = channels . Channel ( "<STR_LIT>" , False ) <EOL> push . message = { "<STR_LIT>" : stats_json } <EOL> push . send ( ) <EOL> logging . debug ( simplejson . dumps ( stats_json ) ) <EOL> class StatsDashboard ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> user = users . User ( '<STR_LIT>' ) <EOL> try : <EOL> user_data = models . getUser ( user ) <EOL> except models . UserDoesNotExistError : <EOL> user_data = models . UserData ( user = user ) . save ( ) <EOL> try : <EOL> device = models . getDevice ( "<STR_LIT>" % ( user . email ( ) , "<STR_LIT>" ) ) <EOL> except models . DeviceDoesNotExistError : <EOL> device = models . DeviceData ( user = user_data , <EOL> name = "<STR_LIT>" ) . save ( ) <EOL> channel = channels . Channel ( device . address , override_quota = True ) <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> stats_data = models . StatsData . all ( ) . order ( "<STR_LIT>" ) . fetch ( <NUM_LIT:1000> ) <EOL> template_values = { <EOL> '<STR_LIT>' : channel . token , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> stats = template_values [ '<STR_LIT>' ] <EOL> for datapoint in stats_data : <EOL> datestamp = int ( time . mktime ( <EOL> datapoint . date . date ( ) . timetuple ( ) ) * <NUM_LIT:1000> ) <EOL> hour = datapoint . date . hour <EOL> if datestamp not in stats : <EOL> stats [ datestamp ] = { <EOL> '<STR_LIT>' : datestamp , <EOL> '<STR_LIT:date>' : datapoint . date , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> if datapoint . datapoint not in stats [ datestamp ] [ '<STR_LIT>' ] : <EOL> stats [ datestamp ] [ '<STR_LIT>' ] [ datapoint . datapoint ] = { <EOL> '<STR_LIT>' : datapoint . datapoint , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> point_id = "<STR_LIT:%s>" % hour <EOL> if datapoint . duration == "<STR_LIT>" : <EOL> point_id = "<STR_LIT>" <EOL> dp = datapoint . datapoint <EOL> stats [ datestamp ] [ '<STR_LIT>' ] [ dp ] [ '<STR_LIT>' ] [ point_id ] = { <EOL> '<STR_LIT>' : datapoint . datapoint , <EOL> '<STR_LIT>' : datestamp , <EOL> '<STR_LIT>' : hour , <EOL> '<STR_LIT:count>' : int ( datapoint . count ) , <EOL> '<STR_LIT>' : datapoint . duration <EOL> } <EOL> template_values [ '<STR_LIT>' ] = stats <EOL> logging . info ( template_values ) <EOL> self . response . out . write ( template . render ( path , template_values ) ) <EOL> class StatsInit ( webapp . RequestHandler ) : <EOL> def get ( self , duration = '<STR_LIT>' ) : <EOL> datapoints = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for datapoint in datapoints : <EOL> datapoint = models . getStats ( datapoint , duration = duration ) <EOL> class QuotaCountdown ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> stamp = timestamp . now ( ) <EOL> reset = stamp . replace ( day = stamp . day + <NUM_LIT:1> , hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , <EOL> microsecond = <NUM_LIT:0> ) <EOL> countdown = reset - stamp <EOL> response = { } <EOL> response [ '<STR_LIT>' ] = str ( countdown ) <EOL> response [ '<STR_LIT>' ] = countdown . seconds + ( countdown . days * <NUM_LIT> * <NUM_LIT> ) <EOL> self . response . out . write ( simplejson . dumps ( response ) ) <EOL> class PaymentNotificationHandler ( webapp . RequestHandler ) : <EOL> def post ( self ) : <EOL> user = auth . getCurrentUser ( ) <EOL> response = { } <EOL> if user : <EOL> try : <EOL> user_data = models . getUser ( user ) <EOL> except models . UserDoesNotExistError : <EOL> user_data = models . UserData ( user = user ) . save ( ) <EOL> payment_data = models . PaymentData ( date = timestamp . now ( ) , <EOL> user = user_data , item = self . request . get ( "<STR_LIT>" ) , <EOL> order_number = self . request . get ( "<STR_LIT>" ) , <EOL> status = "<STR_LIT>" ) <EOL> payment_data . save ( ) <EOL> class CheckTimeHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> self . response . out . write ( simplejson . dumps ( { <EOL> "<STR_LIT:code>" : <NUM_LIT:200> , <EOL> "<STR_LIT>" : int ( time . mktime ( datetime . now ( ) . timetuple ( ) ) ) * <NUM_LIT:1000> <EOL> } ) ) <EOL> class ClearCacheHandler ( webapp . RequestHandler ) : <EOL> def get ( self ) : <EOL> memcache . flush_all ( ) <EOL> self . response . out . write ( "<STR_LIT>" ) <EOL> application = webapp . WSGIApplication ( [ <EOL> ( '<STR_LIT:/>' , MainPage ) , <EOL> ( '<STR_LIT>' , AddLinkPage ) , <EOL> ( '<STR_LIT>' , MarkAsReadHandler ) , <EOL> ( '<STR_LIT>' , ConnectedPage ) , <EOL> ( '<STR_LIT>' , ConnectedPage ) , <EOL> ( '<STR_LIT>' , TokenPage ) , <EOL> ( '<STR_LIT>' , TokenPage ) , <EOL> ( '<STR_LIT>' , SubscribeHandler ) , <EOL> ( '<STR_LIT>' , StatsDashboard ) , <EOL> ( '<STR_LIT>' , StatsInit ) , <EOL> ( '<STR_LIT>' , QuotaCountdown ) , <EOL> ( '<STR_LIT>' , SetQuotaHandler ) , <EOL> ( '<STR_LIT>' , StatsHandler ) , <EOL> ( '<STR_LIT>' , PaymentNotificationHandler ) , <EOL> ( '<STR_LIT>' , ClearCacheHandler ) , <EOL> ( '<STR_LIT>' , CheckTimeHandler ) <EOL> ] , debug = True ) <EOL> def main ( ) : <EOL> run_wsgi_app ( application ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
