예제 #1
0
 def today(self, d):
     '''
     Get today's journal (yesterday if early morning)
     '''
     jrnl = MiniJournal.Get(self.user)
     self.set_response({'journal': jrnl.json() if jrnl else None},
                       success=True)
예제 #2
0
    def test_stateful_journal_submission(self):
        # Journal submissions ask multiple questions and require
        # state to be kept in a conversation_state object (memcached)

        # Setup journal questions for account
        # settings = tools.getJson(self.u.settings)

        NARR = "Productive! #Hacked a few things with @JuliaSpiegel"
        RATING = 7

        conversation = [
            # (User message, Flow reply)
            ("daily report", "A few words on your day?", False),  # narrative
            (NARR, "How was the day?", False),  # day_rating
            ("?", JOURNAL.INVALID_REPLY + " " + JOURNAL.INVALID_SUFFIX_NUMERIC,
             False),
            ("%s" % RATING, JOURNAL.TOP_TASK_PROMPT, False),
            ("Finish hacking the machine", JOURNAL.TOP_TASK_PROMPT_ADDTL,
             False),
            ("done", "Report submitted!", True)
        ]
        for message, expected_reply, expected_end_of_convo in conversation:
            action, params = self.ca.parse_message(message)
            reply, message_data, end_convo = self.ca.respond_to_action(
                action, parameters=params)
            self.assertEqual(expected_end_of_convo, end_convo)
            self.assertEqual(reply, expected_reply)

        # Confirm journal saved properly
        jrnl = MiniJournal.Get(self.u)
        self.assertIsNotNone(jrnl)
        rating = jrnl.get_data_value('day_rating')
        self.assertEqual(rating, RATING)
        narrative = jrnl.get_data_value('narrative')
        self.assertEqual(narrative, NARR)

        # Confirm we have tags from narrative
        tags = JournalTag.All(self.u)
        self.assertEqual(len(tags), 2)
        for t in tags:
            if t.person():
                self.assertEqual(t.name, "JuliaSpiegel")
            else:
                self.assertEqual(t.name, "Hacked")

        # Confirm we created tasks
        tasks = Task.Open(self.u)
        self.assertEqual(len(tasks), 2)  # One added in journal
        self.assertEqual(tasks[0].title, "Finish hacking the machine")

        # Try to submit again
        action, params = self.ca.parse_message("daily journal")
        reply, message_data, end_convo = self.ca.respond_to_action(
            action, parameters=params)
        self.assertEqual(reply, JOURNAL.ALREADY_SUBMITTED_REPLY)
예제 #3
0
 def _journal(self, message=""):
     DONE_MESSAGES = ["done", "that's all", "exit", "finished", "no"]
     MODES = ['questions', 'tasks', 'end']
     settings = tools.getJson(self.user.settings, {})
     questions = settings.get('journals', {}).get('questions', [])
     end_convo = False
     if questions:
         jrnl = MiniJournal.Get(self.user)
         if jrnl:
             return (JOURNAL.ALREADY_SUBMITTED_REPLY, True)
         else:
             if not self.cs:
                 self.cs = self._create_conversation_state()
                 self.cs.set_state('mode', 'questions')
             mode = self.cs.state.get('mode')
             mode_finished = False
             save_response = True
             last_question = None
             # Receive user message
             if mode == 'tasks':
                 is_done = message.lower().strip() in DONE_MESSAGES
                 mode_finished = is_done
                 save_response = not is_done
             elif mode == 'questions':
                 last_q_index = self.cs.state.get('last_q_index', -1)
                 last_question = last_q_index == len(questions) - 1
                 mode_finished = last_question
                 save_response = True
             if save_response:
                 successful_add = self.cs.add_message_from_user(message)
                 if not successful_add:
                     reply = self.cs.invalid_reply(
                     ) if mode == 'questions' else JOURNAL.INVALID_TASK
                     return (reply, False)
             mode_index = MODES.index(mode)
             if mode_finished:
                 mode = MODES[mode_index + 1]
                 self.cs.set_state('mode', mode)
             reply = None
             # Generate next reply
             if mode == 'questions':
                 next_q_index = last_q_index + 1
                 q = questions[next_q_index]
                 reply = q.get('text')
                 name = q.get('name')
                 response_type = q.get('response_type')
                 pattern = JOURNAL.PATTERNS.get(response_type)
                 store_number = response_type in JOURNAL.NUMERIC_RESPONSES
                 self.cs.expect_reply(
                     pattern, name,
                     store_number=store_number)  # Store as name
                 self.cs.set_state('last_q_index', next_q_index)
             elif mode == 'tasks':
                 # Ask to add tasks
                 tasks = self.cs.response_data.get('tasks', [])
                 additional = len(tasks) > 0
                 reply = JOURNAL.TOP_TASK_PROMPT_ADDTL if additional else JOURNAL.TOP_TASK_PROMPT
                 self.cs.expect_reply(JOURNAL.PTN_TEXT_RESPONSE,
                                      'tasks',
                                      store_array=True)  # Store as name
             elif mode == 'end':
                 # Finish and submit
                 task_names = []
                 if 'tasks' in self.cs.response_data:
                     task_names = self.cs.response_data.pop('tasks')
                 jrnl = MiniJournal.Create(self.user)
                 jrnl.Update(data=self.cs.response_data)
                 jrnl.parse_tags()
                 jrnl.put()
                 tasks = []
                 if task_names:
                     for tn in task_names:
                         task = Task.Create(self.user, tn)
                         tasks.append(task)
                 ndb.put_multi(tasks)
                 reply = "Report submitted!"
                 end_convo = True
             if reply:
                 self.cs.set_message_to_user(reply)
             if end_convo:
                 self._expire_conversation()
             else:
                 self._set_conversation_state()
             return (reply, end_convo)
     else:
         return ("Please visit flowdash.co to set up journal questions",
                 True)