|
1 | 1 | """Example with which we test UI elements with L10N support.""" |
| 2 | + |
2 | 3 | import locale |
3 | 4 | import os |
4 | 5 | import random |
5 | | -import typing |
6 | 6 | from contextlib import asynccontextmanager |
7 | 7 |
|
8 | | -from fastapi import Depends, FastAPI, responses |
| 8 | +from fastapi import FastAPI, responses |
9 | 9 | from pydantic import BaseModel |
10 | 10 |
|
11 | 11 | from nc_py_api import NextcloudApp |
12 | | -from nc_py_api.ex_app import nc_app, run_app, set_handlers |
| 12 | +from nc_py_api.ex_app import ( |
| 13 | + run_app, |
| 14 | + set_handlers, |
| 15 | + AppAPIAuthMiddleware, |
| 16 | + SettingsField, |
| 17 | + SettingsFieldType, |
| 18 | + SettingsForm, |
| 19 | +) |
13 | 20 |
|
14 | 21 | import gettext |
15 | 22 |
|
16 | 23 | # ../locale/<lang>/LC_MESSAGES/<app_id>.(mo|po) |
17 | 24 | localedir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "locale") |
18 | 25 | locale.setlocale(locale.LC_ALL) |
19 | 26 | locale.bindtextdomain(os.getenv("APP_ID"), localedir) |
20 | | -my_l10n = gettext.translation(os.getenv("APP_ID"), localedir, fallback=True, languages=['en', 'uk']) |
| 27 | +my_l10n = gettext.translation( |
| 28 | + os.getenv("APP_ID"), localedir, fallback=True, languages=["en", "uk"] |
| 29 | +) |
21 | 30 | my_l10n.install() |
22 | 31 |
|
23 | 32 | _ = my_l10n.gettext |
24 | 33 | _n = my_l10n.ngettext |
25 | 34 |
|
26 | 35 |
|
27 | 36 | @asynccontextmanager |
28 | | -async def lifespan(_app: FastAPI): |
29 | | - set_handlers(APP, enabled_handler) |
30 | | - print(_('UI example')) |
31 | | - yield |
| 37 | +async def lifespan(app: FastAPI): |
| 38 | + set_handlers(app, enabled_handler) |
| 39 | + print(_("UI example")) |
| 40 | + yield |
32 | 41 |
|
33 | 42 |
|
34 | 43 | APP = FastAPI(lifespan=lifespan) |
| 44 | +APP.add_middleware(AppAPIAuthMiddleware) |
| 45 | + |
| 46 | +SETTINGS_EXAMPLE = SettingsForm( |
| 47 | + id="settings_example", |
| 48 | + section_type="admin", |
| 49 | + section_id="ai_integration_team", |
| 50 | + title="Example of declarative settings", |
| 51 | + description="These fields are rendered dynamically from declarative schema", |
| 52 | + fields=[ |
| 53 | + SettingsField( |
| 54 | + id="field1", |
| 55 | + title="Multi-selection", |
| 56 | + description="Select some option setting", |
| 57 | + type=SettingsFieldType.MULTI_SELECT, |
| 58 | + default=["foo", "bar"], |
| 59 | + placeholder="Select some multiple options", |
| 60 | + options=["foo", "bar", "baz"], |
| 61 | + ), |
| 62 | + SettingsField( |
| 63 | + id="some_real_setting", |
| 64 | + title="Choose init status check background job interval", |
| 65 | + description="How often ExApp should check for initialization status", |
| 66 | + type=SettingsFieldType.RADIO, |
| 67 | + default="40m", |
| 68 | + placeholder="Choose init status check background job interval", |
| 69 | + options={ |
| 70 | + "Each 40 minutes": "40m", |
| 71 | + "Each 60 minutes": "60m", |
| 72 | + "Each 120 minutes": "120m", |
| 73 | + "Each day": f"{60 * 24}m", |
| 74 | + }, |
| 75 | + ), |
| 76 | + SettingsField( |
| 77 | + id="test_ex_app_field_1", |
| 78 | + title="Default text field", |
| 79 | + description="Set some simple text setting", |
| 80 | + type=SettingsFieldType.TEXT, |
| 81 | + default="foo", |
| 82 | + placeholder="Enter text setting", |
| 83 | + ), |
| 84 | + SettingsField( |
| 85 | + id="test_ex_app_field_1_1", |
| 86 | + title="Email field", |
| 87 | + description="Set email config", |
| 88 | + type=SettingsFieldType.EMAIL, |
| 89 | + default="", |
| 90 | + placeholder="Enter email", |
| 91 | + ), |
| 92 | + SettingsField( |
| 93 | + id="test_ex_app_field_1_2", |
| 94 | + title="Tel field", |
| 95 | + description="Set tel config", |
| 96 | + type=SettingsFieldType.TEL, |
| 97 | + default="", |
| 98 | + placeholder="Enter your tel", |
| 99 | + ), |
| 100 | + SettingsField( |
| 101 | + id="test_ex_app_field_1_3", |
| 102 | + title="Url (website) field", |
| 103 | + description="Set url config", |
| 104 | + type=SettingsFieldType.URL, |
| 105 | + default="", |
| 106 | + placeholder="Enter url", |
| 107 | + ), |
| 108 | + SettingsField( |
| 109 | + id="test_ex_app_field_1_4", |
| 110 | + title="Number field", |
| 111 | + description="Set number config", |
| 112 | + type=SettingsFieldType.NUMBER, |
| 113 | + default=0, |
| 114 | + placeholder="Enter number value", |
| 115 | + ), |
| 116 | + SettingsField( |
| 117 | + id="test_ex_app_field_2", |
| 118 | + title="Password", |
| 119 | + description="Set some secure value setting", |
| 120 | + type=SettingsFieldType.PASSWORD, |
| 121 | + default="", |
| 122 | + placeholder="Set secure value", |
| 123 | + ), |
| 124 | + SettingsField( |
| 125 | + id="test_ex_app_field_3", |
| 126 | + title="Selection", |
| 127 | + description="Select some option setting", |
| 128 | + type=SettingsFieldType.SELECT, |
| 129 | + default="foo", |
| 130 | + placeholder="Select some option setting", |
| 131 | + options=["foo", "bar", "baz"], |
| 132 | + ), |
| 133 | + SettingsField( |
| 134 | + id="test_ex_app_field_3", |
| 135 | + title="Selection", |
| 136 | + description="Select some option setting", |
| 137 | + type=SettingsFieldType.SELECT, |
| 138 | + default="foo", |
| 139 | + placeholder="Select some option setting", |
| 140 | + options=["foo", "bar", "baz"], |
| 141 | + ), |
| 142 | + SettingsField( |
| 143 | + id="test_ex_app_field_4", |
| 144 | + title="Toggle something", |
| 145 | + description="Select checkbox option setting", |
| 146 | + type=SettingsFieldType.CHECKBOX, |
| 147 | + default=False, |
| 148 | + label="Verify something if enabled", |
| 149 | + ), |
| 150 | + SettingsField( |
| 151 | + id="test_ex_app_field_5", |
| 152 | + title="Multiple checkbox toggles, describing one setting", |
| 153 | + description="Select checkbox option setting", |
| 154 | + type=SettingsFieldType.MULTI_CHECKBOX, |
| 155 | + default={"foo": True, "bar": True}, |
| 156 | + options={"Foo": "foo", "Bar": "bar", "Baz": "baz", "Qux": "qux"}, |
| 157 | + ), |
| 158 | + SettingsField( |
| 159 | + id="test_ex_app_field_6", |
| 160 | + title="Radio toggles, describing one setting like single select", |
| 161 | + description="Select radio option setting", |
| 162 | + type=SettingsFieldType.RADIO, |
| 163 | + label="Select single toggle", |
| 164 | + default="foo", |
| 165 | + options={"First radio": "foo", "Second radio": "bar", "Third radie": "baz"}, |
| 166 | + ), |
| 167 | + ], |
| 168 | +) |
35 | 169 |
|
36 | 170 |
|
37 | 171 | def enabled_handler(enabled: bool, nc: NextcloudApp) -> str: |
38 | | - print(f"enabled={enabled}") |
39 | | - if enabled: |
40 | | - nc.ui.resources.set_initial_state( |
41 | | - "top_menu", "first_menu", "ui_example_state", {"initial_value": "test init value"} |
42 | | - ) |
43 | | - nc.ui.resources.set_script("top_menu", "first_menu", "js/ui_example-main") |
44 | | - nc.ui.top_menu.register("first_menu", "UI example", "img/app.svg") |
45 | | - else: |
46 | | - nc.ui.resources.delete_initial_state("top_menu", "first_menu", "ui_example_state") |
47 | | - nc.ui.resources.delete_script("top_menu", "first_menu", "js/ui_example-main") |
48 | | - nc.ui.top_menu.unregister("first_menu") |
49 | | - return "" |
| 172 | + print(f"enabled={enabled}") |
| 173 | + if enabled: |
| 174 | + nc.ui.resources.set_initial_state( |
| 175 | + "top_menu", |
| 176 | + "first_menu", |
| 177 | + "ui_example_state", |
| 178 | + {"initial_value": "test init value"}, |
| 179 | + ) |
| 180 | + nc.ui.resources.set_script("top_menu", "first_menu", "js/ui_example-main") |
| 181 | + nc.ui.top_menu.register("first_menu", "UI example", "img/app.svg") |
| 182 | + if nc.srv_version["major"] >= 29: |
| 183 | + nc.ui.settings.register_form(SETTINGS_EXAMPLE) |
| 184 | + else: |
| 185 | + nc.ui.resources.delete_initial_state( |
| 186 | + "top_menu", "first_menu", "ui_example_state" |
| 187 | + ) |
| 188 | + nc.ui.resources.delete_script("top_menu", "first_menu", "js/ui_example-main") |
| 189 | + nc.ui.top_menu.unregister("first_menu") |
| 190 | + return "" |
50 | 191 |
|
51 | 192 |
|
52 | 193 | class Button1Format(BaseModel): |
53 | | - initial_value: str |
| 194 | + initial_value: str |
54 | 195 |
|
55 | 196 |
|
56 | 197 | @APP.post("/verify_initial_value") |
57 | 198 | async def verify_initial_value( |
58 | | - _nc: typing.Annotated[NextcloudApp, Depends(nc_app)], |
59 | | - input1: Button1Format, |
| 199 | + input1: Button1Format, |
60 | 200 | ): |
61 | | - print("Old value: ", input1.initial_value) |
62 | | - return responses.JSONResponse(content={"initial_value": str(random.randint(0, 100))}, status_code=200) |
| 201 | + print("Old value: ", input1.initial_value) |
| 202 | + return responses.JSONResponse( |
| 203 | + content={"initial_value": str(random.randint(0, 100))}, status_code=200 |
| 204 | + ) |
63 | 205 |
|
64 | 206 |
|
65 | 207 | class FileInfo(BaseModel): |
66 | | - getlastmodified: str |
67 | | - getetag: str |
68 | | - getcontenttype: str |
69 | | - fileid: int |
70 | | - permissions: str |
71 | | - size: int |
72 | | - getcontentlength: int |
73 | | - favorite: int |
| 208 | + getlastmodified: str |
| 209 | + getetag: str |
| 210 | + getcontenttype: str |
| 211 | + fileid: int |
| 212 | + permissions: str |
| 213 | + size: int |
| 214 | + getcontentlength: int |
| 215 | + favorite: int |
74 | 216 |
|
75 | 217 |
|
76 | 218 | @APP.post("/nextcloud_file") |
77 | 219 | async def nextcloud_file( |
78 | | - _nc: typing.Annotated[NextcloudApp, Depends(nc_app)], |
79 | | - args: dict, |
| 220 | + args: dict, |
80 | 221 | ): |
81 | | - print(args["file_info"]) |
82 | | - return responses.Response() |
| 222 | + print(args["file_info"]) |
| 223 | + return responses.Response() |
| 224 | + |
83 | 225 |
|
84 | 226 | if __name__ == "__main__": |
85 | | - run_app("main:APP", log_level="trace") |
| 227 | + run_app("main:APP", log_level="trace") |
0 commit comments