def report_session(session_id, screen_content): # to avoid circular import from ussd.core import ussd_session, UssdHandlerAbstract logger = get_logger(__name__).bind(action="report_session_task", session_id=session_id) ussd_report_session_data = screen_content['ussd_report_session'] session = ussd_session(session_id) if session.get('posted'): return request_conf = UssdHandlerAbstract.render_request_conf( session, ussd_report_session_data['request_conf']) UssdHandlerAbstract.make_request( http_request_conf=request_conf, response_session_key_save=ussd_report_session_data['session_key'], session=session, logger=logger) # check if it is the desired effect for expr in ussd_report_session_data['validate_response']: if UssdHandlerAbstract.evaluate_jija_expression(expr['expression'], session=session): session['posted'] = True session.save() return
def _with_items(self, text, value, items, start_index): list_items = [] for index, item in enumerate(items, start_index): context = {} extra = {"item": item} if isinstance(items, dict): extra.update( dict(key=item, value=items[item], item={item: items[item]})) context.update(extra) index_text = "{index}{index_format}".format( index=index, index_format=self.ussd_request.menu_index_format) list_items.append( ListItem( self._add_end_line("{index_text}{text}".format( index_text=index_text, text=UssdHandlerAbstract.render_text( self.ussd_request.session, text, extra=context))), self.evaluate_jija_expression( value, session=self.ussd_request.session, extra_context=context))) return list_items
def report_session( self, session_id, screen_content, session_store_backend: KeyValueStore = FilesystemStore("./session_data"), ): # to avoid circular import from ussd.core import UssdHandlerAbstract logger = get_logger(__name__).bind(action="report_session_task", session_id=session_id) logger.info('start') ussd_report_session_data = screen_content['ussd_report_session'] session = SessionStore(session_key=session_id, kv_store=session_store_backend) if session.get('posted'): logger.info("session_already_reported", posted=session['posted']) return request_conf = UssdHandlerAbstract.render_request_conf( session, ussd_report_session_data['request_conf']) UssdHandlerAbstract.make_request( http_request_conf=request_conf, response_session_key_save=ussd_report_session_data['session_key'], session=session, logger=logger) # check if it is the desired effect for expr in ussd_report_session_data['validate_response']: if UssdHandlerAbstract.evaluate_jija_expression(expr['expression'], session=session): session['posted'] = True session.save() return if ussd_report_session_data.get('retry_mechanism'): try: self.retry( **screen_content['ussd_report_session']['retry_mechanism']) except MaxRetriesExceededError as e: logger.warning("report_session_error", error_message=str(e))
def handle(self): loop_items = self.get_loop_items() values_to_update = self.screen_content["values_to_update"] for item in loop_items: # update extra context extra_context = { "item": item } if isinstance(loop_items, dict): extra_context.update( dict( key=item, value=loop_items[item], item={item: loop_items[item]} ) ) for update_value in values_to_update: if not (update_value.get('expression') and self.evaluate_jija_expression( update_value['expression'], session=self.ussd_request.session, extra_context=extra_context)): continue # evaluate key key = update_value['key'] \ if not UssdHandlerAbstract._contains_vars( update_value['key']) \ else self.evaluate_jija_expression( update_value['key'], session=self.ussd_request.session, extra_context=extra_context ) value = self.evaluate_jija_expression( update_value['value'], session=self.ussd_request.session, extra_context=extra_context ) or update_value['value'] # save them in the session store self.ussd_request.session[key] = value return self.route_options()
def _with_items(self, text, value, items, start_index): list_items = [] for index, item in enumerate(items, start_index): context = {} extra = { "item": item } if isinstance(items, dict): extra.update( dict( key=item, value=items[item], item={item: items[item]} ) ) context.update(extra) index_text = "{index}{index_format}".format( index=index, index_format=getattr(settings, 'USSD_INDEX_FORMAT', defaults.index_format)) list_items.append( ListItem( self._add_end_line("{index_text}{text}".format( index_text=index_text, text=UssdHandlerAbstract.render_text( self.ussd_request.session, text, extra=context ) ) ), self.evaluate_jija_expression(value, session= self.ussd_request.session, extra_context=context) ) ) return list_items