annotate libervia/pages/lists/create_from_tpl/page_meta.py @ 1509:106bae41f5c8

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:44:11 +0200
parents a84383c659b4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 from sat.tools.common import data_format
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from sat.core.log import getLogger
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from sat.core.i18n import D_
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from sat.core import exceptions
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 from libervia.server.constants import Const as C
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from sat_frontends.bridge.bridge_frontend import BridgeException
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
9
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 log = getLogger(__name__)
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 name = "list_create_from_tpl"
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 access = C.PAGES_ACCESS_PROFILE
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 template = "list/create_from_template.html"
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 def parse_url(self, request):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
18 self.get_path_args(request, ["template_id"])
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 async def prepare_render(self, request):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
21 data = self.get_r_data(request)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 template_id = data["template_id"]
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 if not template_id:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
24 self.page_error(request, C.HTTP_BAD_REQUEST)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 template_data = request.template_data
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
27 profile = self.get_profile(request)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
28 tpl_raw = await self.host.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
29 "list_template_get",
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 template_id,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 "",
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 profile,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 )
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 template = data_format.deserialise(tpl_raw)
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 template['id'] = template_id
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 template_data["list_template"] = template
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 async def on_data_post(self, request):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
39 data = self.get_r_data(request)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 template_id = data['template_id']
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
41 name, access = self.get_posted_data(request, ('name', 'access'))
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 if access == 'private':
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 access_model = 'whitelist'
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 elif access == 'public':
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 access_model = 'open'
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 else:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 log.warning(f"Unknown access for template creation: {access}")
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
48 self.page_error(request, C.HTTP_BAD_REQUEST)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
49 profile = self.get_profile(request)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
51 service, node = await self.host.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
52 "list_template_create", template_id, name, access_model, profile
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 )
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 except BridgeException as e:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 if e.condition == "conflict":
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 raise exceptions.DataError(D_("A list with this name already exists"))
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 else:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 log.error(f"Can't create list from template: {e}")
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 raise e
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 data["post_redirect_page"] = (
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
61 self.get_page_by_name("lists"),
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 service,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 node or "@",
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 )