diff --git a/spreadsheet_oca/README.rst b/spreadsheet_oca/README.rst index 20fb5ca4..3f2808b9 100644 --- a/spreadsheet_oca/README.rst +++ b/spreadsheet_oca/README.rst @@ -11,7 +11,7 @@ Spreadsheet Oca !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:1310916cafc940d7dcb638553bb2072e73f47cc887916d5c384b1cec841b4223 + !! source digest: sha256:afa8556b48ef425ac88f6fa7b69a9f5d76aaef94958f9ce0e14e0efd97024dc0 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png diff --git a/spreadsheet_oca/__manifest__.py b/spreadsheet_oca/__manifest__.py index 4b6779cc..84f9ffbe 100644 --- a/spreadsheet_oca/__manifest__.py +++ b/spreadsheet_oca/__manifest__.py @@ -5,7 +5,7 @@ "name": "Spreadsheet Oca", "summary": """ Allow to edit spreadsheets""", - "version": "18.0.1.3.1", + "version": "18.0.1.3.0", "license": "AGPL-3", "author": "CreuBlanca,Odoo Community Association (OCA)", "website": "https://github.com/OCA/spreadsheet", @@ -14,7 +14,10 @@ "security/security.xml", "security/ir.model.access.csv", "views/spreadsheet_spreadsheet.xml", + "views/spreadsheet_alert_views.xml", + "data/mail_templates.xml", "data/spreadsheet_spreadsheet_import_mode.xml", + "data/spreadsheet_alert_cron.xml", "wizards/spreadsheet_select_row_number.xml", "wizards/spreadsheet_spreadsheet_import.xml", ], diff --git a/spreadsheet_oca/data/mail_templates.xml b/spreadsheet_oca/data/mail_templates.xml new file mode 100644 index 00000000..8ebbf6b2 --- /dev/null +++ b/spreadsheet_oca/data/mail_templates.xml @@ -0,0 +1,105 @@ + + + + + + spreadsheet.alert.notification + qweb + + +
+ +
+ ⚠ KPI Alert Triggered +
+ +
+

+ +

+ + + + + + + + + + + + + +
Cell + + + + + on sheet + + + +
Value + + + +
Condition + +   + +
+ + +
+
+
+
+
+
diff --git a/spreadsheet_oca/data/spreadsheet_alert_cron.xml b/spreadsheet_oca/data/spreadsheet_alert_cron.xml new file mode 100644 index 00000000..ea4124be --- /dev/null +++ b/spreadsheet_oca/data/spreadsheet_alert_cron.xml @@ -0,0 +1,14 @@ + + + + + Spreadsheet KPI Alert Evaluation + + code + model._cron_evaluate_all() + 1 + hours + True + + diff --git a/spreadsheet_oca/demo/demo_kpi_dashboard.json b/spreadsheet_oca/demo/demo_kpi_dashboard.json new file mode 100644 index 00000000..ba59dfac --- /dev/null +++ b/spreadsheet_oca/demo/demo_kpi_dashboard.json @@ -0,0 +1,85 @@ +{ + "version": 21, + "sheets": [ + { + "id": "dashboard", + "name": "Dashboard", + "colNumber": 26, + "rowNumber": 100, + "rows": {}, + "cols": {}, + "merges": [], + "cells": { + "A1": {"content": "KPI", "style": 1, "border": 1}, + "B1": {"content": "Target", "style": 1, "border": 1}, + "C1": {"content": "Actual", "style": 1, "border": 1}, + "D1": {"content": "Variance", "style": 1, "border": 1}, + "E1": {"content": "Status", "style": 1, "border": 1}, + "A2": {"content": "Cost Per Lead"}, + "B2": {"content": "50", "format": 1}, + "C2": {"content": "42", "format": 1}, + "D2": {"content": "=C2-B2", "format": 1}, + "E2": {"content": "=IF(C2<=B2,\"On Track\",\"Over\")"}, + "A3": {"content": "Revenue Growth"}, + "B3": {"content": "0.15", "format": 2}, + "C3": {"content": "0.12", "format": 2}, + "D3": {"content": "=C3-B3", "format": 2}, + "E3": {"content": "=IF(C3>=B3,\"On Track\",\"Below\")"}, + "A4": {"content": "Customer Churn"}, + "B4": {"content": "0.05", "format": 2}, + "C4": {"content": "0.032", "format": 2}, + "D4": {"content": "=C4-B4", "format": 2}, + "E4": {"content": "=IF(C4<=B4,\"On Track\",\"High\")"}, + "A5": {"content": "Avg Deal Size"}, + "B5": {"content": "25000", "format": 1}, + "C5": {"content": "28500", "format": 1}, + "D5": {"content": "=C5-B5", "format": 1}, + "E5": {"content": "=IF(C5>=B5,\"On Track\",\"Low\")"}, + "A6": {"content": "NPS Score"}, + "B6": {"content": "70"}, + "C6": {"content": "78"}, + "D6": {"content": "=C6-B6"}, + "E6": {"content": "=IF(C6>=B6,\"On Track\",\"Low\")"}, + "A8": {"content": "Last Updated"}, + "B8": {"content": "2026-03-01"}, + "A9": {"content": "Updated By"}, + "B9": {"content": "Admin"} + }, + "conditionalFormats": [], + "figures": [], + "filterTables": [], + "tables": [], + "dataValidationRules": [], + "comments": {}, + "headerGroups": {"ROW": [], "COL": []}, + "areGridLinesVisible": true, + "isVisible": true + } + ], + "settings": {}, + "customTableStyles": {}, + "styles": { + "1": {"bold": true, "align": "center"} + }, + "formats": { + "1": "$#,##0", + "2": "0.00%" + }, + "borders": { + "1": { + "top": ["thin", "#000"], + "bottom": ["thin", "#000"], + "left": ["thin", "#000"], + "right": ["thin", "#000"] + } + }, + "revisionId": "START_REVISION", + "uniqueFigureIds": true, + "odooVersion": 12, + "globalFilters": [], + "pivots": {}, + "pivotNextId": 1, + "lists": {}, + "listNextId": 1, + "chartOdooMenusReferences": {} +} diff --git a/spreadsheet_oca/demo/demo_pivot_dashboard.json b/spreadsheet_oca/demo/demo_pivot_dashboard.json new file mode 100644 index 00000000..2ed2f20e --- /dev/null +++ b/spreadsheet_oca/demo/demo_pivot_dashboard.json @@ -0,0 +1,98 @@ +{ + "version": 21, + "sheets": [ + { + "id": "sheet_partners", + "name": "Partners by Country", + "colNumber": 26, + "rowNumber": 100, + "rows": {}, + "cols": { + "0": {"size": 220}, + "1": {"size": 140}, + "2": {"size": 140}, + "3": {"size": 140} + }, + "merges": [], + "cells": { + "A1": {"content": "=PIVOT(1)"} + }, + "conditionalFormats": [], + "figures": [], + "filterTables": [], + "tables": [], + "dataValidationRules": [], + "comments": {}, + "headerGroups": {"ROW": [], "COL": []}, + "areGridLinesVisible": true, + "isVisible": true + }, + { + "id": "sheet_regions", + "name": "Regions per Country", + "colNumber": 26, + "rowNumber": 100, + "rows": {}, + "cols": { + "0": {"size": 220}, + "1": {"size": 140} + }, + "merges": [], + "cells": { + "A1": {"content": "=PIVOT(2)"} + }, + "conditionalFormats": [], + "figures": [], + "filterTables": [], + "tables": [], + "dataValidationRules": [], + "comments": {}, + "headerGroups": {"ROW": [], "COL": []}, + "areGridLinesVisible": true, + "isVisible": true + } + ], + "settings": {}, + "customTableStyles": {}, + "styles": {}, + "formats": {}, + "borders": {}, + "revisionId": "START_REVISION", + "uniqueFigureIds": true, + "odooVersion": 12, + "globalFilters": [], + "pivots": { + "1": { + "type": "ODOO", + "id": "1", + "formulaId": "1", + "name": "Partners by Country & Type", + "model": "res.partner", + "domain": [["active", "=", true]], + "context": {}, + "measures": [{"id": "__count", "fieldName": "__count"}], + "rows": [{"fieldName": "country_id", "order": "desc"}], + "columns": [{"fieldName": "is_company"}], + "sortedColumn": null, + "fieldMatching": {} + }, + "2": { + "type": "ODOO", + "id": "2", + "formulaId": "2", + "name": "Regions per Country", + "model": "res.country.state", + "domain": [], + "context": {}, + "measures": [{"id": "__count", "fieldName": "__count"}], + "rows": [{"fieldName": "country_id", "order": "desc"}], + "columns": [], + "sortedColumn": null, + "fieldMatching": {} + } + }, + "pivotNextId": 3, + "lists": {}, + "listNextId": 1, + "chartOdooMenusReferences": {} +} diff --git a/spreadsheet_oca/demo/spreadsheet_spreadsheet.xml b/spreadsheet_oca/demo/spreadsheet_spreadsheet.xml index 11222ed5..c0e3f876 100644 --- a/spreadsheet_oca/demo/spreadsheet_spreadsheet.xml +++ b/spreadsheet_oca/demo/spreadsheet_spreadsheet.xml @@ -1,11 +1,128 @@ + + + + Müller GmbH + + + + + Hans Weber + + + + + Dupont SA + + + + + Marie Leclerc + + + + + British Solutions Ltd + + + + + James Clarke + + + + + Tanaka Industries + + + + + Silva Comércio Ltda + + + + + Ana Costa + + + + Patel Technologies Pvt Ltd + + + + + Priya Sharma + + + + + Outback Systems Pty Ltd + + + + + + - Demo spreadsheet + Sales Pipeline Summary + + + + KPI Dashboard + + + + + + Partner Pivot Dashboard + + + + + + + + + Revenue Growth Below Target + + C3 + Dashboard + + 0.15 + edge + + + + + Cost Per Lead Over Target + + C2 + Dashboard + + 50 + level + diff --git a/spreadsheet_oca/models/__init__.py b/spreadsheet_oca/models/__init__.py index 3e07c12a..b8ad8d58 100644 --- a/spreadsheet_oca/models/__init__.py +++ b/spreadsheet_oca/models/__init__.py @@ -1,7 +1,9 @@ from . import ir_model +from . import cell_ref # noqa: F401 — helpers shared by the pivot features from . import spreadsheet_abstract from . import spreadsheet_spreadsheet_tag from . import spreadsheet_spreadsheet from . import spreadsheet_oca_revision from . import ir_websocket from . import spreadsheet_spreadsheet_import_mode +from . import spreadsheet_alert diff --git a/spreadsheet_oca/models/cell_ref.py b/spreadsheet_oca/models/cell_ref.py new file mode 100644 index 00000000..4e41851b --- /dev/null +++ b/spreadsheet_oca/models/cell_ref.py @@ -0,0 +1,140 @@ +# Copyright 2026 Ledo Enterprises LLC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +""" +Shared cell-reference helpers for spreadsheet_oca. + +Used by spreadsheet_alert, spreadsheet_scenario, and spreadsheet_input_param +to avoid duplicating cell-address parsing and raw-JSON access logic. +""" + +import re + +# Pre-compiled pattern: column letters + row number (1-based, no zero row). +_CELL_REF_RE = re.compile(r"^([A-Za-z]+)([1-9][0-9]*)$") + + +def _idx_to_cell_address(col_idx, row_idx): + """Convert 0-based (col, row) to cell address like 'A1', 'B3', 'AA12'.""" + col_str = "" + c = col_idx + while True: + col_str = chr(ord("A") + c % 26) + col_str + c = c // 26 - 1 + if c < 0: + break + return f"{col_str}{row_idx + 1}" + + +def parse_cell_ref(ref): + """ + Parse a bare cell reference like 'B3' or 'AA12' into (col_index, row_index). + + Both indices are 0-based to match the o-spreadsheet JSON cell-map format. + Returns (None, None) on invalid input (empty string, zero row, etc.). + """ + m = _CELL_REF_RE.match(ref.strip()) + if not m: + return None, None + col_str, row_str = m.group(1).upper(), m.group(2) + col_idx = 0 + for ch in col_str: + col_idx = col_idx * 26 + (ord(ch) - ord("A") + 1) + col_idx -= 1 # convert to 0-based + row_idx = int(row_str) - 1 # convert to 0-based + return col_idx, row_idx + + +def parse_cell_key(key): + """ + Parse a possibly-qualified cell key into (sheet_name_or_None, col_idx, row_idx). + + Supported formats: + - ``"B3"`` — no sheet qualifier; sheet_name = None + - ``"Sheet1!B3"`` — explicit sheet qualifier + """ + key = key.strip() + if "!" in key: + sheet_part, addr_part = key.split("!", 1) + sheet_name = sheet_part.strip() + else: + sheet_name = None + addr_part = key + col_idx, row_idx = parse_cell_ref(addr_part) + return sheet_name, col_idx, row_idx + + +def _resolve_sheet(sheets, sheet_name=None): + """Return the target sheet dict from a list of sheets. + + If *sheet_name* is given, searches case-insensitively; falls back to the + first sheet if not found. Returns None when *sheets* is empty. + """ + if not sheets: + return None + if sheet_name: + for s in sheets: + if s.get("name", "").lower() == sheet_name.lower(): + return s + return sheets[0] + + +def read_cell_value(spreadsheet_raw, cell_ref, sheet_name=None): + """ + Read the value of a cell from a spreadsheet_raw JSON dict. + + *cell_ref* may be bare (``"B3"``) or sheet-qualified (``"Sheet1!B3"``). + *sheet_name*, when provided, overrides any sheet qualifier embedded in + *cell_ref* and forces lookup in the named sheet (falling back to sheet 0). + + Return value priority: + 1. The cell's evaluated ``"value"`` key (set by o-spreadsheet when the + workbook is saved after formula evaluation in the browser). + 2. The cell's ``"content"`` string (for static / hand-typed cells). + 3. ``None`` when the cell, sheet, or raw JSON is absent. + """ + sheets = (spreadsheet_raw or {}).get("sheets", []) + ref_sheet, col_idx, row_idx = parse_cell_key(cell_ref) + if col_idx is None: + return None + + target_name = sheet_name or ref_sheet + target_sheet = _resolve_sheet(sheets, target_name) + if target_sheet is None: + return None + + cells = target_sheet.get("cells", {}) + cell_addr = _idx_to_cell_address(col_idx, row_idx) + cell_data = cells.get(cell_addr, {}) + if not cell_data: + return None + + value = cell_data.get("value") + if value is None: + value = cell_data.get("content") + return value if value != "" else None + + +def write_cell_content(spreadsheet_raw, cell_ref, value, sheet_name=None): + """ + Write a value into ``cells[row][col]["content"]`` of *spreadsheet_raw* in-place. + + Creates nested dicts as needed. *cell_ref* and *sheet_name* follow the + same conventions as :func:`read_cell_value`. + + Returns the (mutated) *spreadsheet_raw* dict. + """ + sheets = (spreadsheet_raw or {}).get("sheets", []) + ref_sheet, col_idx, row_idx = parse_cell_key(cell_ref) + if col_idx is None: + return spreadsheet_raw + + target_name = sheet_name or ref_sheet + target_sheet = _resolve_sheet(sheets, target_name) + if target_sheet is None: + return spreadsheet_raw + + cells = target_sheet.setdefault("cells", {}) + cell_addr = _idx_to_cell_address(col_idx, row_idx) + cell_data = cells.setdefault(cell_addr, {}) + cell_data["content"] = str(value) if value is not None else "" + return spreadsheet_raw diff --git a/spreadsheet_oca/models/pivot_data.py b/spreadsheet_oca/models/pivot_data.py new file mode 100644 index 00000000..916158cd --- /dev/null +++ b/spreadsheet_oca/models/pivot_data.py @@ -0,0 +1,422 @@ +# Copyright 2026 Ledo Enterprises LLC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +""" +Server-side pivot data helper. + +Replicates the read_group strategy used by the Odoo web PivotModel +(addons/web/static/src/views/pivot/pivot_model.js) to produce pivot table +data server-side, without executing any JavaScript. + +The JS pivot loads data by: + 1. Computing all row-groupby prefixes ("sections"): + rows=["partner_id","date:month"] → [[], ["partner_id"], + ["partner_id","date:month"]] + 2. Computing all col-groupby prefixes ("sections"): + cols=["stage_id"] → [[], ["stage_id"]] + 3. Taking the cartesian product (row_prefix × col_prefix) for "divisors". + 4. For each divisor [rowPrefix, colPrefix], calling: + read_group(domain, fields=measureSpecs, + groupby=rowPrefix+colPrefix, lazy=False) + +This module replicates that strategy in Python and exposes: + - ``get_pivot_data(model, domain, context, rows, columns, measures)`` + +Rows / columns are lists of dimension dicts: + {"fieldName": "date_order", "granularity": "month"} + {"fieldName": "partner_id"} (no granularity) + +Measures are lists of measure dicts: + {"fieldName": "amount_total", "aggregator": "sum"} + {"fieldName": "__count"} +""" + +import itertools +import logging + +from odoo.exceptions import AccessError + +_logger = logging.getLogger(__name__) + +# --------------------------------------------------------------------------- +# Helpers mirroring the JS helpers in pivot_model.js +# --------------------------------------------------------------------------- + +DATE_GRANULARITIES = {"day", "week", "month", "quarter", "year"} + +# Shown when a group key is empty (read_group returns False for "not set"). +_NONE_LABEL = "None" + + +def _dimension_to_groupby(dim): + """Convert a dimension dict to an Odoo read_group groupby string. + + {"fieldName": "date_order", "granularity": "month"} → "date_order:month" + {"fieldName": "partner_id"} → "partner_id" + """ + name = dim["fieldName"] + gran = dim.get("granularity") + return f"{name}:{gran}" if gran else name + + +def _sections(lst): + """Return all prefixes of lst including the empty prefix. + + sections(["a", "b", "c"]) → [[], ["a"], ["a", "b"], ["a", "b", "c"]] + + Mirrors the JS ``sections()`` helper. + """ + return [lst[:i] for i in range(len(lst) + 1)] + + +def _measure_to_field_spec(measure): + """Convert a measure dict to a read_group ``fields`` element. + + {"fieldName": "amount_total", "aggregator": "sum"} → "amount_total:sum" + {"fieldName": "__count"} → "__count" + """ + if measure["fieldName"] == "__count": + return "__count" + agg = measure.get("aggregator") or "sum" + return f"{measure['fieldName']}:{agg}" + + +# --------------------------------------------------------------------------- +# Main computation +# --------------------------------------------------------------------------- + + +def get_pivot_data(env, model_name, domain, context, row_dims, col_dims, measures): + """Compute pivot table data using the same read_group strategy as the JS. + + Returns a dict: + { + "fields": {fieldName: {type, string, ...}}, + "groups": [ + { + "rowValues": ["2026-01", ...], # normalised group key values + "colValues": ["Confirmed", ...], + "rowLabels": ["January 2026", ...], # human-readable + "colLabels": ["Confirmed", ...], + "rowGroupBy": ["date_order:month"], + "colGroupBy": ["stage_id"], + "count": 12, + "measures": {"amount_total:sum": 9800.0, ...}, + }, + ... + ], + "rowDimensions": [{"fieldName": ..., "granularity": ...}, ...], + "colDimensions": [{"fieldName": ..., "granularity": ...}, ...], + "measureSpecs": ["amount_total:sum", ...], + "partial": False, # True if any subtotal query failed + "failedGroupBys": [], # the groupbys that failed, when partial + } + + An :class:`~odoo.exceptions.AccessError` propagates rather than degrading + to an empty subtotal, so a permission problem is never silently rendered + as a zero. + """ + Model = env[model_name].with_context(**(context or {})) + + # ── 1. Fields metadata (needed for label resolution) ───────────────── + all_field_names = [d["fieldName"] for d in row_dims + col_dims] + [ + m["fieldName"] for m in measures if m["fieldName"] != "__count" + ] + # fields_get returns {fieldName: {type, string, selection, ...}} + fields_meta = Model.fields_get( + all_field_names, attributes=["type", "string", "selection"] + ) + + # ── 2. Build groupby strings ────────────────────────────────────────── + row_groupbys = [_dimension_to_groupby(d) for d in row_dims] + col_groupbys = [_dimension_to_groupby(d) for d in col_dims] + measure_specs = [_measure_to_field_spec(m) for m in measures] + + # Ensure count is always fetched (JS always adds __count implicitly) + field_specs_with_count = measure_specs + ( + [] if "__count" in measure_specs else ["__count"] + ) + + # ── 3. Compute divisors (cartesian product of all prefixes) ────────── + row_sections = _sections(row_groupbys) + col_sections = _sections(col_groupbys) + divisors = list(itertools.product(row_sections, col_sections)) + + # ── 4. Fire read_group for each divisor ────────────────────────────── + groups = [] + failed_groupbys = [] + for row_prefix, col_prefix in divisors: + groupby = row_prefix + col_prefix + try: + # read_group is still the public API in 18.0 and is what the JS + # pivot uses. It is kept here (rather than _read_group) because + # this code needs the formatted values and __domain that only + # read_group returns. 19.0 deprecates it in favour of + # formatted_read_group — this is the single call site to port. + results = Model.read_group( + domain=domain or [], + fields=field_specs_with_count, + groupby=groupby, + lazy=False, + ) + except AccessError: + # Never mask a permission problem as an empty subtotal. + raise + except Exception: + # One malformed divisor must not lose the whole pivot, but the + # caller has to be able to tell a partial result from a complete + # one — hence ``partial``/``failedGroupBys`` in the return value. + _logger.exception( + "read_group failed for model=%s groupby=%s", model_name, groupby + ) + failed_groupbys.append(groupby) + continue + + for rg in results: + group_entry = { + "rowGroupBy": row_prefix, + "colGroupBy": col_prefix, + "rowValues": _extract_group_values(rg, row_prefix, fields_meta), + "colValues": _extract_group_values(rg, col_prefix, fields_meta), + "rowLabels": _extract_group_labels(rg, row_prefix, fields_meta), + "colLabels": _extract_group_labels(rg, col_prefix, fields_meta), + "count": rg.get("__count", 0), + "measures": _extract_measures(rg, measures, fields_meta), + "domain": rg.get("__domain", []), + } + groups.append(group_entry) + + return { + "fields": fields_meta, + "groups": groups, + "rowDimensions": row_dims, + "colDimensions": col_dims, + "measureSpecs": measure_specs, + "partial": bool(failed_groupbys), + "failedGroupBys": failed_groupbys, + } + + +def _extract_group_values(rg_row, groupby_list, fields_meta): + """Extract normalised group values from a read_group result row. + + Many2one fields return (id, display_name) — we normalise to the id (int). + Date/datetime fields return a formatted string (Odoo already handles + granularity in the groupby key). + """ + values = [] + for gb_spec in groupby_list: + field_name = gb_spec.split(":")[0] + raw = rg_row.get(gb_spec) or rg_row.get(field_name) + if raw is False or raw is None: + values.append(False) + elif isinstance(raw, list | tuple) and len(raw) == 2: + # Many2one: (id, display_name) — store id; JS uses id for grouping + values.append(raw[0]) + else: + values.append(raw) + return values + + +def _extract_group_labels(rg_row, groupby_list, fields_meta): + """Human-readable counterpart of :func:`_extract_group_values`. + + The values are ids because that is what grouping needs, but anything that + renders a group to a human — Chatter summaries, digests, XLSX — needs the + label instead, or a many2one group shows up as a bare database id. + """ + labels = [] + for gb_spec in groupby_list: + field_name = gb_spec.split(":")[0] + raw = rg_row.get(gb_spec) or rg_row.get(field_name) + meta = fields_meta.get(field_name) or {} + if raw is False or raw is None: + labels.append(_NONE_LABEL) + elif isinstance(raw, list | tuple) and len(raw) == 2: + labels.append(raw[1]) # many2one display_name + elif meta.get("type") == "selection": + selection = dict(meta.get("selection") or []) + labels.append(selection.get(raw, raw)) + else: + labels.append(raw) + return [str(label) for label in labels] + + +def _extract_measures(rg_row, measures, fields_meta): + """Extract measure values from a read_group result row.""" + result = {} + for measure in measures: + fname = measure["fieldName"] + agg = measure.get("aggregator") + if fname == "__count": + result["__count"] = rg_row.get("__count", 0) + continue + # read_group key: field_name (no aggregator suffix in result keys) + raw = rg_row.get(fname, 0) + if isinstance(raw, list | tuple): + # Many2one used as measure — count distinct occurrences + raw = 1 if raw else 0 + if raw is False: + raw = 0 + spec_key = f"{fname}:{agg}" if agg else fname + result[spec_key] = raw + return result + + +# --------------------------------------------------------------------------- +# Shared helpers for pivot iteration and HTML rendering +# --------------------------------------------------------------------------- + + +def collect_pivot_summaries(env, spreadsheet_raw, domain_transform=None): + """Iterate over ODOO-type pivots and return fresh data for each. + + Args: + env: Odoo environment. + spreadsheet_raw: dict — the spreadsheet's raw JSON data. + domain_transform: optional callable(domain) -> domain, applied to each + pivot's domain before querying (e.g. parameter substitution). + + Returns: + A tuple ``(summaries, failed_names)`` where *summaries* is a list of + ``{"name": ..., "model": ..., "result": ...}`` dicts, and + *failed_names* is a list of pivot display names that could not be loaded. + """ + pivots = spreadsheet_raw.get("pivots", {}) + summaries = [] + failed_names = [] + for pivot_id, pivot_def in pivots.items(): + if pivot_def.get("type") != "ODOO": + continue + model_name = pivot_def.get("model") + pivot_name = pivot_def.get("name") or f"Pivot #{pivot_id}" + if not model_name or model_name not in env: + _logger.warning( + "collect_pivot_summaries: unknown model %r — skipping pivot %s", + model_name, + pivot_id, + ) + failed_names.append(pivot_name) + continue + try: + domain = pivot_def.get("domain", []) + if domain_transform: + domain = domain_transform(domain) + result = get_pivot_data( + env, + model_name, + domain, + pivot_def.get("context", {}), + pivot_def.get("rows", []), + pivot_def.get("columns", []), + pivot_def.get("measures", []), + ) + summaries.append( + { + "name": pivot_name, + "model": model_name, + "result": result, + } + ) + except Exception: + _logger.exception( + "collect_pivot_summaries: failed to compute pivot %s", + pivot_id, + ) + failed_names.append(pivot_name) + return summaries, failed_names + + +def render_pivot_table_html(summary, max_rows=10): + """Render a single pivot summary as an HTML table string. + + Args: + summary: dict with keys ``"name"``, ``"model"``, ``"result"`` + (as returned by ``collect_pivot_summaries``). + max_rows: maximum number of detail rows to include before truncating. + + Returns: + str — HTML fragment for the pivot table. + """ + result = summary["result"] + name = summary["name"] + model = summary["model"] + parts = [] + + parts.append( + f'

{name}' + f' ({model})

' + ) + + row_dims = result.get("rowDimensions", []) + groups = result.get("groups", []) + + # Grand total row + grand_totals = [ + g for g in groups if g["rowGroupBy"] == [] and g["colGroupBy"] == [] + ] + if grand_totals: + gt = grand_totals[0] + count = gt.get("count", 0) + parts.append( + f'

Total records: {count}

' + ) + for key, val in gt.get("measures", {}).items(): + if key != "__count" and val is not None: + parts.append( + f'

{key}: {val}

' + ) + + # Row breakdown table + if row_dims: + row_gb = [d["fieldName"] for d in row_dims] + row_groups = [ + g for g in groups if g["rowGroupBy"] == row_gb and g["colGroupBy"] == [] + ] + if row_groups: + measure_keys = [ + k for k in (row_groups[0].get("measures") or {}) if k != "__count" + ] + headers = ["Group"] + measure_keys + ["Count"] + parts.append( + '' + ) + parts.append("") + for h in headers: + parts.append( + '' + ) + parts.append("") + for g in row_groups[:max_rows]: + label = ", ".join( + g.get("rowLabels") or [str(v) for v in g["rowValues"]] + ) + parts.append("") + parts.append( + f'' + ) + for mk in measure_keys: + val = g.get("measures", {}).get(mk, "") + parts.append( + '' + ) + parts.append( + ''.format(g.get("count", "")) + ) + parts.append("") + if len(row_groups) > max_rows: + colspan = len(headers) + extra = len(row_groups) - max_rows + more_text = f"and {extra} more rows" + parts.append( + f'" + ) + parts.append("
{h}
{label}{val}{}
' + f"… {more_text}
") + + return "".join(parts) diff --git a/spreadsheet_oca/models/spreadsheet_alert.py b/spreadsheet_oca/models/spreadsheet_alert.py new file mode 100644 index 00000000..e12656b1 --- /dev/null +++ b/spreadsheet_oca/models/spreadsheet_alert.py @@ -0,0 +1,256 @@ +# Copyright 2025 Ledo Enterprises LLC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +""" +Threshold alerts / KPI watches. + +Users define a watch on a named cell in a spreadsheet. A shared cron +periodically evaluates the cell's current value (computed server-side +by calling _get_pivot_data for the first matching pivot at that cell, +or by reading the static value from spreadsheet_raw) and fires a +Discuss/email notification when the threshold is crossed. + +Two trigger modes: + edge — notify only on the first evaluation that crosses the threshold + (stays silent until the condition resets and re-triggers) + level — notify on every cron cycle where the condition holds + +Operators: >, >=, <, <=, ==, != +""" + +import logging +import operator as _op + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + +from .cell_ref import parse_cell_ref, read_cell_value + +_logger = logging.getLogger(__name__) + +_OPERATORS = [ + (">", "> (greater than)"), + (">=", ">= (greater or equal)"), + ("<", "< (less than)"), + ("<=", "<= (less or equal)"), + ("==", "== (equal to)"), + ("!=", "!= (not equal to)"), +] + +_TRIGGER_MODES = [ + ("edge", "Edge — notify once when threshold is first crossed"), + ("level", "Level — notify every cycle the condition holds"), +] + +# Maps operator selection values to Python comparison callables. +_OP_FUNCS = { + ">": _op.gt, + ">=": _op.ge, + "<": _op.lt, + "<=": _op.le, + "==": _op.eq, + "!=": _op.ne, +} + + +class SpreadsheetAlert(models.Model): + _name = "spreadsheet.alert" + _description = "Spreadsheet KPI Threshold Alert" + _inherit = ["mail.thread"] + _order = "spreadsheet_id, name" + + name = fields.Char(required=True, tracking=True) + spreadsheet_id = fields.Many2one( + "spreadsheet.spreadsheet", + required=True, + ondelete="cascade", + index=True, + ) + active = fields.Boolean(default=True, tracking=True) + user_id = fields.Many2one( + "res.users", + string="Run As", + required=True, + default=lambda self: self.env.user, + tracking=True, + help="The alert is evaluated with this user's permissions, so it can " + "only ever report on data they are allowed to read.", + ) + + # ── Cell reference ──────────────────────────────────────────────────────── + sheet_name = fields.Char( + help="Sheet containing the watched cell (blank = first sheet).", + ) + cell_ref = fields.Char( + string="Cell Reference", + required=True, + help="Spreadsheet cell reference, e.g. B3 or D12.", + tracking=True, + ) + + # ── Threshold ──────────────────────────────────────────────────────────── + operator = fields.Selection( + _OPERATORS, + required=True, + default=">", + tracking=True, + ) + threshold = fields.Float(required=True, tracking=True) + + # ── Trigger mode ───────────────────────────────────────────────────────── + trigger_mode = fields.Selection( + _TRIGGER_MODES, + default="edge", + required=True, + tracking=True, + help=( + "Edge: notify once when the condition changes from False to True. " + "Level: notify every cron cycle the condition is True." + ), + ) + last_state = fields.Boolean( + default=False, + readonly=True, + copy=False, + help="Previous evaluation state (used for edge mode).", + ) + last_value = fields.Float(readonly=True, copy=False) + last_checked = fields.Datetime(readonly=True, copy=False) + + # ── Notification ───────────────────────────────────────────────────────── + notify_partner_ids = fields.Many2many( + "res.partner", + string="Notify Partners", + help="Notified by email when the threshold is crossed.", + ) + + @api.constrains("cell_ref") + def _check_cell_ref(self): + for rec in self: + col, _row = parse_cell_ref(rec.cell_ref.strip()) + if col is None: + raise ValidationError( + _( + "Cell reference %(cell_ref)s must be in the form" + " 'A1', 'B12', etc.", + cell_ref=rec.cell_ref, + ) + ) + + # ── Shared cron ─────────────────────────────────────────────────────────── + + @api.model + def _cron_evaluate_all(self): + """Called by the shared ir.cron: evaluate all active alerts.""" + alerts = self.search([("active", "=", True)]) + for alert in alerts: + try: + alert.with_user(alert.user_id)._evaluate() + except Exception: + _logger.exception( + "Failed to evaluate alert %s (%s)", alert.id, alert.name + ) + + # ── Single alert evaluation ─────────────────────────────────────────────── + + def _evaluate(self): + """ + Evaluate this alert's cell value and fire a notification if the + threshold condition is met (subject to trigger_mode). + """ + self.ensure_one() + value = self._read_cell_value() + if value is None: + # Not debug: an alert whose cell cannot be read never fires, and + # silence is indistinguishable from "threshold not crossed". + _logger.warning( + "Alert %s (%s): cell %s on sheet %s is missing or non-numeric — " + "this alert cannot fire", + self.id, + self.name, + self.cell_ref, + self.sheet_name or "(first)", + ) + # sudo: bookkeeping. The alert is evaluated as its own user, who + # only needs *read* rights on the data — writing the evaluation + # state back is the module's own housekeeping, not a user edit. + self.sudo().write({"last_checked": fields.Datetime.now()}) + return + + condition_met = self._check_condition(value) + now = fields.Datetime.now() + + should_notify = False + if self.trigger_mode == "level": + should_notify = condition_met + else: # edge + should_notify = condition_met and not self.last_state + + if should_notify: + self._fire_notification(value) + + # sudo: see _evaluate's other write — evaluation state is housekeeping. + self.sudo().write( + { + "last_state": condition_met, + "last_value": value, + "last_checked": now, + } + ) + + def _check_condition(self, value): + """Return True if value satisfies operator(value, threshold).""" + func = _OP_FUNCS.get(self.operator) + return func(value, self.threshold) if func else False + + def _read_cell_value(self): + """ + Read the current numeric value of the watched cell from spreadsheet_raw. + + Uses the shared ``read_cell_value`` helper to locate the cell, then + converts the result to float. Returns None if the cell is absent or + non-numeric. + + Note: For formula cells (=PIVOT(…)), the stored value in spreadsheet_raw + is whatever was last computed client-side and saved. + """ + raw = self.spreadsheet_id.sudo().spreadsheet_raw or {} + value = read_cell_value(raw, self.cell_ref.strip(), self.sheet_name or None) + if value is None: + return None + try: + return float(str(value).replace(",", ".")) + except (ValueError, TypeError): + return None + + def _fire_notification(self, value): + """Post a Chatter alert message and email subscribers.""" + op_label = dict(_OPERATORS).get(self.operator, self.operator) + base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url") + body = self.env["ir.qweb"]._render( + "spreadsheet_oca.spreadsheet_alert_notification_template", + { + "alert": self, + "value_str": f"{value:.4g}", + "op_label": op_label, + "threshold_str": f"{self.threshold:.4g}", + "base_url": base_url, + }, + ) + + self.spreadsheet_id.sudo().message_post( + body=body, + subject=_("KPI Alert: %(name)s", name=self.name), + partner_ids=self.notify_partner_ids.ids, + subtype_xmlid=( + "mail.mt_comment" if self.notify_partner_ids else "mail.mt_note" + ), + ) + + def action_evaluate_now(self): + """Manually trigger evaluation of this alert.""" + self.ensure_one() + self._evaluate() + + def action_reset_state(self): + """Reset last_state so an edge alert can trigger again.""" + self.write({"last_state": False}) diff --git a/spreadsheet_oca/models/spreadsheet_spreadsheet.py b/spreadsheet_oca/models/spreadsheet_spreadsheet.py index 55a9ae9f..f61a4ab5 100644 --- a/spreadsheet_oca/models/spreadsheet_spreadsheet.py +++ b/spreadsheet_oca/models/spreadsheet_spreadsheet.py @@ -56,10 +56,53 @@ class SpreadsheetSpreadsheet(models.Model): string="Tags", comodel_name="spreadsheet.spreadsheet.tag" ) + # ── DRY helper for read_group-based count fields ───────────────────────── + + def _compute_related_count(self, comodel, field_name, extra_domain=None): + """Compute a count field by grouping *comodel* on ``spreadsheet_id``. + + By default the domain filters on ``active=True``; pass *extra_domain* + to override (e.g. ``[("status", "!=", "error")]`` for writeback logs). + """ + domain = [("spreadsheet_id", "in", self.ids)] + if extra_domain is not None: + domain += extra_domain + else: + domain.append(("active", "=", True)) + counts = self.env[comodel].read_group( + domain, ["spreadsheet_id"], ["spreadsheet_id"] + ) + count_map = {c["spreadsheet_id"][0]: c["spreadsheet_id_count"] for c in counts} + for rec in self: + rec[field_name] = count_map.get(rec.id, 0) + @api.depends("name") def _compute_filename(self): for record in self: - record.filename = "%s.json" % (self.name or _("Unnamed")) + record.filename = f"{record.name or _('Unnamed')}.json" + + # ── KPI Alerts ───────────────────────────────────────────────────────── + alert_ids = fields.One2many( + comodel_name="spreadsheet.alert", + inverse_name="spreadsheet_id", + string="Alerts", + ) + alert_count = fields.Integer(compute="_compute_alert_count", string="KPI Alerts") + + @api.depends("alert_ids.active") + def _compute_alert_count(self): + self._compute_related_count("spreadsheet.alert", "alert_count") + + def action_open_alerts(self): + self.ensure_one() + return { + "type": "ir.actions.act_window", + "name": _("KPI Alerts"), + "res_model": "spreadsheet.alert", + "view_mode": "list,form", + "domain": [("spreadsheet_id", "=", self.id)], + "context": {"default_spreadsheet_id": self.id}, + } def create_document_from_attachment(self, attachment_ids): attachments = self.env["ir.attachment"].browse(attachment_ids) diff --git a/spreadsheet_oca/readme/DEVELOP.md b/spreadsheet_oca/readme/DEVELOP.md index 0afb4a08..4c82d718 100644 --- a/spreadsheet_oca/readme/DEVELOP.md +++ b/spreadsheet_oca/readme/DEVELOP.md @@ -1,3 +1,30 @@ If you want to develop custom business functions, you can add others, based on the file + +## Server-side pivot computation + +`models/pivot_data.py` reproduces the strategy of the JavaScript +`PivotModel` (`web/static/src/views/pivot/pivot_model.js`) in Python, so +pivot data can be produced without a browser — for scheduled refreshes, +emails or exports. + +For row groupbys `R` and column groupbys `C` it queries every *prefix* +pair of `R` and `C` (the "divisors"), which is what produces the subtotal +rows a pivot needs. That means **(len(R)+1) x (len(C)+1) queries** per +pivot, matching what the JS does. + +The useful entry points are: + +- `get_pivot_data(env, model, domain, context, rows, cols, measures)` — + one pivot. Groups come back with `rowValues`/`colValues` (ids, for + grouping) and `rowLabels`/`colLabels` (display names, for rendering). + If a subtotal query fails the result is still returned, but flagged + with `partial` and `failedGroupBys`; an `AccessError` is re-raised + rather than degraded into an empty group. +- `collect_pivot_summaries(env, spreadsheet_raw, domain_transform=None)` — + every Odoo pivot in a workbook. +- `render_pivot_table_html(summary, max_rows=10)` — one summary as HTML. + +`models/cell_ref.py` holds the A1-notation helpers used to read and write +individual cells of the `spreadsheet_raw` JSON. diff --git a/spreadsheet_oca/readme/USAGE.md b/spreadsheet_oca/readme/USAGE.md index 935707dc..2c90a753 100644 --- a/spreadsheet_oca/readme/USAGE.md +++ b/spreadsheet_oca/readme/USAGE.md @@ -37,3 +37,26 @@ > - `ODOO.ACCOUNT.GROUP(type)`: Returns the account ids of a given > group where type should be a value of the `account_type` field of > `account.account` model. (`income`, `asset_receivable`, etc.) + +## **Get notified when a KPI crosses a threshold** + +- Go to 'Spreadsheet \> Configuration \> KPI Alerts' and click **New**. +- Pick the spreadsheet, the **Cell Reference** to watch (e.g. `C3`) and, + if the workbook has several sheets, the **Sheet**. +- Choose a comparison and a **Threshold** — for example `<` and `0.15`. +- **Run As** decides whose permissions the evaluation uses; it defaults to you. +- **Trigger Mode** controls repetition: + - *Edge* notifies once, when the condition first becomes true. Use this for + "tell me when we fall behind". + - *Level* notifies on every cycle while the condition holds. Use this for + "keep reminding me until it is fixed". +- Add partners under **Notify Partners** to have them emailed as well. + +A single scheduled action wakes hourly and evaluates every active alert, so +adding alerts does not add scheduled actions. Use **Evaluate Now** to test an +alert immediately, and **Reset State** to let an *edge* alert fire again without +waiting for the value to recover first. + +The watched cell must hold a number. A cell that is missing, on a sheet that +does not exist, or holding text is logged as a warning and the alert never +fires. diff --git a/spreadsheet_oca/security/ir.model.access.csv b/spreadsheet_oca/security/ir.model.access.csv index 1898b166..cdf451cd 100644 --- a/spreadsheet_oca/security/ir.model.access.csv +++ b/spreadsheet_oca/security/ir.model.access.csv @@ -6,3 +6,5 @@ access_spreadsheet_import_mode,access_spreadsheet_oca_revision,model_spreadsheet access_spreadsheet_select_row_number,access_spreadsheet_select_row_number,model_spreadsheet_select_row_number,base.group_user,1,1,1,1 access_spreadsheet_spreadsheet_tag,access_spreadsheet_spreadsheet_tag,model_spreadsheet_spreadsheet_tag,spreadsheet_oca.group_user,1,0,0,0 access_spreadsheet_spreadsheet_manager_tag,access_spreadsheet_spreadsheet_manager_tag,model_spreadsheet_spreadsheet_tag,spreadsheet_oca.group_manager,1,1,1,1 +access_spreadsheet_alert_user,access_spreadsheet_alert_user,model_spreadsheet_alert,base.group_user,1,0,0,0 +access_spreadsheet_alert_manager,access_spreadsheet_alert_manager,model_spreadsheet_alert,spreadsheet_oca.group_manager,1,1,1,1 diff --git a/spreadsheet_oca/security/security.xml b/spreadsheet_oca/security/security.xml index aa94100a..9623fa1a 100644 --- a/spreadsheet_oca/security/security.xml +++ b/spreadsheet_oca/security/security.xml @@ -62,4 +62,27 @@ [('group_ids','in', user.groups_id.ids)] + + + + + Alert: follow spreadsheet access + + + [ + '|', '|', '|', + ('spreadsheet_id.owner_id', '=', user.id), + ('spreadsheet_id.contributor_ids', '=', user.id), + ('spreadsheet_id.contributor_group_ids', 'in', user.groups_id.ids), + ('spreadsheet_id.reader_ids', '=', user.id), + ] + + + Alert: manager full access + + + [(1, '=', 1)] + diff --git a/spreadsheet_oca/static/description/index.html b/spreadsheet_oca/static/description/index.html index 739577db..46936a35 100644 --- a/spreadsheet_oca/static/description/index.html +++ b/spreadsheet_oca/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +Spreadsheet Oca -
+
+

Spreadsheet Oca

- - -Odoo Community Association - -
-

Spreadsheet Oca

-

Beta License: AGPL-3 OCA/spreadsheet Translate me on Weblate Try me on Runboat

+

Beta License: AGPL-3 OCA/spreadsheet Translate me on Weblate Try me on Runboat

This module adds a functionality for adding and editing Spreadsheets using Odoo CE.

It is an alternative to the proprietary module spreadsheet_edition @@ -397,9 +392,9 @@

Spreadsheet Oca

-

Usage

+

Usage

-

Create a new spreadsheet

+

Create a new spreadsheet

-

Development

+

Development

If you want to develop custom business functions, you can add others, based on the file https://github.com/odoo/odoo/blob/16.0/addons/spreadsheet_account/static/src/accounting_functions.js

-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -461,15 +456,15 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • CreuBlanca
-

Contributors

+

Contributors

-

Maintainers

+

Maintainers

This module is maintained by the OCA.

Odoo Community Association @@ -500,6 +495,5 @@

Maintainers

-
diff --git a/spreadsheet_oca/tests/__init__.py b/spreadsheet_oca/tests/__init__.py new file mode 100644 index 00000000..398088cb --- /dev/null +++ b/spreadsheet_oca/tests/__init__.py @@ -0,0 +1,3 @@ +from . import test_spreadsheet +from . import test_pivot_data +from . import test_alert diff --git a/spreadsheet_oca/tests/test_alert.py b/spreadsheet_oca/tests/test_alert.py new file mode 100644 index 00000000..5bb28a7a --- /dev/null +++ b/spreadsheet_oca/tests/test_alert.py @@ -0,0 +1,323 @@ +# Copyright 2025 Ledo Enterprises LLC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +""" +Tests for spreadsheet.alert (threshold alerts / KPI watches). +""" + +from odoo.exceptions import ValidationError +from odoo.tests import TransactionCase + +from ..models.cell_ref import parse_cell_ref as _parse_cell_ref + + +class TestParseCellRef(TransactionCase): + """Unit tests for the _parse_cell_ref helper (pure logic, no DB).""" + + def test_simple(self): + self.assertEqual(_parse_cell_ref("A1"), (0, 0)) + self.assertEqual(_parse_cell_ref("B3"), (1, 2)) + self.assertEqual(_parse_cell_ref("Z1"), (25, 0)) + + def test_multi_letter(self): + col, row = _parse_cell_ref("AA1") + self.assertEqual(col, 26) + self.assertEqual(row, 0) + + def test_case_insensitive(self): + self.assertEqual(_parse_cell_ref("b3"), _parse_cell_ref("B3")) + + def test_invalid(self): + self.assertEqual(_parse_cell_ref(""), (None, None)) + self.assertEqual(_parse_cell_ref("A0"), (None, None)) # row must be ≥ 1 + self.assertEqual(_parse_cell_ref("12"), (None, None)) + + +class TestSpreadsheetAlert(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.spreadsheet = cls.env["spreadsheet.spreadsheet"].create( + {"name": "Alert Test Spreadsheet"} + ) + cls.partner = cls.env["res.partner"].create({"name": "Alert Subscriber"}) + + def _make_alert(self, **kwargs): + defaults = { + "name": "Revenue Alert", + "spreadsheet_id": self.spreadsheet.id, + "cell_ref": "B3", + "operator": ">", + "threshold": 1000.0, + "trigger_mode": "edge", + } + defaults.update(kwargs) + return self.env["spreadsheet.alert"].create(defaults) + + def _set_cell_value(self, value, col="B", row=3): + """Write a cell value into spreadsheet_raw for testing.""" + cell_addr = f"{col.upper()}{row}" + raw = { + "version": 1, + "sheets": [ + { + "id": "sheet1", + "name": "Sheet1", + "cells": { + cell_addr: {"content": str(value)}, + }, + } + ], + } + self.spreadsheet.write({"spreadsheet_raw": raw}) + + # ── Validation ──────────────────────────────────────────────────────────── + + def test_invalid_cell_ref_raises(self): + with self.assertRaises(ValidationError): + self._make_alert(cell_ref="A0") + + def test_valid_cell_ref_accepted(self): + alert = self._make_alert(cell_ref="C12") + self.assertEqual(alert.cell_ref, "C12") + + # ── _check_condition ────────────────────────────────────────────────────── + + def test_operators(self): + alert = self._make_alert(operator=">", threshold=100.0) + self.assertTrue(alert._check_condition(101.0)) + self.assertFalse(alert._check_condition(100.0)) + + alert.operator = ">=" + self.assertTrue(alert._check_condition(100.0)) + + alert.operator = "<" + self.assertTrue(alert._check_condition(99.0)) + self.assertFalse(alert._check_condition(100.0)) + + alert.operator = "==" + self.assertTrue(alert._check_condition(100.0)) + self.assertFalse(alert._check_condition(99.9)) + + alert.operator = "!=" + self.assertTrue(alert._check_condition(99.0)) + self.assertFalse(alert._check_condition(100.0)) + + # ── _read_cell_value ────────────────────────────────────────────────────── + + def test_read_cell_value_numeric(self): + self._set_cell_value(42.5) + alert = self._make_alert(cell_ref="B3") + self.assertAlmostEqual(alert._read_cell_value(), 42.5) + + def test_read_cell_value_empty_sheet(self): + self.spreadsheet.write({"spreadsheet_raw": {}}) + alert = self._make_alert(cell_ref="B3") + self.assertIsNone(alert._read_cell_value()) + + def test_read_cell_value_missing_cell(self): + self._set_cell_value(10, col="A", row=1) + alert = self._make_alert(cell_ref="Z99") + self.assertIsNone(alert._read_cell_value()) + + def test_read_cell_value_by_sheet_name(self): + raw = { + "version": 1, + "sheets": [ + { + "id": "s1", + "name": "Summary", + "cells": {"B3": {"content": "77"}}, + }, + {"id": "s2", "name": "Data", "cells": {"B3": {"content": "99"}}}, + ], + } + self.spreadsheet.write({"spreadsheet_raw": raw}) + alert = self._make_alert(cell_ref="B3", sheet_name="Data") + self.assertAlmostEqual(alert._read_cell_value(), 99.0) + + # ── Edge mode ───────────────────────────────────────────────────────────── + + def test_edge_mode_fires_once_on_crossing(self): + self._set_cell_value(1500) + alert = self._make_alert(operator=">", threshold=1000.0, trigger_mode="edge") + self.assertFalse(alert.last_state) + + msg_count_before = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + alert._evaluate() + msg_count_after = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + self.assertGreater(msg_count_after, msg_count_before) + self.assertTrue(alert.last_state) + + # Second evaluation: already in alert state → no new notification + msg_count_before2 = msg_count_after + alert._evaluate() + msg_count_after2 = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + self.assertEqual(msg_count_before2, msg_count_after2) + + def test_edge_mode_no_fire_below_threshold(self): + self._set_cell_value(500) + alert = self._make_alert(operator=">", threshold=1000.0, trigger_mode="edge") + msg_count_before = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + alert._evaluate() + msg_count_after = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + self.assertEqual(msg_count_before, msg_count_after) + self.assertFalse(alert.last_state) + + def test_edge_reset_allows_re_trigger(self): + self._set_cell_value(1500) + alert = self._make_alert(operator=">", threshold=1000.0, trigger_mode="edge") + alert._evaluate() # fires + self.assertTrue(alert.last_state) + alert.action_reset_state() + self.assertFalse(alert.last_state) + + msg_before = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + alert._evaluate() # fires again after reset + msg_after = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + self.assertGreater(msg_after, msg_before) + + # ── Level mode ──────────────────────────────────────────────────────────── + + def test_level_mode_fires_every_cycle(self): + self._set_cell_value(1500) + alert = self._make_alert(operator=">", threshold=1000.0, trigger_mode="level") + + for i in range(3): + msg_before = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + alert._evaluate() + msg_after = self.env["mail.message"].search_count( + [ + ("res_id", "=", self.spreadsheet.id), + ("model", "=", "spreadsheet.spreadsheet"), + ] + ) + self.assertGreater( + msg_after, msg_before, f"Expected notification on cycle {i + 1}" + ) + + # ── Cron dispatcher ─────────────────────────────────────────────────────── + + def test_cron_evaluates_all_active(self): + self._set_cell_value(2000) + alert1 = self._make_alert(operator=">", threshold=1000.0) + alert2 = self._make_alert( + name="Low Stock", operator="<", threshold=5.0, cell_ref="B3" + ) + + # Both should update last_checked after cron run + self.env["spreadsheet.alert"]._cron_evaluate_all() + self.assertTrue(alert1.last_checked) + self.assertTrue(alert2.last_checked) + + def test_cron_skips_inactive(self): + alert = self._make_alert(active=False, operator=">", threshold=1000.0) + self.env["spreadsheet.alert"]._cron_evaluate_all() + self.assertFalse(alert.last_checked) + + # ── Smart button ────────────────────────────────────────────────────────── + + def test_smart_button_count(self): + self.assertEqual(self.spreadsheet.alert_count, 0) + self._make_alert() + self._make_alert(name="Alert 2", cell_ref="C5") + self.spreadsheet.invalidate_recordset() + self.assertEqual(self.spreadsheet.alert_count, 2) + + # ── Run As ──────────────────────────────────────────────────────────────── + + def test_defaults_to_the_creating_user(self): + self.assertEqual(self._make_alert().user_id, self.env.user) + + def test_cron_evaluates_each_alert_as_its_own_user(self): + """Evaluation must use the alert's user, not the cron's. + + A shared cron runs as whoever installed the module — a privileged + account — so without this binding every alert would read and report + with those rights instead of its owner's. + """ + limited = self.env["res.users"].create( + { + "name": "Limited Alert User", + "login": "limited_alert_user", + "groups_id": [(6, 0, [self.env.ref("base.group_user").id])], + } + ) + self._set_cell_value(2000) # a real value: this must evaluate cleanly + alert = self._make_alert(operator=">", threshold=1000.0) + alert.user_id = limited + + # (alert_id, executing_user_id) — other alerts run as their own users. + seen = [] + original = type(alert)._evaluate + + def _spy(self_): + seen.append((self_.id, self_.env.user.id)) + return original(self_) + + self.patch(type(alert), "_evaluate", _spy) + self.env["spreadsheet.alert"]._cron_evaluate_all() + + runs = [uid for alert_id, uid in seen if alert_id == alert.id] + self.assertEqual( + runs, + [limited.id], + "the alert must be evaluated as its own user, not the cron's", + ) + # The cron swallows per-alert exceptions, so "it was called" is not + # enough — the evaluation has to have actually completed. A plain + # internal user has no write access to spreadsheet.alert, so this + # fails unless the state write is done as housekeeping. + self.assertTrue( + alert.last_checked, + "evaluation ran as the alert's user but could not record its state", + ) + + def test_unreadable_cell_is_reported_not_silent(self): + """A cell that cannot be read must warn — it can never fire.""" + alert = self._make_alert(cell_ref="Z99") + with self.assertLogs( + "odoo.addons.spreadsheet_oca.models.spreadsheet_alert", level="WARNING" + ) as logs: + alert._evaluate() + self.assertTrue(any("cannot fire" in m for m in logs.output)) + self.assertTrue(alert.last_checked) diff --git a/spreadsheet_oca/tests/test_pivot_data.py b/spreadsheet_oca/tests/test_pivot_data.py new file mode 100644 index 00000000..b1341934 --- /dev/null +++ b/spreadsheet_oca/tests/test_pivot_data.py @@ -0,0 +1,198 @@ +# Copyright 2026 Ledo Enterprises LLC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +""" +Tests for server-side pivot data computation. + +These tests verify that get_pivot_data() produces the same grouping +structure that the Odoo JS PivotModel would produce via read_group. + +Run against odoo_test (which has sale, account installed): + docker exec -i odoo-prod odoo test -d odoo_test \ + --test-tags spreadsheet_oca.TestPivotData --stop-after-init +""" + +from odoo.tests import TransactionCase + +from ..models.pivot_data import _dimension_to_groupby, _sections, get_pivot_data + + +class TestPivotDataHelpers(TransactionCase): + """Unit tests for the pure-Python helpers (no DB needed).""" + + def test_sections_empty(self): + self.assertEqual(_sections([]), [[]]) + + def test_sections_one(self): + self.assertEqual(_sections(["a"]), [[], ["a"]]) + + def test_sections_two(self): + self.assertEqual(_sections(["a", "b"]), [[], ["a"], ["a", "b"]]) + + def test_dimension_no_granularity(self): + self.assertEqual( + _dimension_to_groupby({"fieldName": "partner_id"}), "partner_id" + ) + + def test_dimension_with_granularity(self): + self.assertEqual( + _dimension_to_groupby({"fieldName": "date_order", "granularity": "month"}), + "date_order:month", + ) + + +class TestPivotData(TransactionCase): + """Integration tests using res.partner (always available, no demo needed).""" + + @classmethod + def setUpClass(cls): + super().setUpClass() + # Create a handful of partners in different countries to group by + cls.country_be = cls.env.ref("base.be") + cls.country_us = cls.env.ref("base.us") + cls.partners = cls.env["res.partner"].create( + [ + {"name": "Alpha", "country_id": cls.country_be.id, "is_company": True}, + {"name": "Beta", "country_id": cls.country_be.id, "is_company": True}, + {"name": "Gamma", "country_id": cls.country_us.id, "is_company": True}, + {"name": "Delta", "country_id": cls.country_us.id, "is_company": False}, + ] + ) + cls.domain = [("id", "in", cls.partners.ids)] + + # ── Helpers ───────────────────────────────────────────────────────────── + + def _run(self, row_dims, col_dims, measures): + return get_pivot_data( + self.env, + "res.partner", + self.domain, + {}, + row_dims, + col_dims, + measures, + ) + + def _groups_for(self, result, row_prefix, col_prefix): + """Return groups matching the given row/col groupby prefix.""" + return [ + g + for g in result["groups"] + if g["rowGroupBy"] == row_prefix and g["colGroupBy"] == col_prefix + ] + + # ── Grand-total (no groupby) ──────────────────────────────────────────── + + def test_grand_total_count(self): + """With no dims, one group with count = number of partners.""" + result = self._run([], [], [{"fieldName": "__count"}]) + groups = self._groups_for(result, [], []) + self.assertEqual(len(groups), 1) + self.assertEqual(groups[0]["count"], 4) + + # ── Single row groupby ────────────────────────────────────────────────── + + def test_row_groupby_country(self): + """Row groupby country_id → one group per country + grand total.""" + row_dims = [{"fieldName": "country_id"}] + result = self._run(row_dims, [], [{"fieldName": "__count"}]) + + # Grand total (rowGroupBy=[], colGroupBy=[]) + totals = self._groups_for(result, [], []) + self.assertEqual(len(totals), 1) + self.assertEqual(totals[0]["count"], 4) + + # Per-country groups (rowGroupBy=["country_id"], colGroupBy=[]) + country_groups = self._groups_for(result, ["country_id"], []) + self.assertEqual(len(country_groups), 2) + counts_by_country = {g["rowValues"][0]: g["count"] for g in country_groups} + self.assertEqual(counts_by_country[self.country_be.id], 2) + self.assertEqual(counts_by_country[self.country_us.id], 2) + + def test_many2one_groups_carry_display_labels(self): + """A many2one group must expose its name, not just its database id. + + rowValues stays an id because grouping needs it, but anything that + renders a group for a human reads rowLabels — without it a Chatter + summary shows "13" where it should show "Belgium". + """ + result = self._run( + [{"fieldName": "country_id"}], [], [{"fieldName": "__count"}] + ) + country_groups = self._groups_for(result, ["country_id"], []) + + labels = {g["rowLabels"][0] for g in country_groups} + self.assertEqual( + labels, {self.country_be.display_name, self.country_us.display_name} + ) + for group in country_groups: + self.assertIsInstance(group["rowValues"][0], int) + + # ── Row + col groupby ─────────────────────────────────────────────────── + + def test_row_and_col_groupby(self): + """Row=country_id, Col=is_company → 2×2 cell values.""" + row_dims = [{"fieldName": "country_id"}] + col_dims = [{"fieldName": "is_company"}] + result = self._run(row_dims, col_dims, [{"fieldName": "__count"}]) + + # Divisors: ([], []) ([], [is_company]) + # ([country_id], []) ([country_id], [is_company]) + # → 4 divisors, each producing N read_group rows + divisor_keys = { + (tuple(g["rowGroupBy"]), tuple(g["colGroupBy"])) for g in result["groups"] + } + self.assertIn(((), ()), divisor_keys) + self.assertIn(((), ("is_company",)), divisor_keys) + self.assertIn((("country_id",), ()), divisor_keys) + self.assertIn((("country_id",), ("is_company",)), divisor_keys) + + # BE / is_company=True → Alpha + Beta = 2 + cell_groups = self._groups_for(result, ["country_id"], ["is_company"]) + be_company = [ + g + for g in cell_groups + if g["rowValues"] == [self.country_be.id] and g["colValues"] == [True] + ] + self.assertEqual(len(be_company), 1) + self.assertEqual(be_company[0]["count"], 2) + + # US / is_company=False → Delta = 1 + us_individual = [ + g + for g in cell_groups + if g["rowValues"] == [self.country_us.id] and g["colValues"] == [False] + ] + self.assertEqual(len(us_individual), 1) + self.assertEqual(us_individual[0]["count"], 1) + + # ── Return structure ──────────────────────────────────────────────────── + + def test_return_fields_metadata(self): + """Result includes fields metadata for all used fields.""" + row_dims = [{"fieldName": "country_id"}] + result = self._run(row_dims, [], [{"fieldName": "__count"}]) + self.assertIn("country_id", result["fields"]) + self.assertEqual(result["fields"]["country_id"]["type"], "many2one") + + def test_return_dimensions_and_specs(self): + """Result echoes back row/col dims and measure specs.""" + row_dims = [{"fieldName": "country_id"}] + measures = [{"fieldName": "__count"}] + result = self._run(row_dims, [], measures) + self.assertEqual(result["rowDimensions"], row_dims) + self.assertEqual(result["colDimensions"], []) + self.assertEqual(result["measureSpecs"], ["__count"]) + + # ── Domain filtering ──────────────────────────────────────────────────── + + def test_domain_filters_correctly(self): + """Domain restricts records — only BE partners.""" + be_domain = [ + ("id", "in", self.partners.ids), + ("country_id", "=", self.country_be.id), + ] + result = get_pivot_data( + self.env, "res.partner", be_domain, {}, [], [], [{"fieldName": "__count"}] + ) + totals = self._groups_for(result, [], []) + self.assertEqual(totals[0]["count"], 2) diff --git a/spreadsheet_oca/tests/test_spreadsheet.py b/spreadsheet_oca/tests/test_spreadsheet.py new file mode 100644 index 00000000..f27e7196 --- /dev/null +++ b/spreadsheet_oca/tests/test_spreadsheet.py @@ -0,0 +1,28 @@ +# Copyright 2026 Ledo Enterprises LLC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestSpreadsheetFilename(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.Spreadsheet = cls.env["spreadsheet.spreadsheet"] + + def test_filename_single_record(self): + spreadsheet = self.Spreadsheet.create({"name": "Budget"}) + self.assertEqual(spreadsheet.filename, "Budget.json") + + def test_filename_is_computed_per_record(self): + """Each record must get its own name, not the first record's. + + _compute_filename used to read ``self.name`` while iterating over + ``self``, which raises a singleton error on a multi-record set. + """ + alpha = self.Spreadsheet.create({"name": "Alpha"}) + beta = self.Spreadsheet.create({"name": "Beta"}) + + both = alpha | beta + + self.assertEqual(both.mapped("filename"), ["Alpha.json", "Beta.json"]) diff --git a/spreadsheet_oca/views/spreadsheet_alert_views.xml b/spreadsheet_oca/views/spreadsheet_alert_views.xml new file mode 100644 index 00000000..326b4534 --- /dev/null +++ b/spreadsheet_oca/views/spreadsheet_alert_views.xml @@ -0,0 +1,267 @@ + + + + + + spreadsheet.alert.search + spreadsheet.alert + + + + + + + + + + + + + + + + + + + spreadsheet.alert.list + spreadsheet.alert + + + + + + + + + + + + + + + + +