diff libervia/pages/lists/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 libervia/pages/lists/disco/page_meta.py@e3e303a30a74
children 68ffd60a58a5
line wrap: on
line diff
--- a/libervia/pages/lists/page_meta.py	Sat Feb 20 13:58:42 2021 +0100
+++ b/libervia/pages/lists/page_meta.py	Sat Feb 20 14:07:22 2021 +0100
@@ -1,58 +1,69 @@
 #!/usr/bin/env python3
 
 from libervia.server.constants import Const as C
-from sat.tools.common import template_xmlui
-from sat.tools.common import data_objects
+from twisted.words.protocols.jabber import jid
+from sat.core.i18n import _
+from sat.core.log import getLogger
 from sat.tools.common import data_format
-from sat.core.log import getLogger
 
 log = getLogger(__name__)
 
-name = "lists"
+name = "lists_disco"
 access = C.PAGES_ACCESS_PUBLIC
-template = "list/overview.html"
-
-
-def parse_url(self, request):
-    self.getPathArgs(request, ["service", "node"], service="jid")
-    data = self.getRData(request)
-    service, node = data["service"], data["node"]
-    if node is None:
-        self.pageRedirect("lists_disco", request)
-    if node == "@":
-        node = data["node"] = ""
-    template_data = request.template_data
-    template_data["url_list_items"] = self.getURL(service.full(), node or "@")
-    template_data["url_list_new"] = self.getSubPageURL(request, "list_new")
-
+template = "list/discover.html"
 
 async def prepare_render(self, request):
-    data = self.getRData(request)
+    profile = self.getProfile(request)
     template_data = request.template_data
-    service, node = data["service"], data["node"]
-    profile = self.getProfile(request) or C.SERVICE_PROFILE
+    template_data["url_list_create"] = self.getPageByName("list_create").url
+    lists_directory_config = self.host.options["lists_directory_json"]
+    lists_directory = request.template_data["lists_directory"] = []
 
-    self.checkCache(request, C.CACHE_PUBSUB, service=service, node=node, short="tickets")
-
-    extra = self.getPubsubExtra(request)
-    extra["labels_as_list"] = C.BOOL_TRUE
-    self.handleSearch(request, extra)
+    if lists_directory_config:
+        try:
+            for list_data in lists_directory_config:
+                service = list_data["service"]
+                node = list_data["node"]
+                name = list_data["name"]
+                url = self.getPageByName("lists").getURL(service, node)
+                lists_directory.append({"name": name, "url": url})
+        except KeyError as e:
+            log.warning("Missing field in lists_directory_json: {msg}".format(msg=e))
+        except Exception as e:
+            log.warning("Can't decode lists directory: {msg}".format(msg=e))
 
-    list_raw = await self.host.bridgeCall(
-        "listGet",
-        service.full() if service else "",
-        node,
-        C.NO_LIMIT,
-        [],
-        "",
-        extra,
-        profile,
-    )
-    list_items, metadata = data_format.deserialise(list_raw, type_check=list)
-    template_data["list_items"] = [
-        template_xmlui.create(self.host, x) for x in list_items
-    ]
-    template_data["on_list_item_click"] = data_objects.OnClick(
-        url=self.getSubPageURL(request, "list_view") + "/{item.id}"
-    )
-    self.setPagination(request, metadata)
+    if profile is not None:
+        try:
+            lists_list_raw = await self.host.bridgeCall("listsList", "", "", profile)
+        except Exception as e:
+            log.warning(
+                _("Can't get list of registered lists for {profile}: {reason}")
+                .format(profile=profile, reason=e)
+            )
+        else:
+            lists_list = data_format.deserialise(lists_list_raw, type_check=list)
+            for list_data in lists_list:
+                service = list_data["service"]
+                node = list_data["node"]
+                list_data["url"] = self.getPageByName("lists").getURL(service, node)
+                lists_directory.append(list_data)
+
+    icons_names = set()
+    for list_data in lists_directory:
+        try:
+            icons_names.add(list_data['icon_name'])
+        except KeyError:
+            pass
+    if icons_names:
+        template_data["icons_names"] = icons_names
+
+
+def on_data_post(self, request):
+    jid_str = self.getPostedData(request, "jid")
+    try:
+        jid_ = jid.JID(jid_str)
+    except RuntimeError:
+        self.pageError(request, C.HTTP_BAD_REQUEST)
+    # for now we just use default node
+    url = self.getPageByName("lists").getURL(jid_.full(), "@")
+    self.HTTPRedirect(request, url)