diff libervia/pages/lists/create_from_tpl/page_meta.py @ 1387:a84383c659b4

lists: creation, invitation, item deletion: this big patch includes: - reorganisation of pages for consistency, discovery is now the main list page, and list overview is now in `view` while item view is moved to `view_item` - lists from lists of interest are now shown in discovery page - list deletion from discory page - list can now be created, using templates now available from backend - invitation manager can now be used from list overview - list item can now be deleted from `view_item`
author Goffi <goffi@goffi.org>
date Sat, 20 Feb 2021 14:07:22 +0100
parents
children 106bae41f5c8
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/pages/lists/create_from_tpl/page_meta.py	Sat Feb 20 14:07:22 2021 +0100
@@ -0,0 +1,64 @@
+#!/usr/bin/env python3
+
+from sat.tools.common import data_format
+from sat.core.log import getLogger
+from sat.core.i18n import D_
+from sat.core import exceptions
+from libervia.server.constants import Const as C
+from sat_frontends.bridge.bridge_frontend import BridgeException
+
+log = getLogger(__name__)
+
+name = "list_create_from_tpl"
+access = C.PAGES_ACCESS_PROFILE
+template = "list/create_from_template.html"
+
+
+def parse_url(self, request):
+    self.getPathArgs(request, ["template_id"])
+
+async def prepare_render(self, request):
+    data = self.getRData(request)
+    template_id = data["template_id"]
+    if not template_id:
+        self.pageError(request, C.HTTP_BAD_REQUEST)
+
+    template_data = request.template_data
+    profile = self.getProfile(request)
+    tpl_raw = await self.host.bridgeCall(
+        "listTemplateGet",
+        template_id,
+        "",
+        profile,
+    )
+    template = data_format.deserialise(tpl_raw)
+    template['id'] = template_id
+    template_data["list_template"] = template
+
+async def on_data_post(self, request):
+    data = self.getRData(request)
+    template_id = data['template_id']
+    name, access = self.getPostedData(request, ('name', 'access'))
+    if access == 'private':
+        access_model = 'whitelist'
+    elif access == 'public':
+        access_model = 'open'
+    else:
+        log.warning(f"Unknown access for template creation: {access}")
+        self.pageError(request, C.HTTP_BAD_REQUEST)
+    profile = self.getProfile(request)
+    try:
+        service, node = await self.host.bridgeCall(
+            "listTemplateCreate", template_id, name, access_model, profile
+        )
+    except BridgeException as e:
+        if e.condition == "conflict":
+            raise exceptions.DataError(D_("A list with this name already exists"))
+        else:
+            log.error(f"Can't create list from template: {e}")
+            raise e
+    data["post_redirect_page"] = (
+        self.getPageByName("lists"),
+        service,
+        node or "@",
+    )