def getProfile(createIfNeeded=False):
    # Annoying that we can't use django get_or_create() idiom here.  the
    # appengine equivalent get_or_insert() seems to allow querying by
    # key only.  I also ran into problems trying to wrap this in a
    # transaction.
    user = users.get_current_user()
    profiles = ImokUser.all().filter('account =', user).fetch(1)
    if profiles:
        profile = profiles[0]
    else:
        if createIfNeeded:
            profile = ImokUser(account=user)
        else:
            profile = None
    return profile
  def post(self):
    self.response.headers['Content-Type'] = 'text/json'

    phone = Phone.normalize_number(self.request.get('phone'))
    message = self.request.get('message')

    if not (message and phone):
      result = { 'result': 'error',
                 'message': 'missing phone and/or message' }
      self.response.out.write(json.dumps(result))
      return

    sms_message = SmsMessage(phone_number=phone, 
                         message=message, 
                         direction='incoming',
                         status='unclaimed')
    objects = [ sms_message ]

    phone_entity = Phone.all().filter('number =', phone).get()

    if not phone_entity:
      db.put(objects)
      self.response.out.write(json.dumps({'result': 'ok'}))
      return

    post = Post.fromText(message)
    post.unique_id = Post.gen_unique_key()
    post.user = phone_entity.user
    objects.append(post)
    sms_message.status = 'queued'

    db.put(objects)

    imok_user = ImokUser.all().filter('account =', phone_entity.user).get()
    email_query = RegisteredEmail.all().filter('userName ='******'emailAddress')

    for email in email_query:
      template_data = {
        'message': post.message,
        'link': post.permalink(self.request.host_url),
        'unsubscribe_link': email.permalink(self.request.host_url),
        'user': imok_user
        }
      body = template.render(s.template_path('email.txt'), template_data)
      mail.send_mail(sender='*****@*****.**',
                     to=email.emailAddress,
                     subject="I'm OK",
                     body=body)

    sms_message.status = 'processed'

    response_sms = SmsMessage(phone_number=phone,
                              message="I'm OK: Message received. %d contact(s) notified." % email_query.count(),
                              direction="outgoing",
                              status="queued")

    db.put([ response_sms, sms_message ])

    #self.response.out.write(message)
    self.response.out.write(json.dumps({'result': 'ok'}))
Beispiel #3
0
  def post(self):
    self.response.headers['Content-Type'] = 'text/json'

    phone = Phone.normalize_number(self.request.get('phone'))
    message = self.request.get('message')

    if not (message and phone):
      result = { 'result': 'error',
                 'message': 'missing phone and/or message' }
      self.response.out.write(json.dumps(result))
      return

    sms_message = SmsMessage(phone_number=phone, 
                         message=message, 
                         direction='incoming',
                         status='unclaimed')
    objects = [ sms_message ]

    phone_entity = Phone.all().filter('number =', phone).get()

    if not phone_entity:
      db.put(objects)
      self.response.out.write(json.dumps({'result': 'ok'}))
      return

    post = Post.fromText(message)
    post.unique_id = Post.gen_unique_key()
    post.user = phone_entity.user
    objects.append(post)
    sms_message.status = 'queued'

    db.put(objects)

    imok_user = ImokUser.all().filter('account =', phone_entity.user).get()
    email_query = RegisteredEmail.all().filter('userName ='******'emailAddress')

    for email in email_query:
      template_data = {
        'message': post.message,
        'link': post.permalink(self.request.host_url),
        'unsubscribe_link': email.permalink(self.request.host_url),
        'user': imok_user
        }
      body = template.render(s.template_path('email.txt'), template_data)
      mail.send_mail(sender=s.MAILER_EMAIL,
                     to=email.emailAddress,
                     subject="IMOk status",
                     body=body)

    sms_message.status = 'processed'

#     Response_sms = SmsMessage(phone_number=phone,
#                               message="IMOk: Message received, %d contact(s) notified." % email_query.count(),
#                               direction="outgoing",
#                               status="queued")

    sendSms(phone=phone_entity, message="IMOk: Message received, %d contact(s) notified." % email_query.count())
#    db.put([ response_sms, sms_message ])
    db.put([sms_message])
    
    #self.response.out.write(message)
    self.response.out.write(json.dumps({'result': 'ok'}))
Beispiel #4
0
def getPfifRecord(post):
    imokUser = ImokUser.getProfileForUser(post.user)
    if post.positionText == "":
        if post.lat == 0.0:
            posText = ""
        else:
            posText = "%.6f,%.6f" % (post.lat, post.lon)
    else:
        posText = post.positionText
    if posText == "":
        posElt = "\n      <pfif:last_known_location>%s</pfif:last_known_location>" % post.positionText
    else:
        posElt = ""
    # FIX: remove TEST_PREFIX when we start pushing to real Person Finder
    TEST_PREFIX = "iamok_"
    args = dict(
        imokHost="imokapp.appspot.com",
        userId=post.user.user_id(),
        entryDate=post.datetime.isoformat() + "Z",
        firstName=TEST_PREFIX + imokUser.firstName,
        lastName=TEST_PREFIX + imokUser.lastName,
        userName="******" % (TEST_PREFIX, imokUser.firstName, TEST_PREFIX, imokUser.lastName),
        posElt=posElt,
        postId=post.unique_id,
        message=post.message,
    )

    # We are intentionally leaving out some PFIF fields that we
    # have info for, due to privacy concerns.  Any information we send
    # to Person Finder is totally public.
    return (
        """<?xml version="1.0" encoding="utf-8"?>
<pfif:pfif xmlns:pfif="http://zesty.ca/pfif/1.2"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://zesty.ca/pfif/1.2
                     http://zesty.ca/pfif/1.2/pfif-1.2.xsd">
  <pfif:person>
    <pfif:person_record_id>%(imokHost)s/%(userId)s</pfif:person_record_id>
    <pfif:entry_date>%(entryDate)s</pfif:entry_date>
    <pfif:author_name>%(userName)s</pfif:author_name>
    <pfif:source_name>%(imokHost)s</pfif:source_name>
    <pfif:source_date>%(entryDate)s</pfif:source_date>
    <pfif:source_url>http://%(imokHost)s/person/%(userId)s</pfif:source_url>
    <pfif:first_name>%(firstName)s</pfif:first_name>
    <pfif:last_name>%(lastName)s</pfif:last_name>
    <pfif:note>
      <pfif:note_record_id>%(imokHost)s/post/%(postId)s</pfif:note_record_id>
      <pfif:person_record_id>%(imokHost)s/%(userId)s</pfif:person_record_id>
      <pfif:entry_date>%(entryDate)s</pfif:entry_date>
      <pfif:author_name>%(userName)s</pfif:author_name>
      <pfif:source_date>%(entryDate)s</pfif:source_date>
      <pfif:status>believed_alive</pfif:status>
      <pfif:found>true</pfif:found>%(posElt)s
      <pfif:text>
        Full text of status SMS: %(message)s
      </pfif:text>
    </pfif:note>
  </pfif:person>
</pfif:pfif>
"""
        % args
    )
def getPfifRecord(post):
    imokUser = ImokUser.getProfileForUser(post.user)
    if post.positionText == '':
        if post.lat == 0.0:
            posText = ''
        else:
            posText = '%.6f,%.6f' % (post.lat, post.lon)
    else:
        posText = post.positionText
    if posText == '':
        posElt = (
            '\n      <pfif:last_known_location>%s</pfif:last_known_location>' %
            post.positionText)
    else:
        posElt = ''
    # FIX: remove TEST_PREFIX when we start pushing to real Person Finder
    TEST_PREFIX = 'iamok_'
    args = dict(
        imokHost='imokapp.appspot.com',
        userId=post.user.user_id(),
        entryDate=post.datetime.isoformat() + 'Z',
        firstName=TEST_PREFIX + imokUser.firstName,
        lastName=TEST_PREFIX + imokUser.lastName,
        userName='******' %
        (TEST_PREFIX, imokUser.firstName, TEST_PREFIX, imokUser.lastName),
        posElt=posElt,
        postId=post.unique_id,
        message=post.message,
    )

    # We are intentionally leaving out some PFIF fields that we
    # have info for, due to privacy concerns.  Any information we send
    # to Person Finder is totally public.
    return '''<?xml version="1.0" encoding="utf-8"?>
<pfif:pfif xmlns:pfif="http://zesty.ca/pfif/1.2"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://zesty.ca/pfif/1.2
                     http://zesty.ca/pfif/1.2/pfif-1.2.xsd">
  <pfif:person>
    <pfif:person_record_id>%(imokHost)s/%(userId)s</pfif:person_record_id>
    <pfif:entry_date>%(entryDate)s</pfif:entry_date>
    <pfif:author_name>%(userName)s</pfif:author_name>
    <pfif:source_name>%(imokHost)s</pfif:source_name>
    <pfif:source_date>%(entryDate)s</pfif:source_date>
    <pfif:source_url>http://%(imokHost)s/person/%(userId)s</pfif:source_url>
    <pfif:first_name>%(firstName)s</pfif:first_name>
    <pfif:last_name>%(lastName)s</pfif:last_name>
    <pfif:note>
      <pfif:note_record_id>%(imokHost)s/post/%(postId)s</pfif:note_record_id>
      <pfif:person_record_id>%(imokHost)s/%(userId)s</pfif:person_record_id>
      <pfif:entry_date>%(entryDate)s</pfif:entry_date>
      <pfif:author_name>%(userName)s</pfif:author_name>
      <pfif:source_date>%(entryDate)s</pfif:source_date>
      <pfif:status>believed_alive</pfif:status>
      <pfif:found>true</pfif:found>%(posElt)s
      <pfif:text>
        Full text of status SMS: %(message)s
      </pfif:text>
    </pfif:note>
  </pfif:person>
</pfif:pfif>
''' % args
Beispiel #6
0
    def savePostAndPush(self,
                        text,
                        phoneNumber,
                        user,
                        smsMessage=None,
                        bogusTimestamp=None):
        post = Post.fromText(text)
        if bogusTimestamp != None:
            post.datetime = bogusTimestamp
        post.unique_id = Post.gen_unique_key()
        post.user = user
        objects = [post]
        if smsMessage != None:
            smsMessage.status = 'queued'
            objects.append(smsMessage)
        db.put(objects)

        ######################################################################
        # send email

        # FIX: this block of code does not belong in sms_twilio

        imok_user = ImokUser.getProfileForUser(user)
        email_query = RegisteredEmail.all().filter('userName ='******'emailAddress')

        debugOutput = []
        for email in email_query:
            template_data = {
                'message': post.message,
                'link': post.permalink(self.request.host_url),
                'unsubscribe_link': email.permalink(self.request.host_url),
                'user': imok_user
            }
            body = template.render(s.template_path('email.txt'), template_data)
            debugOutput.append(body)
            mail.send_mail(sender=s.MAILER_EMAIL,
                           to=email.emailAddress,
                           subject="IMOk status",
                           body=body)

        twitter_post(imok_user, post.message)

        ######################################################################
        # post to person finder
        if 0:
            debugText = personFinder.postToPersonFinder(post)
            debugOutput.append(debugText)

        ######################################################################
        # send confirmation SMS
        if smsMessage != None:
            smsMessage.status = 'processed'
            smsMessage.put()

            response_sms = SmsMessage(
                phone_number=phoneNumber,
                message="I'm OK: Message received, %d contact(s) notified." %
                email_query.count(),
                direction="outgoing",
                status="queued")
            #response_sms.put()
            sendSms(response_sms)

        #self.response.out.write(message)
        #self.response.out.write(json.dumps({'result': 'ok'}))
        return debugOutput