|
| 1 | +# (C) 2024 GoodData Corporation |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from typing import Any, Optional, Union |
| 5 | + |
| 6 | +import attr |
| 7 | +from gooddata_api_client.model.json_api_filter_view_in import JsonApiFilterViewIn |
| 8 | +from gooddata_api_client.model.json_api_filter_view_in_attributes import JsonApiFilterViewInAttributes |
| 9 | +from gooddata_api_client.model.json_api_filter_view_in_document import JsonApiFilterViewInDocument |
| 10 | +from gooddata_api_client.model.json_api_filter_view_in_relationships import JsonApiFilterViewInRelationships |
| 11 | + |
| 12 | +from gooddata_sdk.catalog.base import Base |
| 13 | +from gooddata_sdk.catalog.identifier import CatalogDeclarativeAnalyticalDashboardIdentifier, CatalogUserIdentifier |
| 14 | + |
| 15 | + |
| 16 | +@attr.s(auto_attribs=True, kw_only=True) |
| 17 | +class CatalogFilterViewDocument(Base): |
| 18 | + data: CatalogFilterView |
| 19 | + |
| 20 | + @staticmethod |
| 21 | + def client_class() -> type[JsonApiFilterViewInDocument]: |
| 22 | + return JsonApiFilterViewInDocument |
| 23 | + |
| 24 | + def to_api(self) -> JsonApiFilterViewInDocument: |
| 25 | + return JsonApiFilterViewInDocument(data=self.data.to_api()) |
| 26 | + |
| 27 | + |
| 28 | +def _data_entity(value: Any) -> dict[str, Any]: |
| 29 | + return {"data": value} |
| 30 | + |
| 31 | + |
| 32 | +@attr.s(auto_attribs=True, kw_only=True) |
| 33 | +class CatalogFilterView(Base): |
| 34 | + id: Optional[str] = None |
| 35 | + attributes: CatalogFilterViewAttributes |
| 36 | + relationships: Optional[CatalogFilterViewRelationships] = None |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def client_class() -> type[JsonApiFilterViewIn]: |
| 40 | + return JsonApiFilterViewIn |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def init( |
| 44 | + cls, |
| 45 | + filter_view_id: str, |
| 46 | + content: dict[str, Any], |
| 47 | + title: str, |
| 48 | + are_relations_valid: Optional[bool] = None, |
| 49 | + description: Optional[str] = None, |
| 50 | + is_default: Optional[bool] = None, |
| 51 | + tags: Optional[list[str]] = None, |
| 52 | + user_id: Optional[str] = None, |
| 53 | + analytical_dashboard_id: Optional[str] = None, |
| 54 | + ) -> CatalogFilterView: |
| 55 | + attributes = CatalogFilterViewAttributes( |
| 56 | + content=content, |
| 57 | + title=title, |
| 58 | + are_relations_valid=are_relations_valid, |
| 59 | + description=description, |
| 60 | + is_default=is_default, |
| 61 | + tags=tags, |
| 62 | + ) |
| 63 | + relationships = CatalogFilterViewRelationships.create_user_analytical_dashboard_relationship( |
| 64 | + user_id=user_id, analytical_dashboard_id=analytical_dashboard_id |
| 65 | + ) |
| 66 | + return cls(id=filter_view_id, attributes=attributes, relationships=relationships) |
| 67 | + |
| 68 | + def to_api(self) -> JsonApiFilterViewIn: |
| 69 | + attributes = self.attributes.to_api() |
| 70 | + relationships = self.relationships.to_api() if self.relationships is not None else None |
| 71 | + return JsonApiFilterViewIn(id=self.id, attributes=attributes, relationships=relationships) |
| 72 | + |
| 73 | + @property |
| 74 | + def user_id(self) -> Union[str, None]: |
| 75 | + if self.relationships and self.relationships.user: |
| 76 | + return self.relationships.user["data"].id |
| 77 | + return None |
| 78 | + |
| 79 | + @property |
| 80 | + def analytical_dashboard_id(self) -> Union[str, None]: |
| 81 | + if self.relationships and self.relationships.analytical_dashboard: |
| 82 | + return self.relationships.analytical_dashboard["data"].id |
| 83 | + return None |
| 84 | + |
| 85 | + def assign_user(self, user_id: str) -> None: |
| 86 | + if self.relationships is None: |
| 87 | + self.relationships = CatalogFilterViewRelationships.create_user_analytical_dashboard_relationship( |
| 88 | + user_id=user_id |
| 89 | + ) |
| 90 | + else: |
| 91 | + self.relationships.user = _data_entity(CatalogUserIdentifier(id=user_id, type="user")) |
| 92 | + |
| 93 | + def assign_analytical_dashboard(self, analytical_dashboard_id: str) -> None: |
| 94 | + if self.relationships is None: |
| 95 | + self.relationships = CatalogFilterViewRelationships.create_user_analytical_dashboard_relationship( |
| 96 | + analytical_dashboard_id=analytical_dashboard_id |
| 97 | + ) |
| 98 | + else: |
| 99 | + self.relationships.analytical_dashboard = _data_entity( |
| 100 | + CatalogDeclarativeAnalyticalDashboardIdentifier(id=analytical_dashboard_id, type="analyticalDashboard") |
| 101 | + ) |
| 102 | + |
| 103 | + def clean_relationships(self) -> None: |
| 104 | + if self.relationships is not None: |
| 105 | + self.relationships.user = None |
| 106 | + self.relationships.analytical_dashboard = None |
| 107 | + |
| 108 | + |
| 109 | +@attr.s(auto_attribs=True, kw_only=True) |
| 110 | +class CatalogFilterViewAttributes(Base): |
| 111 | + content: dict[str, Any] |
| 112 | + title: str |
| 113 | + are_relations_valid: Optional[bool] = None |
| 114 | + description: Optional[str] = None |
| 115 | + is_default: Optional[bool] = None |
| 116 | + tags: Optional[list[str]] = None |
| 117 | + |
| 118 | + @staticmethod |
| 119 | + def client_class() -> type[JsonApiFilterViewInAttributes]: |
| 120 | + return JsonApiFilterViewInAttributes |
| 121 | + |
| 122 | + |
| 123 | +@attr.s(auto_attribs=True, kw_only=True) |
| 124 | +class CatalogFilterViewRelationships(Base): |
| 125 | + user: Optional[dict[str, CatalogUserIdentifier]] = None |
| 126 | + analytical_dashboard: Optional[dict[str, CatalogDeclarativeAnalyticalDashboardIdentifier]] = None |
| 127 | + |
| 128 | + @staticmethod |
| 129 | + def client_class() -> type[JsonApiFilterViewInRelationships]: |
| 130 | + return JsonApiFilterViewInRelationships |
| 131 | + |
| 132 | + @classmethod |
| 133 | + def create_user_analytical_dashboard_relationship( |
| 134 | + cls, user_id: Optional[str] = None, analytical_dashboard_id: Optional[str] = None |
| 135 | + ) -> CatalogFilterViewRelationships | None: |
| 136 | + if user_id is None and analytical_dashboard_id is None: |
| 137 | + return None |
| 138 | + assignee_user = _data_entity(CatalogUserIdentifier(id=user_id, type="user")) if user_id else None |
| 139 | + assignee_analytical_dashboard = ( |
| 140 | + _data_entity( |
| 141 | + CatalogDeclarativeAnalyticalDashboardIdentifier(id=analytical_dashboard_id, type="analyticalDashboard") |
| 142 | + ) |
| 143 | + if analytical_dashboard_id |
| 144 | + else None |
| 145 | + ) |
| 146 | + return cls(user=assignee_user, analytical_dashboard=assignee_analytical_dashboard) |
0 commit comments