feat(metabase): auto-commit con 17 cambios
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
"""Copia parameter_mappings de un dashcard a otro dentro del mismo dashboard."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from .client import MetabaseClient
|
||||
from .dashboards import metabase_get_dashboard
|
||||
|
||||
|
||||
def metabase_copy_dashcard_mappings(
|
||||
client: MetabaseClient,
|
||||
*,
|
||||
dashboard_id: int,
|
||||
source_card_id: int,
|
||||
dest_card_id: int,
|
||||
) -> list[dict]:
|
||||
"""Copia los parameter_mappings del primer dashcard fuente al card destino.
|
||||
|
||||
Obtiene el dashboard, localiza el primer dashcard cuyo card_id coincide con
|
||||
source_card_id y devuelve una lista nueva de parameter_mappings con card_id
|
||||
sustituido por dest_card_id. No muta el dashboard ni los mappings originales.
|
||||
|
||||
Args:
|
||||
client: Cliente autenticado con sesion activa.
|
||||
dashboard_id: ID del dashboard que contiene ambas cards.
|
||||
source_card_id: card_id del dashcard del que se copian los mappings.
|
||||
dest_card_id: card_id que se asigna en los mappings copiados.
|
||||
|
||||
Returns:
|
||||
Lista de dicts con los parameter_mappings adaptados al card destino.
|
||||
Cada elemento tiene: parameter_id, card_id (dest_card_id), target.
|
||||
Retorna lista vacia si el dashcard fuente no tiene mappings.
|
||||
|
||||
Raises:
|
||||
ValueError: Si source_card_id no aparece en ninguna dashcard del dashboard.
|
||||
|
||||
Example:
|
||||
>>> mappings = metabase_copy_dashcard_mappings(
|
||||
... client,
|
||||
... dashboard_id=10,
|
||||
... source_card_id=42,
|
||||
... dest_card_id=99,
|
||||
... )
|
||||
>>> # Usar los mappings al añadir el nuevo dashcard al dashboard
|
||||
>>> new_dashcard = {
|
||||
... "id": -1,
|
||||
... "card_id": 99,
|
||||
... "col": 0, "row": 20, "size_x": 6, "size_y": 4,
|
||||
... "parameter_mappings": mappings,
|
||||
... "visualization_settings": {},
|
||||
... }
|
||||
"""
|
||||
dashboard = metabase_get_dashboard(client, dashboard_id)
|
||||
dashcards: list[dict] = dashboard.get("dashcards") or []
|
||||
|
||||
source_dc: dict | None = None
|
||||
for dc in dashcards:
|
||||
if dc.get("card_id") == source_card_id:
|
||||
source_dc = dc
|
||||
break
|
||||
|
||||
if source_dc is None:
|
||||
raise ValueError(
|
||||
f"card {source_card_id} not in dashboard {dashboard_id}"
|
||||
)
|
||||
|
||||
source_mappings: list[dict] = source_dc.get("parameter_mappings") or []
|
||||
|
||||
return [
|
||||
{
|
||||
"parameter_id": m["parameter_id"],
|
||||
"card_id": dest_card_id,
|
||||
"target": m["target"],
|
||||
}
|
||||
for m in source_mappings
|
||||
]
|
||||
Reference in New Issue
Block a user