def setUp(self): super(ExpressionsTest, self).setUp() contact = self.create_contact("Joe Blow", "123") contact.language = u'eng' contact.save() variables = dict() variables['contact'] = contact.build_message_context() variables['flow'] = dict(water_source="Well", # key with underscore blank="", # blank string arabic="اثنين ثلاثة", # RTL chars english="two three", # LTR chars urlstuff=' =&\u0628', # stuff that needs URL encoding users=5, # numeric as int count="5", # numeric as string average=2.5, # numeric as float joined=datetime(2014, 12, 1, 9, 0, 0, 0, timezone.utc), # date as datetime started="1/12/14 9:00") # date as string self.context = EvaluationContext(variables, timezone.utc, DateStyle.DAY_FIRST)
def execute_with_message(self, runner, context, msg): errors = [] # variables should evaluate to group names or phone numbers variables = [] for variable in self.variables: if not variable.is_new_contact(): var, var_errors = runner.substitute_variables(variable.value, context) if not var_errors: variables.append(VariableRef(var)) else: errors += var_errors else: variables.append(VariableRef(variable.value)) # create a new context without the @contact.* variables which will remain unresolved for now new_vars = context.variables.copy() del new_vars['contact'] context_for_other_contacts = EvaluationContext(new_vars, context.timezone, context.date_style) template, errors = runner.substitute_variables_if_available(msg, context_for_other_contacts) performed = SendAction(TranslatableText(template), self.contacts, self.groups, variables) return Action.Result.performed(performed, errors)
def build_context(self, runner, input): # our concept of now may be overridden by the runner now = runner.now if runner.now else datetime.datetime.now(tz=self.org.timezone) context = EvaluationContext({}, self.org.timezone, self.org.date_style, now) contact_context = self.contact.build_context(self, context) if input is not None: context.put_variable("step", input.build_context(context, contact_context)) context.put_variable("date", self.build_date_context(context)) context.put_variable("contact", contact_context) context.put_variable("extra", self.extra) flow_context = {} values = [] for key, value in self.values.iteritems(): flow_context[key] = value.build_context(context) values.append("%s: %s" % (key, value)) flow_context['*'] = "\n".join(values) context.put_variable("flow", flow_context) return context
def build_context(self, runner, input): # our concept of now may be overridden by the runner now = runner.now if runner.now else datetime.datetime.now(tz=self.org.timezone) context = EvaluationContext({}, self.org.timezone, self.org.date_style, now) contact_context = self.contact.build_context(self, context) if input is not None: context.put_variable("step", input.build_context(context, contact_context)) context.put_variable("date", self.build_date_context(context)) context.put_variable("contact", contact_context) context.put_variable("extra", self.extra) def build_flow_context(value_dict): flow_context = {} values = [] for key, value in value_dict.iteritems(): flow_context[key] = value.build_context(context) values.append("%s: %s" % (key, value)) flow_context['*'] = "\n".join(values) return flow_context context.put_variable("flow", build_flow_context(self.get_values())) # add the flow that was one level above us if self.level > 0: context.put_variable("parent", build_flow_context(self.values[self.level - 1])) # if we have a child below us, add that context in too if len(self.values) > self.level + 1: context.put_variable("child", build_flow_context(self.values[self.level + 1])) return context