Beispiel #1
0
    def log(self, question, r):
        logging.info('Updating datastore with question.')
        # Insert question into datastore
        response = json.loads(r.content)
        answers = response['question']['answers']
        answer = answers[0]['text'] if len(answers) > 0 else 'No answer given'
        phone_number = int(self.request.get('p', '0'))
        q = Question(phone_number=phone_number,
                     question=question,
                     response=response,
                     answer=answer)
        q.put()

        # Update user stats
        # This isn't atomic, I don't think it's a big deal though.
        user = Stats.query(Stats.phone_number == phone_number).fetch()
        if len(user) > 0:
          user = user[0]
        else:
          user = Stats(phone_number=phone_number,
                       number_of_questions=0,
                       most_recent_question=datetime.min)
        user.number_of_questions += 1
        user.most_recent_question = q.time
        user.put()
Beispiel #2
0
    def get(self):
        query = Stats.query()
        stats = []

        for stat in query.fetch():
            stats.append({'phone_number': stat.phone_number,
                          'last_call_date': str(stat.most_recent_question),
                          'number_of_questions': stat.number_of_questions
                         })

        output = json.dumps(stats)
        self.render_json(output)