<s> try : <EOL> from httplib import BAD_REQUEST , FORBIDDEN , INTERNAL_SERVER_ERROR , ACCEPTED <EOL> except : <EOL> from http . client import BAD_REQUEST , FORBIDDEN , INTERNAL_SERVER_ERROR , ACCEPTED <EOL> from routes import route <EOL> from api import APIBaseHandler , EntityBuilder <EOL> import random <EOL> import time <EOL> from importlib import import_module <EOL> from constants import DEVICE_TYPE_IOS , DEVICE_TYPE_ANDROID , DEVICE_TYPE_WNS , DEVICE_TYPE_MPNS , DEVICE_TYPE_SMS <EOL> from pushservices . gcm import GCMUpdateRegIDsException , GCMInvalidRegistrationException , GCMNotRegisteredException , GCMException <EOL> import logging <EOL> _logger = logging . getLogger ( __name__ ) <EOL> @ route ( r"<STR_LIT>" ) <EOL> class PushHandler ( APIBaseHandler ) : <EOL> def validate_data ( self , data ) : <EOL> data . setdefault ( '<STR_LIT>' , '<STR_LIT:default>' ) <EOL> data . setdefault ( '<STR_LIT>' , None ) <EOL> data . setdefault ( '<STR_LIT>' , None ) <EOL> data . setdefault ( '<STR_LIT>' , { } ) <EOL> return data <EOL> def get_apns_conn ( self ) : <EOL> if not self . apnsconnections . has_key ( self . app [ '<STR_LIT>' ] ) : <EOL> self . send_response ( INTERNAL_SERVER_ERROR , dict ( error = "<STR_LIT>" ) ) <EOL> return <EOL> count = len ( self . apnsconnections [ self . app [ '<STR_LIT>' ] ] ) <EOL> random . seed ( time . time ( ) ) <EOL> instanceid = random . randint ( <NUM_LIT:0> , count - <NUM_LIT:1> ) <EOL> return self . apnsconnections [ self . app [ '<STR_LIT>' ] ] [ instanceid ] <EOL> def post ( self ) : <EOL> try : <EOL> """<STR_LIT>""" <EOL> if not self . can ( "<STR_LIT>" ) : <EOL> self . send_response ( FORBIDDEN , dict ( error = "<STR_LIT>" ) ) <EOL> return <EOL> data = self . json_decode ( self . request . body ) <EOL> data = self . validate_data ( data ) <EOL> if '<STR_LIT>' in data : <EOL> if '<STR_LIT>' in data [ '<STR_LIT>' ] : <EOL> try : <EOL> proc = import_module ( '<STR_LIT>' + data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> data = proc . process_pushnotification_payload ( data ) <EOL> except Exception as ex : <EOL> self . send_response ( BAD_REQUEST , dict ( error = str ( ex ) ) ) <EOL> if not self . token : <EOL> self . token = data . get ( '<STR_LIT>' , None ) <EOL> extra = data . get ( '<STR_LIT>' , { } ) <EOL> device = data . get ( '<STR_LIT>' , DEVICE_TYPE_IOS ) . lower ( ) <EOL> channel = data . get ( '<STR_LIT>' , '<STR_LIT:default>' ) <EOL> token = self . db . tokens . find_one ( { '<STR_LIT>' : self . token } ) <EOL> if not token : <EOL> token = EntityBuilder . build_token ( self . token , device , self . appname , channel ) <EOL> if not self . can ( "<STR_LIT>" ) : <EOL> self . send_response ( BAD_REQUEST , dict ( error = "<STR_LIT>" ) ) <EOL> return <EOL> try : <EOL> self . db . tokens . insert ( token , safe = True ) <EOL> except Exception as ex : <EOL> self . send_response ( INTERNAL_SERVER_ERROR , dict ( error = str ( ex ) ) ) <EOL> if device == DEVICE_TYPE_SMS : <EOL> data . setdefault ( '<STR_LIT>' , { } ) <EOL> data [ '<STR_LIT>' ] . setdefault ( '<STR_LIT:to>' , data . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> data [ '<STR_LIT>' ] . setdefault ( '<STR_LIT:message>' , data . get ( '<STR_LIT:message>' , '<STR_LIT>' ) ) <EOL> sms = self . smsconnections [ self . app [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> sms . process ( token = data [ '<STR_LIT>' ] , alert = data [ '<STR_LIT>' ] , extra = extra , sms = data [ '<STR_LIT>' ] ) <EOL> self . send_response ( ACCEPTED ) <EOL> elif device == DEVICE_TYPE_IOS : <EOL> if type ( data [ '<STR_LIT>' ] ) is not dict : <EOL> alert = '<STR_LIT>' . join ( data [ '<STR_LIT>' ] . splitlines ( ) ) <EOL> else : <EOL> alert = data [ '<STR_LIT>' ] <EOL> data . setdefault ( '<STR_LIT>' , { } ) <EOL> data [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , data . get ( '<STR_LIT>' , None ) ) <EOL> data [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , data . get ( '<STR_LIT>' , None ) ) <EOL> data [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , data . get ( '<STR_LIT>' , None ) ) <EOL> self . get_apns_conn ( ) . process ( token = self . token , alert = alert , extra = extra , apns = data [ '<STR_LIT>' ] ) <EOL> self . send_response ( ACCEPTED ) <EOL> elif device == DEVICE_TYPE_ANDROID : <EOL> data . setdefault ( '<STR_LIT>' , { } ) <EOL> try : <EOL> gcm = self . gcmconnections [ self . app [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> response = gcm . process ( token = [ self . token ] , alert = data [ '<STR_LIT>' ] , extra = data [ '<STR_LIT>' ] , gcm = data [ '<STR_LIT>' ] ) <EOL> responsedata = response . json ( ) <EOL> if responsedata [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> self . send_response ( ACCEPTED ) <EOL> except GCMUpdateRegIDsException as ex : <EOL> self . send_response ( ACCEPTED ) <EOL> except GCMInvalidRegistrationException as ex : <EOL> self . send_response ( BAD_REQUEST , dict ( error = str ( ex ) , regids = ex . regids ) ) <EOL> except GCMNotRegisteredException as ex : <EOL> self . send_response ( BAD_REQUEST , dict ( error = str ( ex ) , regids = ex . regids ) ) <EOL> except GCMException as ex : <EOL> self . send_response ( INTERNAL_SERVER_ERROR , dict ( error = str ( ex ) ) ) <EOL> elif device == DEVICE_TYPE_WNS : <EOL> data . setdefault ( '<STR_LIT>' , { } ) <EOL> wns = self . wnsconnections [ self . app [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> wns . process ( token = data [ '<STR_LIT>' ] , alert = data [ '<STR_LIT>' ] , extra = extra , wns = data [ '<STR_LIT>' ] ) <EOL> self . send_response ( ACCEPTED ) <EOL> elif device == DEVICE_TYPE_MPNS : <EOL> data . setdefault ( '<STR_LIT>' , { } ) <EOL> mpns = self . mpnsconnections [ self . app [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> mpns . process ( token = data [ '<STR_LIT>' ] , alert = data [ '<STR_LIT>' ] , extra = extra , mpns = data [ '<STR_LIT>' ] ) <EOL> self . send_response ( ACCEPTED ) <EOL> else : <EOL> self . send_response ( BAD_REQUEST , dict ( error = '<STR_LIT>' ) ) <EOL> logmessage = '<STR_LIT>' % ( len ( data [ '<STR_LIT>' ] ) , self . appkey ) <EOL> self . add_to_log ( '<STR_LIT>' % self . appname , logmessage ) <EOL> except Exception as ex : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> self . send_response ( INTERNAL_SERVER_ERROR , dict ( error = str ( ex ) ) ) </s>
