<s> import re <EOL> from corehq . apps . sms . api import send_sms_to_verified_number <EOL> from corehq . util . translation import localize <EOL> from custom . ilsgateway . slab . handlers . transfer import TransferHandler <EOL> from custom . ilsgateway . tanzania . handlers . arrived import ArrivedHandler <EOL> from custom . ilsgateway . tanzania . handlers . delivered import DeliveredHandler <EOL> from custom . ilsgateway . tanzania . handlers . help import HelpHandler <EOL> from custom . ilsgateway . tanzania . handlers . la import LossAndAdjustment <EOL> from custom . ilsgateway . tanzania . handlers . language import LanguageHandler <EOL> from custom . ilsgateway . tanzania . handlers . messageinitiator import MessageInitiatior <EOL> from custom . ilsgateway . tanzania . handlers . notdelivered import NotDeliveredHandler <EOL> from custom . ilsgateway . tanzania . handlers . notsubmitted import NotSubmittedHandler <EOL> from custom . ilsgateway . tanzania . handlers . register import RegisterHandler <EOL> from custom . ilsgateway . tanzania . handlers . soh import SOHHandler <EOL> from custom . ilsgateway . tanzania . handlers . stockout import StockoutHandler <EOL> from custom . ilsgateway . tanzania . handlers . stop import StopHandler <EOL> from custom . ilsgateway . tanzania . handlers . supervision import SupervisionHandler <EOL> from custom . ilsgateway . tanzania . handlers . randr import RandrHandler <EOL> from custom . ilsgateway . tanzania . handlers . yes import YesHandler <EOL> from custom . ilsgateway . models import ILSGatewayConfig <EOL> from custom . ilsgateway . tanzania . reminders import CONTACT_SUPERVISOR <EOL> def choose_handler ( keyword , handlers ) : <EOL> for k , v in handlers . iteritems ( ) : <EOL> if keyword . lower ( ) in k : <EOL> return v <EOL> return None <EOL> def handle ( verified_contact , text , msg = None ) : <EOL> user = verified_contact . owner if verified_contact else None <EOL> domain = user . domain <EOL> if domain and not ILSGatewayConfig . for_domain ( domain ) : <EOL> return False <EOL> text = text . replace ( '<STR_LIT:\r>' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) . strip ( ) <EOL> args = text . split ( ) <EOL> if not args : <EOL> return False <EOL> keyword = args [ <NUM_LIT:0> ] <EOL> if keyword . startswith ( '<STR_LIT:#>' ) : <EOL> return False <EOL> args = args [ <NUM_LIT:1> : ] <EOL> params = { <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : domain , <EOL> '<STR_LIT:args>' : args , <EOL> '<STR_LIT>' : msg , <EOL> '<STR_LIT>' : verified_contact <EOL> } <EOL> def not_function ( word ) : <EOL> if args and re . match ( "<STR_LIT>" , word ) : <EOL> return NotDeliveredHandler <EOL> elif args and re . match ( "<STR_LIT>" , word ) : <EOL> return NotSubmittedHandler <EOL> return None <EOL> handlers = { <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : ArrivedHandler , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : HelpHandler , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : LanguageHandler , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : StopHandler , <EOL> ( '<STR_LIT:yes>' , '<STR_LIT>' , '<STR_LIT>' ) : YesHandler , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : RegisterHandler , <EOL> ( '<STR_LIT:test>' , ) : MessageInitiatior , <EOL> } <EOL> location_needed_handlers = { <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : SOHHandler , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : RandrHandler , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : DeliveredHandler , <EOL> ( '<STR_LIT>' , ) : NotDeliveredHandler , <EOL> ( '<STR_LIT>' , ) : NotSubmittedHandler , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : SupervisionHandler , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : LossAndAdjustment , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : StockoutHandler , <EOL> ( '<STR_LIT>' , ) : not_function ( args [ <NUM_LIT:0> ] ) if args else None , <EOL> ( '<STR_LIT>' , ) : TransferHandler <EOL> } <EOL> handler_class = choose_handler ( keyword , location_needed_handlers ) <EOL> if handler_class and not user . location_id : <EOL> return True <EOL> if not handler_class : <EOL> handler_class = choose_handler ( keyword , handlers ) <EOL> handler = handler_class ( ** params ) if handler_class else None <EOL> if handler : <EOL> if args : <EOL> return handler . handle ( ) <EOL> else : <EOL> return handler . help ( ) <EOL> elif keyword != '<STR_LIT:l>' : <EOL> with localize ( verified_contact . owner . get_language_code ( ) ) : <EOL> send_sms_to_verified_number ( verified_contact , unicode ( CONTACT_SUPERVISOR ) ) <EOL> return True </s>
