changeset 1416:0554103ec700

pages (blog): new `edit` page: this page is used for new blog post and may be used for editing existing posts too. New `editor` module is using for autosave and tags editor. New `mbPreview` bridge method is used to generate preview when javascript is available.
author Goffi <goffi@goffi.org>
date Thu, 29 Apr 2021 16:55:07 +0200
parents 8415d882b686
children 314bba1ae433
files libervia/pages/blog/edit/_browser/__init__.py libervia/pages/blog/edit/page_meta.py libervia/pages/blog/page_meta.py
diffstat 3 files changed, 98 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/pages/blog/edit/_browser/__init__.py	Thu Apr 29 16:55:07 2021 +0200
@@ -0,0 +1,58 @@
+from browser import document, window, aio, bind
+from aio_bridge import Bridge, BridgeException
+import editor
+import dialog
+from javascript import JSON
+from template import Template
+
+bridge = Bridge()
+item_tpl = Template('blog/item.html')
+tab_select = window.tab_select
+
+async def on_preview(evt):
+    elt = evt.target
+    tab_preview = document["tab_preview"]
+    tab_preview.clear()
+    form = document["blog_post_edit"]
+    data = {
+        "content_rich": form.select_one('textarea[name="body"]').value.strip()
+    }
+    title = form.select_one('input[name="title"]').value.strip()
+    if title:
+        data["title_rich"] = title
+    tags = form.select_one('input[name="tags"]').value.strip()
+    if tags:
+        data['tags'] = [t.strip() for t in tags.split(',') if t.strip()]
+    try:
+        preview_data = JSON.parse(await bridge.mbPreview("", "", JSON.stringify(data)))
+    except BridgeException as e:
+        dialog.notification.show(
+            f"Can't generate item preview: {e.message}",
+            level="error"
+        )
+    else:
+        tab_select(elt, "tab_preview", "is-active")
+        item_elt = item_tpl.get_elt({
+            "item": preview_data,
+            "dates_format": "short",
+        })
+        tab_preview <= item_elt
+
+
+@bind(".click_to_edit", "click")
+def on_edit(evt):
+    tab_select(evt.target, "tab_edit", "is-active")
+
+
+@bind(document["blog_post_edit"], "submit")
+def on_submit(evt):
+    submit_btn = document.select_one("button[type='submit']")
+    submit_btn.classList.add("is-loading")
+
+
+for elt in document.select('.click_to_preview'):
+    elt.bind("click", lambda evt: aio.run(on_preview(evt)))
+
+
+editor.set_form_autosave("blog_post_edit")
+editor.TagsEditor("input[name='tags']")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/pages/blog/edit/page_meta.py	Thu Apr 29 16:55:07 2021 +0200
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+from libervia.server.constants import Const as C
+from sat.core.log import getLogger
+from sat.tools.common import data_format
+
+log = getLogger(__name__)
+
+name = "blog_edit"
+access = C.PAGES_ACCESS_PROFILE
+template = "blog/publish.html"
+
+
+async def on_data_post(self, request):
+    profile = self.getProfile(request)
+    if profile is None:
+        self.pageError(request, C.HTTP_FORBIDDEN)
+    request_data = self.getRData(request)
+    request_data["post_redirect_page"] = self.getPageByName("blog")
+    title, tags, body = self.getPostedData(request, ('title', 'tags', 'body'))
+    mb_data = {"content_rich": body}
+    title = title.strip()
+    if title:
+        mb_data["title_rich"] = title
+    tags = [t.strip() for t in tags.split(',') if t.strip()]
+    if tags:
+        mb_data["tags"] = tags
+
+    await self.host.bridgeCall(
+        'mbSend',
+        "",
+        "",
+        data_format.serialise(mb_data),
+        profile
+    )
--- a/libervia/pages/blog/page_meta.py	Thu Apr 29 16:45:39 2021 +0200
+++ b/libervia/pages/blog/page_meta.py	Thu Apr 29 16:55:07 2021 +0200
@@ -15,12 +15,11 @@
 template = "blog/discover.html"
 
 
-@defer.inlineCallbacks
-def prepare_render(self, request):
+async def prepare_render(self, request):
     profile = self.getProfile(request)
     template_data = request.template_data
     if profile is not None:
-        __, entities_own, entities_roster = yield self.host.bridgeCall(
+        __, entities_own, entities_roster = await self.host.bridgeCall(
             "discoFindByFeatures",
             [],
             [("pubsub", "pep")],
@@ -50,7 +49,7 @@
                         [],
                         True,
                         profile)
-        identities_data = yield defer.DeferredList(d_list.values())
+        identities_data = await defer.DeferredList(d_list.values())
         entities_idx = list(d_list.keys())
         for idx, (success, identity_raw) in enumerate(identities_data):
             entity_jid_s = entities_idx[idx]
@@ -60,6 +59,8 @@
             else:
                 identities[entity_jid_s] = data_format.deserialise(identity_raw)
 
+        template_data["url_blog_edit"] = self.getSubPageURL(request, "blog_edit")
+
 
 def on_data_post(self, request):
     jid_str = self.getPostedData(request, "jid")