view libervia/web/pages/events/view/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/events/view/page_meta.py@106bae41f5c8
children
line wrap: on
line source

#!/usr/bin/env python3


from libervia.web.server.constants import Const as C
from libervia.backend.core.i18n import _
from twisted.internet import defer
from twisted.words.protocols.jabber import jid
from libervia.web.server import session_iface
from libervia.backend.tools.common import uri
from libervia.backend.tools.common.template import safe
import time
import html
from libervia.backend.core.log import getLogger

name = "event_view"
access = C.PAGES_ACCESS_PROFILE
template = "event/invitation.html"
log = getLogger(__name__)


@defer.inlineCallbacks
def prepare_render(self, request):
    template_data = request.template_data
    guest_session = self.host.get_session_data(request, session_iface.IWebGuestSession)
    try:
        event_uri = guest_session.data["event_uri"]
    except KeyError:
        log.warning(_("event URI not found, can't render event page"))
        self.page_error(request, C.HTTP_SERVICE_UNAVAILABLE)

    data = self.get_r_data(request)

    ## Event ##

    event_uri_data = uri.parse_xmpp_uri(event_uri)
    if event_uri_data["type"] != "pubsub":
        self.page_error(request, C.HTTP_SERVICE_UNAVAILABLE)

    event_service = template_data["event_service"] = jid.JID(event_uri_data["path"])
    event_node = template_data["event_node"] = event_uri_data["node"]
    event_id = template_data["event_id"] = event_uri_data.get("item", "")
    profile = self.get_profile(request)
    event_timestamp, event_data = yield self.host.bridge_call(
        "eventGet", event_service.userhost(), event_node, event_id, profile
    )
    try:
        background_image = event_data.pop("background-image")
    except KeyError:
        pass
    else:
        template_data["dynamic_style"] = safe(
            """
            html {
                background-image: url("%s");
                background-size: 15em;
            }
            """
            % html.escape(background_image, True)
        )
    template_data["event"] = event_data
    event_invitee_data = yield self.host.bridge_call(
        "event_invitee_get",
        event_data["invitees_service"],
        event_data["invitees_node"],
        '',
        profile,
    )
    template_data["invitee"] = event_invitee_data
    template_data["days_left"] = int((event_timestamp - time.time()) / (60 * 60 * 24))

    ## Blog ##

    data["service"] = jid.JID(event_data["blog_service"])
    data["node"] = event_data["blog_node"]
    data["allow_commenting"] = "simple"

    # we now need blog items, using blog common page
    # this will fill the "items" template data
    blog_page = self.get_page_by_name("blog_view")
    yield blog_page.prepare_render(self, request)


@defer.inlineCallbacks
def on_data_post(self, request):
    type_ = self.get_posted_data(request, "type")
    if type_ == "comment":
        blog_page = self.get_page_by_name("blog_view")
        yield blog_page.on_data_post(self, request)
    elif type_ == "attendance":
        profile = self.get_profile(request)
        service, node, attend, guests = self.get_posted_data(
            request, ("service", "node", "attend", "guests")
        )
        data = {"attend": attend, "guests": guests}
        yield self.host.bridge_call("event_invitee_set", service, node, data, profile)
    else:
        log.warning(_("Unhandled data type: {}").format(type_))