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