File: top_menu.py

package info (click to toggle)
nc-py-api 0.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,320 kB
  • sloc: python: 12,415; makefile: 238; xml: 100; javascript: 56; sh: 14
file content (133 lines) | stat: -rw-r--r-- 5,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Nextcloud API for working with Top App menu."""

import dataclasses

from ..._exceptions import NextcloudExceptionNotFound
from ..._misc import require_capabilities
from ..._session import AsyncNcSessionApp, NcSessionApp


@dataclasses.dataclass
class UiTopMenuEntry:
    """App top menu entry description."""

    def __init__(self, raw_data: dict):
        self._raw_data = raw_data

    @property
    def appid(self) -> str:
        """App ID for which this entry is."""
        return self._raw_data["appid"]

    @property
    def name(self) -> str:
        """Top Menu entry name, acts like ID."""
        return self._raw_data["name"]

    @property
    def display_name(self) -> str:
        """Display name of the entry."""
        return self._raw_data["display_name"]

    @property
    def icon(self) -> str:
        """Relative to the ExApp url with icon or empty value to use the default one icon."""
        return self._raw_data["icon"] if self._raw_data["icon"] else ""

    @property
    def admin_required(self) -> bool:
        """Flag that determines whether the entry menu is displayed only for administrators."""
        return bool(int(self._raw_data["admin_required"]))

    def __repr__(self):
        return f"<{self.__class__.__name__} name={self.name}, admin_required={self.admin_required}>"


class _UiTopMenuAPI:
    """API for the top menu app nav bar in Nextcloud, avalaible as **nc.ui.top_menu.<method>**."""

    _ep_suffix: str = "ui/top-menu"

    def __init__(self, session: NcSessionApp):
        self._session = session

    def register(self, name: str, display_name: str, icon: str = "", admin_required=False) -> None:
        """Registers or edit the App entry in Top Meny.

        :param name: Unique name for the menu entry.
        :param display_name: Display name of the menu entry.
        :param icon: Optional, url relative to the ExApp, like: "img/icon.svg"
        :param admin_required: Boolean value indicating should be Entry visible to all or only to admins.
        """
        require_capabilities("app_api", self._session.capabilities)
        params = {
            "name": name,
            "displayName": display_name,
            "icon": icon,
            "adminRequired": int(admin_required),
        }
        self._session.ocs("POST", f"{self._session.ae_url}/{self._ep_suffix}", json=params)

    def unregister(self, name: str, not_fail=True) -> None:
        """Removes App entry in Top Menu."""
        require_capabilities("app_api", self._session.capabilities)
        try:
            self._session.ocs("DELETE", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
        except NextcloudExceptionNotFound as e:
            if not not_fail:
                raise e from None

    def get_entry(self, name: str) -> UiTopMenuEntry | None:
        """Get information of the top meny entry for current app."""
        require_capabilities("app_api", self._session.capabilities)
        try:
            return UiTopMenuEntry(
                self._session.ocs("GET", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
            )
        except NextcloudExceptionNotFound:
            return None


class _AsyncUiTopMenuAPI:
    """Async API for the top menu app nav bar in Nextcloud."""

    _ep_suffix: str = "ui/top-menu"

    def __init__(self, session: AsyncNcSessionApp):
        self._session = session

    async def register(self, name: str, display_name: str, icon: str = "", admin_required=False) -> None:
        """Registers or edit the App entry in Top Meny.

        :param name: Unique name for the menu entry.
        :param display_name: Display name of the menu entry.
        :param icon: Optional, url relative to the ExApp, like: "img/icon.svg"
        :param admin_required: Boolean value indicating should be Entry visible to all or only to admins.
        """
        require_capabilities("app_api", await self._session.capabilities)
        params = {
            "name": name,
            "displayName": display_name,
            "icon": icon,
            "adminRequired": int(admin_required),
        }
        await self._session.ocs("POST", f"{self._session.ae_url}/{self._ep_suffix}", json=params)

    async def unregister(self, name: str, not_fail=True) -> None:
        """Removes App entry in Top Menu."""
        require_capabilities("app_api", await self._session.capabilities)
        try:
            await self._session.ocs("DELETE", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
        except NextcloudExceptionNotFound as e:
            if not not_fail:
                raise e from None

    async def get_entry(self, name: str) -> UiTopMenuEntry | None:
        """Get information of the top meny entry for current app."""
        require_capabilities("app_api", await self._session.capabilities)
        try:
            return UiTopMenuEntry(
                await self._session.ocs("GET", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
            )
        except NextcloudExceptionNotFound:
            return None