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..9e3c0a43 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_subscription_views.xml", + "data/mail_templates.xml", "data/spreadsheet_spreadsheet_import_mode.xml", + "data/spreadsheet_subscription_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..ff843bb2 --- /dev/null +++ b/spreadsheet_oca/data/mail_templates.xml @@ -0,0 +1,66 @@ + + + + + + spreadsheet.subscription.digest + qweb + + +
+

+ +

+
+ Generated: +
+
+ +

+ No pivot data sources are available in this spreadsheet. +

+
+ + +
+ +
+
+
+
+ + +
+ You are receiving this because you subscribed + to digest emails for this spreadsheet. +
+
+
+
+
+
diff --git a/spreadsheet_oca/data/spreadsheet_subscription_cron.xml b/spreadsheet_oca/data/spreadsheet_subscription_cron.xml new file mode 100644 index 00000000..01e471a2 --- /dev/null +++ b/spreadsheet_oca/data/spreadsheet_subscription_cron.xml @@ -0,0 +1,14 @@ + + + + + Spreadsheet Dashboard Digest + + code + model._cron_send_digests() + 1 + days + True + + 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..e632e7ab 100644 --- a/spreadsheet_oca/demo/spreadsheet_spreadsheet.xml +++ b/spreadsheet_oca/demo/spreadsheet_spreadsheet.xml @@ -1,11 +1,101 @@ + + + + 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 + + + + Partner Pivot Dashboard + + + + + + + + + + weekly + + diff --git a/spreadsheet_oca/models/__init__.py b/spreadsheet_oca/models/__init__.py index 3e07c12a..689c3e3d 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_subscription 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_spreadsheet.py b/spreadsheet_oca/models/spreadsheet_spreadsheet.py index 55a9ae9f..d089f25a 100644 --- a/spreadsheet_oca/models/spreadsheet_spreadsheet.py +++ b/spreadsheet_oca/models/spreadsheet_spreadsheet.py @@ -56,10 +56,55 @@ 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" + + # ── Subscriptions ───────────────────────────────────────────────────── + subscription_ids = fields.One2many( + comodel_name="spreadsheet.subscription", + inverse_name="spreadsheet_id", + string="Subscriptions", + ) + subscriber_count = fields.Integer( + compute="_compute_subscriber_count", string="Subscribers" + ) + + @api.depends("subscription_ids.active") + def _compute_subscriber_count(self): + self._compute_related_count("spreadsheet.subscription", "subscriber_count") + + def action_open_subscriptions(self): + self.ensure_one() + return { + "type": "ir.actions.act_window", + "name": _("Subscribers"), + "res_model": "spreadsheet.subscription", + "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/models/spreadsheet_subscription.py b/spreadsheet_oca/models/spreadsheet_subscription.py new file mode 100644 index 00000000..ae5fe9e3 --- /dev/null +++ b/spreadsheet_oca/models/spreadsheet_subscription.py @@ -0,0 +1,215 @@ +# Copyright 2025 Ledo Enterprises LLC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +""" +Dashboard subscriptions. + +Allows partners to subscribe to a periodic email digest of a spreadsheet. +A shared daily cron evaluates all active subscriptions and sends those that +are due (based on frequency and last_sent timestamp). + +Each digest email optionally includes a compact pivot data summary rendered +with inline CSS, suitable for email clients. +""" + +import logging +from datetime import timedelta + +from markupsafe import Markup + +from odoo import _, api, fields, models + +from .pivot_data import collect_pivot_summaries, render_pivot_table_html + +_logger = logging.getLogger(__name__) + +_FREQUENCY_SELECTION = [ + ("daily", "Daily"), + ("weekly", "Weekly"), + ("monthly", "Monthly"), +] + +_FREQUENCY_DELTA = { + "daily": timedelta(days=1), + "weekly": timedelta(weeks=1), + "monthly": timedelta(days=30), +} + + +class SpreadsheetSubscription(models.Model): + _name = "spreadsheet.subscription" + _description = "Spreadsheet Dashboard Subscription" + _inherit = ["mail.thread"] + _order = "spreadsheet_id, partner_id" + + name = fields.Char( + compute="_compute_name", + store=True, + readonly=False, + ) + spreadsheet_id = fields.Many2one( + "spreadsheet.spreadsheet", + required=True, + ondelete="cascade", + index=True, + ) + partner_id = fields.Many2one( + "res.partner", + string="Subscriber", + required=True, + ondelete="restrict", + 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 digest is computed with this user's permissions, so a " + "subscriber never receives figures this user could not read.", + ) + frequency = fields.Selection( + _FREQUENCY_SELECTION, + default="weekly", + required=True, + tracking=True, + ) + last_sent = fields.Datetime(readonly=True, copy=False) + include_pivot_data = fields.Boolean( + default=True, + help="Include a live pivot data summary in the email.", + ) + + _sql_constraints = [ + ( + "unique_spreadsheet_partner", + "UNIQUE(spreadsheet_id, partner_id)", + "A partner can only have one subscription per spreadsheet.", + ), + ] + + # ── Default name computation ─────────────────────────────────────────────── + + @api.depends("spreadsheet_id", "partner_id") + def _compute_name(self): + for rec in self: + spreadsheet_name = rec.spreadsheet_id.name or _("Unnamed Spreadsheet") + partner_name = rec.partner_id.name or _("Unknown Partner") + rec.name = f"{spreadsheet_name} — {partner_name}" + + # ── Shared cron ─────────────────────────────────────────────────────────── + + @api.model + def _cron_send_digests(self): + """Called by the shared ir.cron: send digests that are due.""" + active_subs = self.search([("active", "=", True)]) + now = fields.Datetime.now() + for sub in active_subs: + try: + sub.with_user(sub.user_id)._send_digest_if_due(now) + except Exception: + _logger.exception( + "Failed to send digest for subscription %s (%s)", + sub.id, + sub.name, + ) + + def _send_digest_if_due(self, now=None): + """Send this subscription's digest if it is due, otherwise skip.""" + self.ensure_one() + if now is None: + now = fields.Datetime.now() + delta = _FREQUENCY_DELTA.get(self.frequency, timedelta(weeks=1)) + if self.last_sent and (now - self.last_sent) < delta: + return # not due yet + self._send_digest() + + # ── Digest sending ──────────────────────────────────────────────────────── + + def _send_digest(self): + """Generate and send a digest email for this subscription.""" + self.ensure_one() + spreadsheet = self.spreadsheet_id.sudo() + summaries = [] + + if self.include_pivot_data: + raw = spreadsheet.spreadsheet_raw or {} + summaries, _failed = collect_pivot_summaries(self.env, raw) + + body_html = self._render_digest_html(spreadsheet, summaries) + subject = _("Spreadsheet Digest: %(name)s", name=spreadsheet.name) + + partner = self.partner_id + email_to = partner.email + if not email_to: + _logger.warning( + "Subscription %s: partner %s has no email — skipping", + self.id, + partner.name, + ) + return + + # An explicit sender: without one the mail only goes out if the server + # happens to have mail.catchall.domain / mail.default.from configured, + # and mail.mail logs the failure rather than raising it. + email_from = ( + self.user_id.email_formatted + or self.env.company.email_formatted + or self.env.company.email + ) + if not email_from: + _logger.warning( + "Subscription %s: no sender address available (neither %s nor " + "the company has an email) — digest not sent", + self.id, + self.user_id.name, + ) + return + + self.env["mail.mail"].sudo().create( + { + "subject": subject, + "body_html": body_html, + "email_to": email_to, + "email_from": email_from, + } + ).send() + + self.sudo().write({"last_sent": fields.Datetime.now()}) + + # ── HTML rendering ──────────────────────────────────────────────────────── + + def _render_digest_html(self, spreadsheet, summaries): + """Return a styled HTML email body for the digest. + + Uses the QWeb template ``spreadsheet_subscription_digest_template`` + which can be customised via Settings > Technical > Views. + + Args: + spreadsheet: browse record of spreadsheet.spreadsheet (already sudo'd). + summaries: list of {"name", "model", "result"} dicts from _get_pivot_data. + + Returns: + str — complete HTML body suitable for sending via mail.mail. + """ + now_str = fields.Datetime.now().strftime("%Y-%m-%d %H:%M UTC") + base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url") + pivot_html_list = [Markup(render_pivot_table_html(s)) for s in summaries] + return self.env["ir.qweb"]._render( + "spreadsheet_oca.spreadsheet_subscription_digest_template", + { + "spreadsheet": spreadsheet, + "now_str": now_str, + "summaries": summaries, + "pivot_html_list": pivot_html_list, + "base_url": base_url, + }, + ) + + # ── Manual send ─────────────────────────────────────────────────────────── + + def action_send_now(self): + """Manually send the digest immediately, bypassing the due check.""" + self.ensure_one() + self._send_digest() 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..8921d46c 100644 --- a/spreadsheet_oca/readme/USAGE.md +++ b/spreadsheet_oca/readme/USAGE.md @@ -37,3 +37,19 @@ > - `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.) + +## **Email a spreadsheet digest on a schedule** + +Go to 'Spreadsheet \> Configuration \> Subscriptions' and add one per person +who should receive a spreadsheet by email. + +- Pick the **Subscriber** and how often they should hear from you. +- **Include Pivot Data** attaches a live summary of every Odoo pivot in the + workbook, recomputed at send time rather than reusing stale saved values. +- **Run As** decides whose permissions the figures are computed with; it + defaults to you. A subscriber therefore never receives numbers that this + user could not read themselves. + +A single scheduled action wakes daily and sends whichever digests are due, so +adding subscribers does not add scheduled actions. **Send Now** delivers one +immediately without waiting for the schedule. diff --git a/spreadsheet_oca/security/ir.model.access.csv b/spreadsheet_oca/security/ir.model.access.csv index 1898b166..0d01041b 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_subscription_user,access_spreadsheet_subscription_user,model_spreadsheet_subscription,base.group_user,1,0,0,0 +access_spreadsheet_subscription_manager,access_spreadsheet_subscription_manager,model_spreadsheet_subscription,spreadsheet_oca.group_manager,1,1,1,1 diff --git a/spreadsheet_oca/security/security.xml b/spreadsheet_oca/security/security.xml index aa94100a..af0c34b2 100644 --- a/spreadsheet_oca/security/security.xml +++ b/spreadsheet_oca/security/security.xml @@ -62,4 +62,25 @@ [('group_ids','in', user.groups_id.ids)] + + + + + Subscription: 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), + ] + + + Subscription: 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..9ed79a48 --- /dev/null +++ b/spreadsheet_oca/tests/__init__.py @@ -0,0 +1,3 @@ +from . import test_spreadsheet +from . import test_pivot_data +from . import test_subscription 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/tests/test_subscription.py b/spreadsheet_oca/tests/test_subscription.py new file mode 100644 index 00000000..2abe05df --- /dev/null +++ b/spreadsheet_oca/tests/test_subscription.py @@ -0,0 +1,257 @@ +# Copyright 2025 Ledo Enterprises LLC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +""" +Tests for spreadsheet.subscription (dashboard subscriptions). +""" + +from datetime import timedelta + +from psycopg2 import IntegrityError + +from odoo import fields +from odoo.tests import TransactionCase +from odoo.tools import mute_logger + + +class TestSpreadsheetSubscription(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Archive any pre-existing demo subscriptions so they don't + # interfere with cron-based mail-count assertions. + cls.env["spreadsheet.subscription"].search([]).write({"active": False}) + cls.spreadsheet = cls.env["spreadsheet.spreadsheet"].create( + {"name": "Sub Test Spreadsheet"} + ) + cls.partner = cls.env["res.partner"].create( + {"name": "Digest Subscriber", "email": "sub@example.com"} + ) + cls.partner2 = cls.env["res.partner"].create( + {"name": "Second Subscriber", "email": "sub2@example.com"} + ) + + def _make_subscription(self, **kwargs): + defaults = { + "spreadsheet_id": self.spreadsheet.id, + "partner_id": self.partner.id, + "frequency": "weekly", + "include_pivot_data": False, + } + defaults.update(kwargs) + return self.env["spreadsheet.subscription"].create(defaults) + + def _make_raw_with_pivot(self): + """Return a spreadsheet_raw dict containing one ODOO pivot.""" + return { + "version": 1, + "pivots": { + "1": { + "type": "ODOO", + "model": "res.partner", + "name": "Partner Count", + "domain": [], + "context": {}, + "rows": [], + "columns": [], + "measures": [{"fieldName": "__count"}], + } + }, + } + + # ── Creation ────────────────────────────────────────────────────────────── + + def test_create_subscription(self): + sub = self._make_subscription(frequency="daily", include_pivot_data=True) + self.assertEqual(sub.spreadsheet_id, self.spreadsheet) + self.assertEqual(sub.partner_id, self.partner) + self.assertEqual(sub.frequency, "daily") + self.assertTrue(sub.include_pivot_data) + self.assertTrue(sub.active) + self.assertFalse(sub.last_sent) + # Name is auto-computed from spreadsheet + partner + self.assertIn("Sub Test Spreadsheet", sub.name) + self.assertIn("Digest Subscriber", sub.name) + + def test_unique_constraint(self): + self._make_subscription() + with self.assertRaises(IntegrityError), mute_logger("odoo.sql_db"): + # Creating a second subscription for the same spreadsheet + partner + # must raise a DB-level unique constraint violation. + self.env["spreadsheet.subscription"].create( + { + "spreadsheet_id": self.spreadsheet.id, + "partner_id": self.partner.id, + "frequency": "monthly", + } + ) + + # ── _send_digest: mail.mail creation ───────────────────────────────────── + + def test_send_digest_no_pivots(self): + """Digest is sent (last_sent updated) even when there are no pivot sources.""" + sub = self._make_subscription(include_pivot_data=False) + self.assertFalse(sub.last_sent) + sub._send_digest() + self.assertTrue(sub.last_sent) + + def test_send_digest_checks_subject(self): + """Digest email subject contains the spreadsheet name.""" + sub = self._make_subscription(include_pivot_data=False) + # Check rendered HTML directly instead of querying mail.mail + body = sub._render_digest_html(self.spreadsheet.sudo(), []) + self.assertIn(self.spreadsheet.name, body) + + def test_send_digest_with_pivot_data(self): + """Digest with include_pivot_data=True processes pivot data.""" + self.spreadsheet.write({"spreadsheet_raw": self._make_raw_with_pivot()}) + sub = self._make_subscription(include_pivot_data=True) + self.assertFalse(sub.last_sent) + sub._send_digest() + self.assertTrue(sub.last_sent) + + def test_send_digest_include_false(self): + """When include_pivot_data=False, pivot rendering is skipped.""" + self.spreadsheet.write({"spreadsheet_raw": self._make_raw_with_pivot()}) + sub = self._make_subscription(include_pivot_data=False) + # Check rendered HTML directly + body = sub._render_digest_html(self.spreadsheet.sudo(), []) + self.assertNotIn(" + + + + + spreadsheet.subscription.search + spreadsheet.subscription + + + + + + + + + + + + + + + + + + + + + + + + spreadsheet.subscription.list + spreadsheet.subscription + + + + + + + + + + + + +