def handle_messages(org): from casepro.cases.models import Case from casepro.rules.models import Rule from .models import Message backend = org.get_backend() case_replies = [] num_rules_matched = 0 # fetch all unhandled messages who now have full contacts unhandled = Message.get_unhandled(org).filter(contact__is_stub=False) unhandled = list( unhandled.select_related("contact").prefetch_related( "contact__groups")) if unhandled: rules = Rule.get_all(org) rule_processor = Rule.BatchProcessor(org, rules) for msg in unhandled: open_case = Case.get_open_for_contact_on(org, msg.contact, msg.created_on) # only apply rules if there isn't a currently open case for this contact if open_case: open_case.add_reply(msg) case_replies.append(msg) else: rules_matched, actions_deferred = rule_processor.include_messages( msg) num_rules_matched += rules_matched # archive messages which are case replies on the backend if case_replies: backend.archive_messages(org, case_replies) rule_processor.apply_actions() # mark all of these messages as handled Message.objects.filter(pk__in=[m.pk for m in unhandled]).update( is_handled=True, modified_on=timezone.now()) return { "handled": len(unhandled), "rules_matched": num_rules_matched, "case_replies": len(case_replies) }
def create_label(self, org, uuid, name, description, keywords=(), **kwargs): label = Label.objects.create(org=org, uuid=uuid, name=name, description=description, **kwargs) if keywords: rule = Rule.create(org, [ContainsTest(keywords, Quantifier.ANY)], [LabelAction(label)]) label.rule = rule label.save(update_fields=('rule',)) return label
def update_tests(self, tests): from casepro.rules.models import Rule, LabelAction if tests: if self.rule: self.rule.tests = json_encode(tests) self.rule.save(update_fields=('tests', )) else: self.rule = Rule.create(self.org, tests, [LabelAction(self)]) self.save(update_fields=('rule', )) else: if self.rule: rule = self.rule self.rule = None self.save(update_fields=('rule', )) rule.delete()
def update_tests(self, tests): from casepro.rules.models import Rule, LabelAction if tests: if self.rule: self.rule.tests = json_encode(tests) self.rule.save(update_fields=('tests',)) else: self.rule = Rule.create(self.org, tests, [LabelAction(self)]) self.save(update_fields=('rule',)) else: if self.rule: rule = self.rule self.rule = None self.save(update_fields=('rule',)) rule.delete()
def handle_messages(org): from casepro.backend import get_backend from casepro.cases.models import Case from casepro.rules.models import Rule from .models import Message backend = get_backend() case_replies = [] num_rules_matched = 0 # fetch all unhandled messages who now have full contacts unhandled = Message.get_unhandled(org).filter(contact__is_stub=False) unhandled = list(unhandled.select_related('contact').prefetch_related('contact__groups')) if unhandled: rules = Rule.get_all(org) rule_processor = Rule.BatchProcessor(org, rules) for msg in unhandled: open_case = Case.get_open_for_contact_on(org, msg.contact, msg.created_on) # only apply rules if there isn't a currently open case for this contact if open_case: open_case.add_reply(msg) case_replies.append(msg) else: rules_matched, actions_deferred = rule_processor.include_messages(msg) num_rules_matched += rules_matched # archive messages which are case replies on the backend if case_replies: backend.archive_messages(org, case_replies) rule_processor.apply_actions() # mark all of these messages as handled Message.objects.filter(pk__in=[m.pk for m in unhandled]).update(is_handled=True) return {'handled': len(unhandled), 'rules_matched': num_rules_matched, 'case_replies': len(case_replies)}
def create_rule(self, org, tests, actions): return Rule.create(org, tests, actions)