def save_actions(self, rule): with transaction.atomic(): rule.delete_actions() if self.cleaned_data['close_case'] or self.cleaned_data['properties_to_update']: definition = UpdateCaseDefinition(close_case=self.cleaned_data['close_case']) definition.set_properties_to_update(self.cleaned_data['properties_to_update']) definition.save() action = CaseRuleAction(rule=rule) action.definition = definition action.save() for item in self.cleaned_data['custom_action_definitions']: definition = CustomActionDefinition.objects.create( name=item['name'], ) action = CaseRuleAction(rule=rule) action.definition = definition action.save()
def setUp(self): test_rules = [ AutomaticUpdateRule( domain=self.domain, active=True, workflow=AutomaticUpdateRule.WORKFLOW_CASE_UPDATE, name="Norway rule", case_type="person", filter_on_server_modified=True, server_modified_boundary=None), AutomaticUpdateRule( domain=self.domain, active=True, workflow=AutomaticUpdateRule.WORKFLOW_CASE_UPDATE, name="Is it hot enough?", case_type="person", filter_on_server_modified=False, server_modified_boundary=2), AutomaticUpdateRule( domain=self.domain, active=False, workflow=AutomaticUpdateRule.WORKFLOW_CASE_UPDATE, name="Closed parent case", case_type="family_member", filter_on_server_modified=True, server_modified_boundary=None), ] test_rules[0].save() test_rules[1].save() test_rules[2].save() # Create test rule criterias definition = MatchPropertyDefinition.objects.create( property_name="place", property_value="Norway", match_type=MatchPropertyDefinition.MATCH_EQUAL, ) criteria = CaseRuleCriteria(rule=test_rules[0]) criteria.definition = definition criteria.save() definition = MatchPropertyDefinition.objects.create( property_name="temperature", property_value="96", match_type=MatchPropertyDefinition.MATCH_EQUAL, ) criteria = CaseRuleCriteria(rule=test_rules[1]) criteria.definition = definition criteria.save() definition = CustomMatchDefinition.objects.create( name="COVID_US_ASSOCIATED_USER_CASES", ) criteria = CaseRuleCriteria(rule=test_rules[0]) criteria.definition = definition criteria.save() definition = ClosedParentDefinition.objects.create() criteria = CaseRuleCriteria(rule=test_rules[2]) criteria.definition = definition criteria.save() # Create test rule actions definition = UpdateCaseDefinition(close_case=True) definition.save() action = CaseRuleAction(rule=test_rules[0]) action.definition = definition action.save() definition = UpdateCaseDefinition(close_case=False) test_properties = [] test_properties.append( UpdateCaseDefinition.PropertyDefinition( name="hot_enough", value_type="EXACT", value="true", )) test_properties.append( UpdateCaseDefinition.PropertyDefinition( name="territory_hot_enough", value_type="CASE_PROPERTY", value="current_territory", )) definition.set_properties_to_update(test_properties) definition.save() action = CaseRuleAction(rule=test_rules[1]) action.definition = definition action.save() definition = CustomActionDefinition.objects.create( name="COVID_US_CLOSE_CASES_ASSIGNED_CHECKIN", ) action = CaseRuleAction(rule=test_rules[1]) action.definition = definition action.save()
def update_auto_update_rules(domain_link): if domain_link.is_remote: upstream_rules = remote_get_auto_update_rules(domain_link) else: upstream_rules = local_get_auto_update_rules(domain_link.master_domain) downstream_rules = AutomaticUpdateRule.by_domain( domain_link.linked_domain, AutomaticUpdateRule.WORKFLOW_CASE_UPDATE, active_only=False ) for upstream_rule_def in upstream_rules: # Grab local rule by upstream ID (preferred) or by name try: downstream_rule = downstream_rules.get(upstream_id=upstream_rule_def['rule']['id']) except AutomaticUpdateRule.DoesNotExist: try: downstream_rule = downstream_rules.get(name=upstream_rule_def['rule']['name']) except AutomaticUpdateRule.MultipleObjectsReturned: # If there are multiple rules with the same name, overwrite the first. downstream_rule = downstream_rules.filter(name=upstream_rule_def['rule']['name']).first() except AutomaticUpdateRule.DoesNotExist: downstream_rule = None # If no corresponding local rule, make a new rule if not downstream_rule: downstream_rule = AutomaticUpdateRule( domain=domain_link.linked_domain, active=upstream_rule_def['rule']['active'], workflow=AutomaticUpdateRule.WORKFLOW_CASE_UPDATE, upstream_id=upstream_rule_def['rule']['id'] ) # Copy all the contents from old rule to new rule with transaction.atomic(): downstream_rule.name = upstream_rule_def['rule']['name'] downstream_rule.case_type = upstream_rule_def['rule']['case_type'] downstream_rule.filter_on_server_modified = upstream_rule_def['rule']['filter_on_server_modified'] downstream_rule.server_modified_boundary = upstream_rule_def['rule']['server_modified_boundary'] downstream_rule.active = upstream_rule_def['rule']['active'] downstream_rule.save() downstream_rule.delete_criteria() downstream_rule.delete_actions() # Largely from data_interfaces/forms.py - save_criteria() for criteria in upstream_rule_def['criteria']: definition = None if criteria['match_property_definition']: definition = MatchPropertyDefinition.objects.create( property_name=criteria['match_property_definition']['property_name'], property_value=criteria['match_property_definition']['property_value'], match_type=criteria['match_property_definition']['match_type'], ) elif criteria['custom_match_definition']: definition = CustomMatchDefinition.objects.create( name=criteria['custom_match_definition']['name'], ) elif criteria['closed_parent_definition']: definition = ClosedParentDefinition.objects.create() new_criteria = CaseRuleCriteria(rule=downstream_rule) new_criteria.definition = definition new_criteria.save() # Largely from data_interfacees/forms.py - save_actions() for action in upstream_rule_def['actions']: definition = None if action['update_case_definition']: definition = UpdateCaseDefinition(close_case=action['update_case_definition']['close_case']) properties = [] for propertyItem in action['update_case_definition']['properties_to_update']: properties.append( UpdateCaseDefinition.PropertyDefinition( name=propertyItem['name'], value_type=propertyItem['value_type'], value=propertyItem['value'], ) ) definition.set_properties_to_update(properties) definition.save() elif action['custom_action_definition']: definition = CustomActionDefinition.objects.create( name=action['custom_action_definition']['name'], ) action = CaseRuleAction(rule=downstream_rule) action.definition = definition action.save()