def submit(self, d): ''' Submit today's journal (yesterday if 00:00 - 04:00) ''' date = None _date = self.request.get('date') if _date: date = tools.fromISODate(_date) task_json = tools.getJson(self.request.get('tasks')) # JSON params = tools.gets(self, strings=['lat', 'lon', 'tags_from_text'], json=['data'], lists=['tags']) logging.debug(params) if params.get('data'): if not params.get('tags'): params['tags'] = [] jrnl = MiniJournal.Create(self.user, date) jrnl.Update(**params) jrnl.parse_tags() jrnl.put() if task_json: # Save new tasks for tomorrow tasks = [] for t in task_json: if t: task = Task.Create(self.user, t) tasks.append(task) ndb.put_multi(tasks) self.success = True self.message = "Journal submitted!" self.set_response({'journal': jrnl.json() if jrnl else None})
def test_journal_report(self): jrnl = MiniJournal.Create(self.u, date=date(2017, 4, 5)) jrnl.Update(lat="-1.289744", lon="36.7694933", tags=[], data={'happiness': 9}) jrnl.put() self._test_report({'type': REPORT.JOURNAL_REPORT}, [['Date', 'Tags', 'Location', 'Data'], [ "2017-04-05", "", "\"-1.289744,36.7694933\"", "\"{\"\"happiness\"\": 9}\"" ]])
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)