comparison libervia/web/pages/lists/create_from_tpl/page_meta.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents libervia/pages/lists/create_from_tpl/page_meta.py@106bae41f5c8
children
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3 from libervia.backend.tools.common import data_format
4 from libervia.backend.core.log import getLogger
5 from libervia.backend.core.i18n import D_
6 from libervia.backend.core import exceptions
7 from libervia.web.server.constants import Const as C
8 from libervia.frontends.bridge.bridge_frontend import BridgeException
9
10 log = getLogger(__name__)
11
12 name = "list_create_from_tpl"
13 access = C.PAGES_ACCESS_PROFILE
14 template = "list/create_from_template.html"
15
16
17 def parse_url(self, request):
18 self.get_path_args(request, ["template_id"])
19
20 async def prepare_render(self, request):
21 data = self.get_r_data(request)
22 template_id = data["template_id"]
23 if not template_id:
24 self.page_error(request, C.HTTP_BAD_REQUEST)
25
26 template_data = request.template_data
27 profile = self.get_profile(request)
28 tpl_raw = await self.host.bridge_call(
29 "list_template_get",
30 template_id,
31 "",
32 profile,
33 )
34 template = data_format.deserialise(tpl_raw)
35 template['id'] = template_id
36 template_data["list_template"] = template
37
38 async def on_data_post(self, request):
39 data = self.get_r_data(request)
40 template_id = data['template_id']
41 name, access = self.get_posted_data(request, ('name', 'access'))
42 if access == 'private':
43 access_model = 'whitelist'
44 elif access == 'public':
45 access_model = 'open'
46 else:
47 log.warning(f"Unknown access for template creation: {access}")
48 self.page_error(request, C.HTTP_BAD_REQUEST)
49 profile = self.get_profile(request)
50 try:
51 service, node = await self.host.bridge_call(
52 "list_template_create", template_id, name, access_model, profile
53 )
54 except BridgeException as e:
55 if e.condition == "conflict":
56 raise exceptions.DataError(D_("A list with this name already exists"))
57 else:
58 log.error(f"Can't create list from template: {e}")
59 raise e
60 data["post_redirect_page"] = (
61 self.get_page_by_name("lists"),
62 service,
63 node or "@",
64 )