# HG changeset patch # User Goffi # Date 1619708107 -7200 # Node ID 0554103ec7000eb6e71fd51b44f6dbaaa5fc303e # Parent 8415d882b68670781dd5e8a7f662ddae9cd215ac 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. diff -r 8415d882b686 -r 0554103ec700 libervia/pages/blog/edit/_browser/__init__.py --- /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']") diff -r 8415d882b686 -r 0554103ec700 libervia/pages/blog/edit/page_meta.py --- /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 + ) diff -r 8415d882b686 -r 0554103ec700 libervia/pages/blog/page_meta.py --- 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")