|
| 1 | +"""API for adding scripts, styles, initial-states to the Nextcloud UI.""" |
| 2 | + |
| 3 | +import dataclasses |
| 4 | +import typing |
| 5 | + |
| 6 | +from ..._exceptions import NextcloudExceptionNotFound |
| 7 | +from ..._misc import require_capabilities |
| 8 | +from ..._session import NcSessionApp |
| 9 | + |
| 10 | + |
| 11 | +@dataclasses.dataclass |
| 12 | +class UiBase: |
| 13 | + """Basic class for InitialStates, Scripts, Styles.""" |
| 14 | + |
| 15 | + def __init__(self, raw_data: dict): |
| 16 | + self._raw_data = raw_data |
| 17 | + |
| 18 | + @property |
| 19 | + def appid(self) -> str: |
| 20 | + """The App ID of the owner of this UI.""" |
| 21 | + return self._raw_data["appid"] |
| 22 | + |
| 23 | + @property |
| 24 | + def ui_type(self) -> str: |
| 25 | + """UI type. Possible values: 'top_menu'.""" |
| 26 | + return self._raw_data["type"] |
| 27 | + |
| 28 | + @property |
| 29 | + def name(self) -> str: |
| 30 | + """UI page name, acts like ID.""" |
| 31 | + return self._raw_data["name"] |
| 32 | + |
| 33 | + |
| 34 | +class UiInitState(UiBase): |
| 35 | + """One Initial State description.""" |
| 36 | + |
| 37 | + @property |
| 38 | + def key(self) -> str: |
| 39 | + """Name of the object.""" |
| 40 | + return self._raw_data["key"] |
| 41 | + |
| 42 | + @property |
| 43 | + def value(self) -> typing.Union[dict, list]: |
| 44 | + """Object for the page(template).""" |
| 45 | + return self._raw_data["value"] |
| 46 | + |
| 47 | + def __repr__(self): |
| 48 | + return f"<{self.__class__.__name__} type={self.ui_type}, name={self.name}, key={self.key}>" |
| 49 | + |
| 50 | + |
| 51 | +class UiScript(UiBase): |
| 52 | + """One Script description.""" |
| 53 | + |
| 54 | + @property |
| 55 | + def path(self) -> str: |
| 56 | + """Url to script relative to the ExApp.""" |
| 57 | + return self._raw_data["path"] |
| 58 | + |
| 59 | + @property |
| 60 | + def after_app_id(self) -> str: |
| 61 | + """Optional AppID after which script should be injected.""" |
| 62 | + return self._raw_data["after_app_id"] if self._raw_data["after_app_id"] else "" |
| 63 | + |
| 64 | + def __repr__(self): |
| 65 | + return f"<{self.__class__.__name__} type={self.ui_type}, name={self.name}, path={self.path}>" |
| 66 | + |
| 67 | + |
| 68 | +class UiStyle(UiBase): |
| 69 | + """One Style description.""" |
| 70 | + |
| 71 | + @property |
| 72 | + def path(self) -> str: |
| 73 | + """Url to style relative to the ExApp.""" |
| 74 | + return self._raw_data["path"] |
| 75 | + |
| 76 | + def __repr__(self): |
| 77 | + return f"<{self.__class__.__name__} type={self.ui_type}, name={self.name}, path={self.path}>" |
| 78 | + |
| 79 | + |
| 80 | +class _UiResources: |
| 81 | + """API for adding scripts, styles, initial-states to the TopMenu pages.""" |
| 82 | + |
| 83 | + _ep_suffix_init_state: str = "ui/initial-state" |
| 84 | + _ep_suffix_js: str = "ui/script" |
| 85 | + _ep_suffix_css: str = "ui/style" |
| 86 | + |
| 87 | + def __init__(self, session: NcSessionApp): |
| 88 | + self._session = session |
| 89 | + |
| 90 | + def set_initial_state(self, ui_type: str, name: str, key: str, value: typing.Union[dict, list]) -> None: |
| 91 | + """Add or update initial state for the page(template).""" |
| 92 | + require_capabilities("app_api", self._session.capabilities) |
| 93 | + params = { |
| 94 | + "type": ui_type, |
| 95 | + "name": name, |
| 96 | + "key": key, |
| 97 | + "value": value, |
| 98 | + } |
| 99 | + self._session.ocs(method="POST", path=f"{self._session.ae_url}/{self._ep_suffix_init_state}", json=params) |
| 100 | + |
| 101 | + def delete_initial_state(self, ui_type: str, name: str, key: str, not_fail=True) -> None: |
| 102 | + """Removes initial state for the page(template) by object name.""" |
| 103 | + require_capabilities("app_api", self._session.capabilities) |
| 104 | + try: |
| 105 | + self._session.ocs( |
| 106 | + method="DELETE", |
| 107 | + path=f"{self._session.ae_url}/{self._ep_suffix_init_state}", |
| 108 | + params={"type": ui_type, "name": name, "key": key}, |
| 109 | + ) |
| 110 | + except NextcloudExceptionNotFound as e: |
| 111 | + if not not_fail: |
| 112 | + raise e from None |
| 113 | + |
| 114 | + def get_initial_state(self, ui_type: str, name: str, key: str) -> typing.Optional[UiInitState]: |
| 115 | + """Get information about initial state for the page(template) by object name.""" |
| 116 | + require_capabilities("app_api", self._session.capabilities) |
| 117 | + try: |
| 118 | + return UiInitState( |
| 119 | + self._session.ocs( |
| 120 | + method="GET", |
| 121 | + path=f"{self._session.ae_url}/{self._ep_suffix_init_state}", |
| 122 | + params={"type": ui_type, "name": name, "key": key}, |
| 123 | + ) |
| 124 | + ) |
| 125 | + except NextcloudExceptionNotFound: |
| 126 | + return None |
| 127 | + |
| 128 | + def set_script(self, ui_type: str, name: str, path: str, after_app_id: str = "") -> None: |
| 129 | + """Add or update script for the page(template).""" |
| 130 | + require_capabilities("app_api", self._session.capabilities) |
| 131 | + params = { |
| 132 | + "type": ui_type, |
| 133 | + "name": name, |
| 134 | + "path": path, |
| 135 | + "afterAppId": after_app_id, |
| 136 | + } |
| 137 | + self._session.ocs(method="POST", path=f"{self._session.ae_url}/{self._ep_suffix_js}", json=params) |
| 138 | + |
| 139 | + def delete_script(self, ui_type: str, name: str, path: str, not_fail=True) -> None: |
| 140 | + """Removes script for the page(template) by object name.""" |
| 141 | + require_capabilities("app_api", self._session.capabilities) |
| 142 | + try: |
| 143 | + self._session.ocs( |
| 144 | + method="DELETE", |
| 145 | + path=f"{self._session.ae_url}/{self._ep_suffix_js}", |
| 146 | + params={"type": ui_type, "name": name, "path": path}, |
| 147 | + ) |
| 148 | + except NextcloudExceptionNotFound as e: |
| 149 | + if not not_fail: |
| 150 | + raise e from None |
| 151 | + |
| 152 | + def get_script(self, ui_type: str, name: str, path: str) -> typing.Optional[UiScript]: |
| 153 | + """Get information about script for the page(template) by object name.""" |
| 154 | + require_capabilities("app_api", self._session.capabilities) |
| 155 | + try: |
| 156 | + return UiScript( |
| 157 | + self._session.ocs( |
| 158 | + method="GET", |
| 159 | + path=f"{self._session.ae_url}/{self._ep_suffix_js}", |
| 160 | + params={"type": ui_type, "name": name, "path": path}, |
| 161 | + ) |
| 162 | + ) |
| 163 | + except NextcloudExceptionNotFound: |
| 164 | + return None |
| 165 | + |
| 166 | + def set_style(self, ui_type: str, name: str, path: str) -> None: |
| 167 | + """Add or update style(css) for the page(template).""" |
| 168 | + require_capabilities("app_api", self._session.capabilities) |
| 169 | + params = { |
| 170 | + "type": ui_type, |
| 171 | + "name": name, |
| 172 | + "path": path, |
| 173 | + } |
| 174 | + self._session.ocs(method="POST", path=f"{self._session.ae_url}/{self._ep_suffix_css}", json=params) |
| 175 | + |
| 176 | + def delete_style(self, ui_type: str, name: str, path: str, not_fail=True) -> None: |
| 177 | + """Removes style(css) for the page(template) by object name.""" |
| 178 | + require_capabilities("app_api", self._session.capabilities) |
| 179 | + try: |
| 180 | + self._session.ocs( |
| 181 | + method="DELETE", |
| 182 | + path=f"{self._session.ae_url}/{self._ep_suffix_css}", |
| 183 | + params={"type": ui_type, "name": name, "path": path}, |
| 184 | + ) |
| 185 | + except NextcloudExceptionNotFound as e: |
| 186 | + if not not_fail: |
| 187 | + raise e from None |
| 188 | + |
| 189 | + def get_style(self, ui_type: str, name: str, path: str) -> typing.Optional[UiStyle]: |
| 190 | + """Get information about style(css) for the page(template) by object name.""" |
| 191 | + require_capabilities("app_api", self._session.capabilities) |
| 192 | + try: |
| 193 | + return UiStyle( |
| 194 | + self._session.ocs( |
| 195 | + method="GET", |
| 196 | + path=f"{self._session.ae_url}/{self._ep_suffix_css}", |
| 197 | + params={"type": ui_type, "name": name, "path": path}, |
| 198 | + ) |
| 199 | + ) |
| 200 | + except NextcloudExceptionNotFound: |
| 201 | + return None |
0 commit comments