def _get_shared_parameters(context): """ Returns the rendering and condition evaluation environment for Jinja2 templates """ def chance(value: float) -> bool: return random.random() < value return dict( user=context.user, get_answer=lambda identifier: UserAnswers.get_answer( context.user, identifier), has_answered=lambda identifier: UserAnswers.has_answered( context.user, identifier), questionnaire_completion=context.questionnaire_completion_ratio, user_recent=context.has_incoming_intent, bot_recent=context.has_outgoing_intent, question=context.current_question, questionnaire=context.current_questionnaire, overall_completion=context.overall_completion_ratio, num_actions=lambda intent: len( context.filter_outgoing_utterances( lambda ca: intent in ca.intents, 12)), get=lambda key: context.get(key, None), formal=context.user.formal_address, informal=not context.user.formal_address, chance=chance, month=lambda n: MONTHS.get(n), is_this_year=lambda y: y == datetime.datetime.now().year)
def add_answer_to_question(self, question: Union[Question, str], answer: str): """ Creates a database entry for the answer given by the user to which this context belongs. """ UserAnswers.add_answer( user=self.user, question_id=question.id if isinstance(question, Question) else question, answer=answer, ) self._answered_question_ids.add(question.id if isinstance(question, Question) else question) self._update_question_context()
def doesUserSupportChoice(self, choice, user): query = (UserAnswers.query().filter( UserAnswers._properties["choice"] == choice.key.integer_id() ).filter( UserAnswers._properties["user"] == user.key.integer_id()).filter( UserAnswers._properties["isUpToDate"] == True)) return query.count() == 1
def _init_collections(self): uid = self.user.id self.dialog_states = DialogStates( self._initial_state, redis=self._redis, key=f'{uid}:ds') # User and Bot utterances from newest to oldest if self._redis: self._utterances = SyncableDeque( maxlen=self.SIZE_LIMIT, redis=self._redis, key=f'{uid}:utterances') # type: Deque[Union[MessageUnderstanding, ChatAction]] self._value_store = SyncableDict( # type: Dict redis=self._redis, writeback=True, key=f'{uid}:kv_store') self._utterances.sync() self._value_store.sync() else: self._utterances = deque() # type: Deque[Union[MessageUnderstanding, ChatAction]] self._value_store = dict() self._answered_question_ids = UserAnswers.get_answered_question_ids(self.user) self._current_question = None # type: Question self._current_questionnaire = None # type: Questionnaire self._all_done = False # type: bool self._update_question_context() # cached property self.__last_user_utterance = None # type: MessageUnderstanding
def getUserNumericalAnswerOrNone(self, question, user): query = (UserAnswers.query().filter( UserAnswers._properties["question"] == question.key.integer_id()). filter(UserAnswers._properties["user"] == user.key.integer_id()).order(-UserAnswers.updatedOn)) if query.count() == 0: return None return query.get().number
def reset_all(self) -> int: self.dialog_states.reset() self._utterances.clear() self._value_store.clear() if self._redis: self._utterances.sync() self._sync_utterances() num_reset = UserAnswers.reset_answers(self.user) self._answered_question_ids = set() self._update_question_context() # self.__last_user_utterance = None # type: MessageUnderstanding return num_reset
def send_questionnaires(r, c: Context): for u in User.select(): all_answers = UserAnswers.get_name_answer_dict(u) if all_answers: r.say("preview claim", parameters=dict(answers=all_answers))
def postUserVote(self): user = self.getUserOrNone() question_id = self.request.get('question_id', None) choice_id = self.request.get('choice_id', None) choice_numeric = self.request.get('choice_numeric', None) if (user is None): self.response.out.write( json.dumps({ 'status': 'FAILURE', 'info': 'user_email required', })) return if (question_id is None): self.response.out.write( json.dumps({ 'status': 'FAILURE', 'info': 'question_id required', })) return if (choice_id is None and choice_numeric == None): self.response.out.write( json.dumps({ 'status': 'FAILURE', 'info': 'choice_id or choice_numeric required', })) return if (choice_id is not None and choice_numeric is not None): self.response.out.write( json.dumps({ 'status': 'FAILURE', 'info': 'Only one of {choice_id, choice_numeric} should be stated', })) return # Invalidate old votes oldVotes = (UserAnswers.query().filter( UserAnswers._properties["question"] == int(question_id)).filter( UserAnswers._properties["user"] == user.key.integer_id() ).filter(UserAnswers._properties["isUpToDate"] == True).fetch()) for oldVote in oldVotes: oldVote.isUpToDate = False oldVote.put() # Create new choice vote newVote = None if (choice_id is not None): newVote = UserAnswers(parent=DATASTORE_KEY, question=int(question_id), user=user.key.integer_id(), choice=int(choice_id)) elif (choice_numeric is not None): newVote = UserAnswers(parent=DATASTORE_KEY, question=int(question_id), user=user.key.integer_id(), number=int(choice_numeric)) # Store in db newVote.put() self.response.out.write( json.dumps({ 'status': 'SUCCESS', 'user_email': user.email, 'question_id': newVote.question, 'choice_id': newVote.choice, 'number': newVote.number, 'timestamp': time.mktime(newVote.updatedOn.timetuple()) * 1000 + newVote.updatedOn.microsecond / 1000, }))
def preview_claim(r, c): all_answers = UserAnswers.get_name_answer_dict(c.user) r.ask("preview claim", parameters=dict(answers=all_answers), choices=['affirm_submit', 'negate_no']) return "previewing_claim"
def last_name(self, value): from model import UserAnswers UserAnswers.add_answer(user=self, question_id='last_name', answer=value)
def last_name(self) -> str: from model import UserAnswers return UserAnswers.get_answer(self, 'last_name')