<s> from routes import route <EOL> import platform <EOL> import tornado . web <EOL> import time <EOL> from constants import DEVICE_TYPE_IOS , VERSION <EOL> from pymongo import DESCENDING <EOL> from util import * <EOL> from tornado . options import options <EOL> import sys <EOL> def buildUpdateFields ( params ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:U+002C>" . join ( [ "<STR_LIT>" % ( k , v ) for k , v in params . items ( ) ] ) <EOL> def normalize_tokens ( tokens ) : <EOL> for token in tokens : <EOL> if not '<STR_LIT>' in token : <EOL> token [ '<STR_LIT>' ] = DEVICE_TYPE_IOS <EOL> return tokens <EOL> class WebBaseHandler ( tornado . web . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( self ) : <EOL> pass <EOL> def prepare ( self ) : <EOL> pass <EOL> @ property <EOL> def dbname ( self ) : <EOL> """<STR_LIT>""" <EOL> return options . appprefix + self . appname <EOL> @ property <EOL> def db ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . application . mongodb [ self . dbname ] <EOL> @ property <EOL> def mongodbconnection ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . application . mongodb <EOL> @ property <EOL> def masterdb ( self ) : <EOL> return self . application . masterdb <EOL> @ property <EOL> def apnsconnections ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . application . services [ '<STR_LIT>' ] <EOL> @ property <EOL> def gcmconnections ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . application . services [ '<STR_LIT>' ] <EOL> @ property <EOL> def wnsconnections ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . application . services [ '<STR_LIT>' ] <EOL> @ property <EOL> def mpnsconnections ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . application . services [ '<STR_LIT>' ] <EOL> @ property <EOL> def currentuser ( self ) : <EOL> return self . get_current_user ( ) <EOL> def get_current_user ( self ) : <EOL> """<STR_LIT>""" <EOL> userid = self . get_secure_cookie ( '<STR_LIT:user>' ) <EOL> if not userid : <EOL> return None <EOL> userId = ObjectId ( userid ) <EOL> user = self . masterdb . managers . find_one ( { '<STR_LIT>' : userId } ) <EOL> return user <EOL> def render_string ( self , template_name , ** kwargs ) : <EOL> apps = self . masterdb . applications . find ( ) <EOL> kwargs [ "<STR_LIT>" ] = apps <EOL> return super ( WebBaseHandler , self ) . render_string ( template_name , ** kwargs ) <EOL> @ route ( r"<STR_LIT:/>" ) <EOL> class MainHandler ( WebBaseHandler ) : <EOL> """<STR_LIT>""" <EOL> @ tornado . web . authenticated <EOL> def get ( self ) : <EOL> self . redirect ( r"<STR_LIT>" ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class AppDeletionHandler ( WebBaseHandler ) : <EOL> @ tornado . web . authenticated <EOL> def get ( self , appname ) : <EOL> self . appname = appname <EOL> app = self . masterdb . applications . find_one ( { '<STR_LIT>' : appname } ) <EOL> if not app : raise tornado . web . HTTPError ( <NUM_LIT> ) <EOL> self . render ( "<STR_LIT>" , app = app ) <EOL> @ tornado . web . authenticated <EOL> def post ( self , appname ) : <EOL> self . appname = appname <EOL> app = self . masterdb . applications . find_one ( { '<STR_LIT>' : appname } ) <EOL> if not app : raise tornado . web . HTTPError ( <NUM_LIT> ) <EOL> self . masterdb . applications . remove ( { '<STR_LIT>' : appname } , safe = True ) <EOL> self . mongodbconnection . drop_database ( appname ) <EOL> self . redirect ( r"<STR_LIT>" ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class AppLogViewHandler ( WebBaseHandler ) : <EOL> @ tornado . web . authenticated <EOL> def get ( self , appname ) : <EOL> self . appname = appname <EOL> app = self . masterdb . applications . find_one ( { '<STR_LIT>' : appname } ) <EOL> if not app : raise tornado . web . HTTPError ( <NUM_LIT> ) <EOL> page = self . get_argument ( '<STR_LIT>' , None ) <EOL> perpage = <NUM_LIT:50> <EOL> if page : <EOL> logs = self . db . logs . find ( ) . sort ( '<STR_LIT>' , DESCENDING ) . skip ( int ( page ) * perpage ) . limit ( perpage ) <EOL> else : <EOL> page = <NUM_LIT:0> <EOL> logs = self . db . logs . find ( ) . sort ( '<STR_LIT>' , DESCENDING ) . limit ( perpage ) <EOL> self . render ( "<STR_LIT>" , app = app , logs = logs , page = int ( page ) ) <EOL> def post ( self , appname ) : <EOL> self . appname = appname <EOL> now = int ( time . time ( ) ) <EOL> thirtydaysago = now - <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> self . db . logs . remove ( { '<STR_LIT>' : { '<STR_LIT>' : thirtydaysago } } ) <EOL> self . redirect ( r"<STR_LIT>" % appname ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class AppObjectsHandler ( WebBaseHandler ) : <EOL> @ tornado . web . authenticated <EOL> def get ( self , appname ) : <EOL> self . appname = appname <EOL> app = self . masterdb . applications . find_one ( { '<STR_LIT>' : appname } ) <EOL> if not app : raise tornado . web . HTTPError ( <NUM_LIT> ) <EOL> objects = self . db . objects . find ( ) <EOL> self . render ( "<STR_LIT>" , app = app , objects = objects ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class AppHandler ( WebBaseHandler ) : <EOL> '''<STR_LIT>''' <EOL> @ tornado . web . authenticated <EOL> def get ( self , appname ) : <EOL> self . redirect ( r"<STR_LIT>" % appname ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class AppsListHandler ( WebBaseHandler ) : <EOL> @ tornado . web . authenticated <EOL> def get ( self ) : <EOL> version_object = self . masterdb [ '<STR_LIT>' ] . find_one ( { '<STR_LIT:name>' : '<STR_LIT:version>' } ) <EOL> outdated = False <EOL> if int ( version_object [ '<STR_LIT:value>' ] ) < int ( VERSION ) : <EOL> outdated = True <EOL> apps = self . masterdb . applications . find ( ) <EOL> self . render ( '<STR_LIT>' , apps = apps , outdated = outdated ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class StatsHandler ( WebBaseHandler ) : <EOL> @ tornado . web . authenticated <EOL> def get ( self ) : <EOL> records = self . masterdb . applications . find ( ) <EOL> self . render ( '<STR_LIT>' , apns = self . apnsconnections ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class InfoHandler ( WebBaseHandler ) : <EOL> @ tornado . web . authenticated <EOL> def get ( self ) : <EOL> airnotifierinfo = { } <EOL> airnotifierinfo [ '<STR_LIT:version>' ] = VERSION <EOL> mongodbinfo = self . application . mongodb . server_info ( ) <EOL> if mongodbinfo . has_key ( '<STR_LIT>' ) : <EOL> del mongodbinfo [ '<STR_LIT>' ] <EOL> pythoninfo = { } <EOL> pythoninfo [ '<STR_LIT:version>' ] = sys . version <EOL> pythoninfo [ '<STR_LIT>' ] = sys . platform <EOL> pythoninfo [ '<STR_LIT>' ] = os . name <EOL> pythoninfo [ '<STR_LIT>' ] = platform . architecture ( ) <EOL> pythoninfo [ '<STR_LIT>' ] = platform . machine ( ) <EOL> pythoninfo [ '<STR_LIT>' ] = platform . python_build ( ) [ <NUM_LIT:1> ] <EOL> pythoninfo [ '<STR_LIT>' ] = platform . python_compiler ( ) <EOL> pythoninfo [ '<STR_LIT>' ] = "<STR_LIT:U+002CU+0020>" . join ( sys . builtin_module_names ) <EOL> self . render ( '<STR_LIT>' , airnotifierinfo = airnotifierinfo , pythoninfo = pythoninfo , mongodb = mongodbinfo , tornadoversion = tornado . version ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class AdminHandler ( WebBaseHandler ) : <EOL> @ tornado . web . authenticated <EOL> def get ( self , action ) : <EOL> if self . get_argument ( '<STR_LIT>' , None ) : <EOL> user_id = self . get_argument ( '<STR_LIT>' , None ) <EOL> if user_id : <EOL> self . masterdb . managers . remove ( { '<STR_LIT>' : ObjectId ( user_id ) } ) <EOL> self . redirect ( "<STR_LIT>" ) <EOL> return <EOL> managers = self . masterdb . managers . find ( ) <EOL> self . render ( '<STR_LIT>' , managers = managers , created = None , updated = None , currentuser = self . currentuser ) <EOL> def post ( self , action ) : <EOL> action = self . get_argument ( '<STR_LIT:action>' , "<STR_LIT>" ) <EOL> if action == '<STR_LIT>' : <EOL> user = { } <EOL> user [ '<STR_LIT>' ] = int ( time . time ( ) ) <EOL> user [ '<STR_LIT:username>' ] = self . get_argument ( '<STR_LIT>' ) . strip ( ) <EOL> password = self . get_argument ( '<STR_LIT>' ) . strip ( ) <EOL> passwordhash = sha1 ( "<STR_LIT>" % ( options . passwordsalt , password ) ) . hexdigest ( ) <EOL> user [ '<STR_LIT:password>' ] = passwordhash <EOL> user [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> result = self . masterdb . managers . update ( { '<STR_LIT:username>' : user [ '<STR_LIT:username>' ] } , user , safe = True , upsert = True ) <EOL> managers = self . masterdb . managers . find ( ) <EOL> if result [ '<STR_LIT>' ] : <EOL> self . render ( '<STR_LIT>' , managers = managers , updated = user , created = None , currentuser = self . currentuser ) <EOL> else : <EOL> self . render ( '<STR_LIT>' , managers = managers , updated = None , created = user , currentuser = self . currentuser ) <EOL> elif action == '<STR_LIT>' : <EOL> password = self . get_argument ( '<STR_LIT>' ) . strip ( ) <EOL> passwordhash = sha1 ( "<STR_LIT>" % ( options . passwordsalt , password ) ) . hexdigest ( ) <EOL> self . masterdb . managers . update ( { "<STR_LIT:username>" : self . currentuser [ '<STR_LIT:username>' ] } , { "<STR_LIT>" : { "<STR_LIT:password>" : passwordhash } } ) <EOL> managers = self . masterdb . managers . find ( ) <EOL> user = self . currentuser <EOL> self . render ( '<STR_LIT>' , managers = managers , updated = user , created = None , currentuser = self . currentuser ) </s>
