diff --git a/.gitignore b/.gitignore index 7cbdaf3..5128d7f 100644 --- a/.gitignore +++ b/.gitignore @@ -91,6 +91,9 @@ ipython_config.py # pyenv .python-version +# IDE +.idea/ + # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies diff --git a/README.md b/README.md index 978a06a..5368b6b 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ dashboards = client.dashboards.find() dashboard = client.dashboards.find(dashboard_title="Example")[0] ``` +To delete a dashboard: +```python3 +dashboard.delete() +``` + Update dashboard colors, some properties and save changes to server: ```python3 # Update label_colors mapping @@ -65,6 +70,15 @@ Create the embed configuration for a dashboard: embed = dashboard.create_embed(allowed_domains=[]) ``` +Copy a dashboard: +```python3 +dashboard_copy = dashboard.copy_dashboard(dashboard_payload={ + "css": "", + "dashboard_title": "your-new-dashboard-title", + "duplicate_slices": False, + "json_metadata": "{}", +}) +``` ### Export one ore more dashboard @@ -90,11 +104,23 @@ dashboard.export( This functionality is also available in the same manner for datasets +### CSS Templates + +```python3 +# Get all CSS Templates +css_templates = client.css_templates.find() + +# Get a CSS Template by name +css_template = client.css_templates.find(template_name="Flat")[0] + +# Retrieve the CSS of a CSS Template +css = css_template.css +``` + ### Retrieve a Guest Token You can retrieve a guest token using the `guest_token` method. This method requires the UUID of the resource (e.g., dashboard) and uses the user's first name, last name, and username from the client instance. -#### Example Usage: ```python3 # Retrieve a guest token guest_token = client.guest_token(uuid="your-example-uuid") diff --git a/supersetapiclient/client.py b/supersetapiclient/client.py index 8ce73d6..a478cbb 100644 --- a/supersetapiclient/client.py +++ b/supersetapiclient/client.py @@ -20,6 +20,7 @@ from supersetapiclient.datasets import Datasets from supersetapiclient.exceptions import QueryLimitReached from supersetapiclient.saved_queries import SavedQueries +from supersetapiclient.css_templates import CssTemplates logger = logging.getLogger(__name__) @@ -33,6 +34,7 @@ class SupersetClient: datasets_cls = Datasets databases_cls = Databases saved_queries_cls = SavedQueries + css_templates_cls = CssTemplates http_adapter_cls = None def __init__( @@ -63,6 +65,7 @@ def __init__( self.datasets = self.datasets_cls(self) self.databases = self.databases_cls(self) self.saved_queries = self.saved_queries_cls(self) + self.css_templates = self.css_templates_cls(self) @cached_property def _token(self): diff --git a/supersetapiclient/css_templates.py b/supersetapiclient/css_templates.py new file mode 100644 index 0000000..6764461 --- /dev/null +++ b/supersetapiclient/css_templates.py @@ -0,0 +1,17 @@ +"""CSS Templates.""" +from dataclasses import dataclass +from typing import Optional + +from supersetapiclient.base import Object, ObjectFactories, default_string, raise_for_status + + +@dataclass +class CssTemplate(Object): + template_name: str + id: Optional[int] = None + css: str = default_string() + + +class CssTemplates(ObjectFactories): + endpoint = "css_template/" + base_object = CssTemplate diff --git a/supersetapiclient/dashboards.py b/supersetapiclient/dashboards.py index ca35cfd..265bfb5 100644 --- a/supersetapiclient/dashboards.py +++ b/supersetapiclient/dashboards.py @@ -4,6 +4,7 @@ from supersetapiclient.base import Object, ObjectFactories, default_string, json_field, raise_for_status + @dataclass class DashboardEmbed(Object): allowed_domains: List[str] = field(default_factory=list) @@ -54,8 +55,8 @@ def get_charts(self) -> List[int]: def get_embed(self) -> DashboardEmbed: """Get the dashboard's embedded configuration""" client = self._parent.client - embed_url = client.join_urls(self.base_url,"embedded") - response = client.get(embed_url) + embed_dashboard_url = client.join_urls(self.base_url,"embedded") + response = client.get(embed_dashboard_url) if response.status_code == 404: return None return DashboardEmbed().from_json(response.json().get("result")) @@ -63,11 +64,17 @@ def get_embed(self) -> DashboardEmbed: def create_embed(self, allowed_domains: List[str]) -> DashboardEmbed: """Set a dashboard's embedded configuration""" client = self._parent.client - embed_url = client.join_urls(self.base_url,"embedded") - response = client.post(embed_url, json={ "allowed_domains": allowed_domains }) + embed_dashboard_url = client.join_urls(self.base_url,"embedded") + response = client.post(embed_dashboard_url, json={ "allowed_domains": allowed_domains }) raise_for_status(response) return DashboardEmbed().from_json(response.json().get("result")) - + + def copy_dashboard(self, dashboard_payload: dict) -> None: + """Copy the dashboard with the given payload""" + client = self._parent.client + copy_dashboard_url = client.join_urls(self.base_url,"copy") + response = client.post(copy_dashboard_url, json=dashboard_payload) + raise_for_status(response) class Dashboards(ObjectFactories): endpoint = "dashboard/"