|
| 1 | +""" |
| 2 | +Evaluate trigger_conditions against form submission data. |
| 3 | +
|
| 4 | +Condition format (same as FormField.conditional_rules): |
| 5 | +
|
| 6 | + { |
| 7 | + "operator": "AND" | "OR", |
| 8 | + "conditions": [ |
| 9 | + { |
| 10 | + "field": "field_name", |
| 11 | + "operator": "equals" | "not_equals" | "gt" | "lt" | |
| 12 | + "gte" | "lte" | "contains" | "in", |
| 13 | + "value": <expected_value> |
| 14 | + }, |
| 15 | + ... |
| 16 | + ] |
| 17 | + } |
| 18 | +
|
| 19 | +``evaluate_conditions(conditions, data)`` returns ``True`` when the |
| 20 | +submission data satisfies the rule set, or when ``conditions`` is |
| 21 | +``None`` / empty (unconditional). |
| 22 | +""" |
| 23 | + |
| 24 | +from __future__ import annotations |
| 25 | + |
| 26 | +import logging |
| 27 | +from decimal import Decimal, InvalidOperation |
| 28 | +from typing import Any |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +def _coerce_numeric(val: Any) -> Decimal | None: |
| 34 | + """Try to coerce a value to Decimal for numeric comparisons.""" |
| 35 | + if val is None: |
| 36 | + return None |
| 37 | + try: |
| 38 | + return Decimal(str(val)) |
| 39 | + except (InvalidOperation, ValueError, TypeError): |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +def _evaluate_single(condition: dict, data: dict) -> bool: |
| 44 | + """Evaluate a single condition dict against submission data.""" |
| 45 | + field = condition.get("field", "") |
| 46 | + operator = condition.get("operator", "equals") |
| 47 | + expected = condition.get("value") |
| 48 | + |
| 49 | + actual = data.get(field) |
| 50 | + |
| 51 | + # Normalise to strings for simple comparisons |
| 52 | + actual_str = str(actual).strip() if actual is not None else "" |
| 53 | + expected_str = str(expected).strip() if expected is not None else "" |
| 54 | + |
| 55 | + if operator == "equals": |
| 56 | + return actual_str.lower() == expected_str.lower() |
| 57 | + |
| 58 | + if operator == "not_equals": |
| 59 | + return actual_str.lower() != expected_str.lower() |
| 60 | + |
| 61 | + if operator == "contains": |
| 62 | + return expected_str.lower() in actual_str.lower() |
| 63 | + |
| 64 | + if operator == "in": |
| 65 | + # expected should be a list; check if actual is in it |
| 66 | + if isinstance(expected, list): |
| 67 | + return actual_str.lower() in [str(v).strip().lower() for v in expected] |
| 68 | + # Fallback: comma-separated string |
| 69 | + return actual_str.lower() in [ |
| 70 | + v.strip().lower() for v in expected_str.split(",") |
| 71 | + ] |
| 72 | + |
| 73 | + # Numeric comparisons |
| 74 | + actual_num = _coerce_numeric(actual) |
| 75 | + expected_num = _coerce_numeric(expected) |
| 76 | + if actual_num is None or expected_num is None: |
| 77 | + logger.debug( |
| 78 | + "Non-numeric comparison attempted: field=%s op=%s actual=%r expected=%r", |
| 79 | + field, |
| 80 | + operator, |
| 81 | + actual, |
| 82 | + expected, |
| 83 | + ) |
| 84 | + return False |
| 85 | + |
| 86 | + if operator == "gt": |
| 87 | + return actual_num > expected_num |
| 88 | + if operator == "lt": |
| 89 | + return actual_num < expected_num |
| 90 | + if operator == "gte": |
| 91 | + return actual_num >= expected_num |
| 92 | + if operator == "lte": |
| 93 | + return actual_num <= expected_num |
| 94 | + |
| 95 | + logger.warning("Unknown condition operator: %s", operator) |
| 96 | + return False |
| 97 | + |
| 98 | + |
| 99 | +def evaluate_conditions(conditions: dict | None, data: dict) -> bool: |
| 100 | + """Evaluate a trigger_conditions rule set against form data. |
| 101 | +
|
| 102 | + Returns ``True`` when: |
| 103 | + - ``conditions`` is ``None``, empty dict, or has no ``conditions`` list |
| 104 | + (unconditional — always matches) |
| 105 | + - All / any individual conditions pass (depending on the top-level operator) |
| 106 | + """ |
| 107 | + if not conditions: |
| 108 | + return True |
| 109 | + |
| 110 | + condition_list = conditions.get("conditions") |
| 111 | + if not condition_list: |
| 112 | + return True |
| 113 | + |
| 114 | + group_operator = conditions.get("operator", "AND").upper() |
| 115 | + |
| 116 | + results = [_evaluate_single(c, data) for c in condition_list] |
| 117 | + |
| 118 | + if group_operator == "OR": |
| 119 | + return any(results) |
| 120 | + # Default: AND |
| 121 | + return all(results) |
0 commit comments