<s> """<STR_LIT>""" <EOL> import json <EOL> import datetime <EOL> from django . http import HttpResponse <EOL> from django . core . urlresolvers import reverse <EOL> from django . views . decorators . http import require_POST <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . utils . translation import ugettext as _ <EOL> from onadata . apps . sms_support . tools import SMS_API_ERROR , SMS_SUBMISSION_ACCEPTED <EOL> from onadata . apps . sms_support . parser import process_incoming_smses <EOL> def autodoc ( url_root , username , id_string ) : <EOL> urla = url_root + reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : username , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> urlb = url_root + reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : username , <EOL> '<STR_LIT>' : id_string , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> doc = ( u'<STR_LIT>' + <EOL> _ ( u"<STR_LIT>" ) <EOL> % { '<STR_LIT>' : u'<STR_LIT>' <EOL> u'<STR_LIT>' } <EOL> + u'<STR_LIT>' + <EOL> _ ( u"<STR_LIT>" ) <EOL> + '<STR_LIT>' + <EOL> _ ( u"<STR_LIT>" ) <EOL> + u'<STR_LIT>' <EOL> + u'<STR_LIT>' + <EOL> _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) <EOL> + '<STR_LIT>' + <EOL> _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) <EOL> + '<STR_LIT>' + <EOL> _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) <EOL> + u'<STR_LIT>' ) % { '<STR_LIT>' : urla , '<STR_LIT>' : urlb } <EOL> return doc <EOL> def get_response ( data ) : <EOL> message = data . get ( '<STR_LIT:text>' ) <EOL> if data . get ( '<STR_LIT:code>' ) == SMS_API_ERROR : <EOL> success = False <EOL> message = None <EOL> elif data . get ( '<STR_LIT:code>' ) != SMS_SUBMISSION_ACCEPTED : <EOL> success = True <EOL> message = _ ( u"<STR_LIT>" ) % message <EOL> else : <EOL> success = True <EOL> response = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:success>" : success , <EOL> "<STR_LIT>" : "<STR_LIT>" } } <EOL> if message : <EOL> messages = [ { "<STR_LIT:to>" : data . get ( '<STR_LIT>' ) , "<STR_LIT:message>" : message } ] <EOL> sendouts = data . get ( '<STR_LIT>' , [ ] ) <EOL> if len ( sendouts ) : <EOL> messages += [ { "<STR_LIT:to>" : data . get ( '<STR_LIT>' ) , "<STR_LIT:message>" : text } <EOL> for text in sendouts ] <EOL> response [ '<STR_LIT>' ] . update ( { "<STR_LIT>" : messages } ) <EOL> return HttpResponse ( json . dumps ( response ) , content_type = '<STR_LIT:application/json>' ) <EOL> @ require_POST <EOL> @ csrf_exempt <EOL> def import_submission ( request , username ) : <EOL> """<STR_LIT>""" <EOL> return import_submission_for_form ( request , username , None ) <EOL> @ require_POST <EOL> @ csrf_exempt <EOL> def import_submission_for_form ( request , username , id_string ) : <EOL> """<STR_LIT>""" <EOL> sms_identity = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> sms_text = request . POST . get ( '<STR_LIT:message>' , '<STR_LIT>' ) . strip ( ) <EOL> now_timestamp = datetime . datetime . now ( ) . strftime ( '<STR_LIT:%s>' ) <EOL> sent_timestamp = request . POST . get ( '<STR_LIT>' , now_timestamp ) . strip ( ) <EOL> try : <EOL> sms_time = datetime . datetime . fromtimestamp ( float ( sent_timestamp ) ) <EOL> except ValueError : <EOL> sms_time = datetime . datetime . now ( ) <EOL> return process_message_for_smssync ( username = username , <EOL> sms_identity = sms_identity , <EOL> sms_text = sms_text , <EOL> sms_time = sms_time , <EOL> id_string = id_string ) <EOL> def process_message_for_smssync ( username , <EOL> sms_identity , sms_text , sms_time , id_string ) : <EOL> """<STR_LIT>""" <EOL> if not sms_identity or not sms_text : <EOL> return get_response ( { '<STR_LIT:code>' : SMS_API_ERROR , <EOL> '<STR_LIT:text>' : _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> u"<STR_LIT>" ) } ) <EOL> incomings = [ ( sms_identity , sms_text ) ] <EOL> response = process_incoming_smses ( username , incomings , id_string ) [ - <NUM_LIT:1> ] <EOL> response . update ( { '<STR_LIT>' : sms_identity } ) <EOL> return get_response ( response ) </s>
