changeset 1111:4d1c4bd4931a

pages (events): added root, admin, new, rsvp and view pages
author Goffi <goffi@goffi.org>
date Thu, 21 Jun 2018 01:19:57 +0200
parents 045e8bdaed4f
children f287fc8bb31a
files src/pages/events/admin/page_meta.py src/pages/events/new/page_meta.py src/pages/events/page_meta.py src/pages/events/rsvp/page_meta.py src/pages/events/view/page_meta.py src/server/constants.py
diffstat 6 files changed, 406 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pages/events/admin/page_meta.py	Thu Jun 21 01:19:57 2018 +0200
@@ -0,0 +1,147 @@
+#!/usr/bin/env python2.7
+# -*- coding: utf-8 -*-
+
+from libervia.server.constants import Const as C
+from twisted.internet import defer
+from twisted.words.protocols.jabber import jid
+from sat.tools.common.template import safe
+from sat.core.i18n import _
+from sat.core.log import getLogger
+import time
+import cgi
+import math
+import re
+
+name = u'event_admin'
+access = C.PAGES_ACCESS_PROFILE
+template = u"event/admin.html"
+log = getLogger(u'pages/' + name)
+REG_EMAIL_RE = re.compile(C.REG_EMAIL_RE, re.IGNORECASE)
+
+
+def parse_url(self, request):
+    self.getPathArgs(request, ("event_service", "event_node", "event_id"), min_args=2, event_service="@jid", event_id='')
+
+@defer.inlineCallbacks
+def prepare_render(self, request):
+    data = self.getRData(request)
+    template_data = request.template_data
+
+    ## Event ##
+
+    event_service = template_data[u'event_service'] = data['event_service']
+    event_node = template_data[u'event_node'] = data['event_node']
+    event_id = template_data[u'event_id'] = data['event_id']
+    profile = self.getProfile(request)
+    event_timestamp, event_data = yield self.host.bridgeCall(u"eventGet", event_service.userhost() if event_service else '', event_node, event_id, profile)
+    try:
+        background_image = event_data.pop('background-image')
+    except KeyError:
+        pass
+    else:
+        template_data['dynamic_style'] = safe(u"""
+            html {
+                background-image: url("%s");
+                background-size: 15em;
+            }
+            """ % cgi.escape(background_image, True))
+    template_data['event'] = event_data
+    invitees = yield self.host.bridgeCall(
+        u"eventInviteesList",
+        event_data['invitees_service'],
+        event_data['invitees_node'],
+        profile)
+    template_data['invitees'] = invitees
+    invitees_guests = 0
+    for invitee_data in invitees.itervalues():
+        if invitee_data.get('attend', 'no') == 'no':
+            continue
+        try:
+            invitees_guests += int(invitee_data.get('guests', 0))
+        except ValueError:
+            log.warning(_(u"guests value is not valid: {invitee}").format(invitee=invitee_data))
+    template_data['invitees_guests'] = invitees_guests
+    template_data['days_left'] = int(math.ceil((event_timestamp - time.time()) / (60 * 60 * 24)))
+
+    ## Blog ##
+
+    data[u'service'] = jid.JID(event_data[u'blog_service'])
+    data[u'node'] = event_data[u'blog_node']
+    data[u'allow_commenting'] = u'simple'
+
+    # we now need blog items, using blog common page
+    # this will fill the "items" template data
+    blog_page = self.getPageByName(u'blog_view')
+    yield blog_page.prepare_render(self, request)
+
+@defer.inlineCallbacks
+def on_data_post(self, request):
+    profile = self.getProfile(request)
+    if not profile:
+        log.error(u"got post data without profile")
+        self.pageError(request, C.HTTP_INTERNAL_ERROR)
+    type_ = self.getPostedData(request, 'type')
+    if type_ == 'blog':
+        service, node, title, body, lang = self.getPostedData(
+            request, (u'service', u'node', u'title',  u'body', u'language'))
+
+        if not body.strip():
+            self.pageError(request, C.HTTP_BAD_REQUEST)
+        data = {u"content": body}
+        if title:
+            data[u'title'] = title
+        if lang:
+            data[u'language'] = lang
+        try:
+            comments = bool(self.getPostedData(request, u'comments').strip())
+        except KeyError:
+            pass
+        else:
+            if comments:
+                data[u'allow_comments'] = C.BOOL_TRUE
+
+        try:
+            yield self.host.bridgeCall(u'mbSend', service, node, data, profile)
+        except Exception as e:
+            if u"forbidden" in unicode(e):
+                self.pageError(request, C.HTTP_UNAUTHORIZED)
+            else:
+                raise e
+    elif type_ == 'event':
+        service, node, event_id, jids, emails = self.getPostedData(request, (u'service',
+                                                                             u'node',
+                                                                             u'event_id',
+                                                                             u'jids',
+                                                                             u'emails'))
+        for invitee_jid_s in jids.split():
+            try:
+                invitee_jid = jid.JID(invitee_jid_s)
+            except RuntimeError as e:
+                log.warning(_(u"this is not a valid jid: {jid}").format(jid=invitee_jid_s))
+                continue
+            yield self.host.bridgeCall('eventInvite', invitee_jid.userhost(),
+                                                      service,
+                                                      node,
+                                                      event_id,
+                                                      profile)
+        for email_addr in emails.split():
+            if not REG_EMAIL_RE.match(email_addr):
+                log.warning(_(u"this is not a valid email address: {email}").format(email=email_addr))
+                continue
+            yield self.host.bridgeCall('eventInviteByEmail',
+                                                      service,
+                                                      node,
+                                                      event_id,
+                                                      email_addr,
+                                                      {},
+                                                      u'',
+                                                      u'',
+                                                      u'',
+                                                      u'',
+                                                      u'',
+                                                      u'',
+                                                      profile)
+
+
+    else:
+        log.warning(_(u"Unhandled data type: {}").format(type_))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pages/events/new/page_meta.py	Thu Jun 21 01:19:57 2018 +0200
@@ -0,0 +1,42 @@
+#!/usr/bin/env python2.7
+# -*- coding: utf-8 -*-
+
+from libervia.server.constants import Const as C
+from twisted.internet import defer
+from sat.core.log import getLogger
+from sat.tools.common import date_utils
+"""creation of new events"""
+
+name = u'event_new'
+access = C.PAGES_ACCESS_PROFILE
+template = u"event/create.html"
+log = getLogger(u'pages/' + name)
+
+@defer.inlineCallbacks
+def on_data_post(self, request):
+    request_data = self.getRData(request)
+    profile = self.getProfile(request)
+    title, location, body, date, main_img, bg_img = self.getPostedData(
+        request, ('name', 'location', 'body', 'date', 'main_image', 'bg_image'))
+    timestamp = date_utils.date_parse(date)
+    data = {'name': title,
+            'description': body,
+            'location': location,
+            }
+
+    for value, var in ((main_img, 'image'),
+                       (bg_img, 'background-image')):
+        value = value.strip()
+        if not value:
+            continue
+        if not value.startswith('http'):
+            self.pageError(request, C.HTTP_BAD_REQUEST)
+        data[var] = value
+    data[u'register'] = C.BOOL_TRUE
+    node = yield self.host.bridgeCall('eventCreate', timestamp, data, '', '', '', profile)
+    log.info(u"Event node created at {node}".format(node=node))
+
+    request_data['post_redirect_page'] = (self.getPageByName(u'event_admin'),
+                                          '@',
+                                          node)
+    defer.returnValue(C.POST_NO_CONFIRM)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pages/events/page_meta.py	Thu Jun 21 01:19:57 2018 +0200
@@ -0,0 +1,45 @@
+#!/usr/bin/env python2.7
+# -*- coding: utf-8 -*-
+
+from libervia.server.constants import Const as C
+from twisted.internet import defer
+from sat.core.i18n import _
+from sat.core.log import getLogger
+log = getLogger('pages/ticket')
+"""ticket handling pages"""
+
+name = u'events'
+access = C.PAGES_ACCESS_PUBLIC
+template = u"event/overview.html"
+
+
+@defer.inlineCallbacks
+def parse_url(self, request):
+    profile = self.getProfile(request)
+    template_data = request.template_data
+    template_data[u'url_event_new'] = self.getSubPageURL(request, 'event_new')
+    if profile is not None:
+        try:
+            events = yield self.host.bridgeCall("eventsList", "", "", profile)
+        except Exception:
+            log.warning(_(u"Can't get events list for {profile}").format(profile=profile))
+        else:
+            own_events = []
+            other_events = []
+            for event in events:
+                if C.bool(event.get('creator', C.BOOL_FALSE)):
+                    own_events.append(event)
+                    event['url'] = self.getSubPageURL(request,
+                                                      u'event_admin',
+                                                      event.get('service',''),
+                                                      event.get('node',''),
+                                                      event.get('item'))
+                else:
+                    other_events.append(event)
+                    event['url'] = self.getSubPageURL(request,
+                                                      u'event_rsvp',
+                                                      event.get('service',''),
+                                                      event.get('node',''),
+                                                      event.get('item'))
+
+            template_data[u'events'] = own_events + other_events
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pages/events/rsvp/page_meta.py	Thu Jun 21 01:19:57 2018 +0200
@@ -0,0 +1,83 @@
+#!/usr/bin/env python2.7
+# -*- coding: utf-8 -*-
+
+from libervia.server.constants import Const as C
+from sat.core.i18n import _
+from twisted.internet import defer
+from twisted.words.protocols.jabber import jid
+from sat.core.log import getLogger
+from sat.tools.common.template import safe
+import time
+import cgi
+"""creation of new events"""
+
+name = u'event_rsvp'
+access = C.PAGES_ACCESS_PROFILE
+template = u"event/invitation.html"
+log = getLogger(u'pages/' + name)
+
+
+def parse_url(self, request):
+    self.getPathArgs(request, ("event_service", "event_node", "event_id"), min_args=2, event_service="@jid", event_id='')
+
+@defer.inlineCallbacks
+def prepare_render(self, request):
+    template_data = request.template_data
+    data = self.getRData(request)
+    profile = self.getProfile(request)
+
+    ## Event ##
+
+    event_service = data['event_service']
+    event_node = data[u'event_node']
+    event_id = data[u'event_id']
+    event_timestamp, event_data = yield self.host.bridgeCall(u"eventGet",
+                                                             event_service.userhost() if event_service else '',
+                                                             event_node,
+                                                             event_id,
+                                                             profile)
+    try:
+        background_image = event_data.pop('background-image')
+    except KeyError:
+        pass
+    else:
+        template_data['dynamic_style'] = safe(u"""
+            html {
+                background-image: url("%s");
+                background-size: 15em;
+            }
+            """ % cgi.escape(background_image, True))
+    template_data['event'] = event_data
+    event_invitee_data = yield self.host.bridgeCall(
+        u"eventInviteeGet",
+        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[u'service'] = jid.JID(event_data[u'blog_service'])
+    data[u'node'] = event_data[u'blog_node']
+    data[u'allow_commenting'] = u'simple'
+
+    # we now need blog items, using blog common page
+    # this will fill the "items" template data
+    blog_page = self.getPageByName(u'blog_view')
+    yield blog_page.prepare_render(self, request)
+
+@defer.inlineCallbacks
+def on_data_post(self, request):
+    type_ = self.getPostedData(request, u'type')
+    if type_ == u'comment':
+        blog_page = self.getPageByName(u'blog_view')
+        yield blog_page.on_data_post(self, request)
+    elif type_ == u'attendance':
+        profile = self.getProfile(request)
+        service, node, attend, guests = self.getPostedData(request, (u'service', u'node', u'attend', u'guests'))
+        data = {u'attend': attend,
+                u'guests': guests}
+        yield self.host.bridgeCall(u"eventInviteeSet", service, node, data, profile)
+    else:
+        log.warning(_(u"Unhandled data type: {}").format(type_))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pages/events/view/page_meta.py	Thu Jun 21 01:19:57 2018 +0200
@@ -0,0 +1,88 @@
+#!/usr/bin/env python2.7
+# -*- coding: utf-8 -*-
+
+from libervia.server.constants import Const as C
+from sat.core.i18n import _
+from twisted.internet import defer
+from twisted.words.protocols.jabber import jid
+from libervia.server import session_iface
+from sat.tools.common import uri
+from sat.tools.common.template import safe
+import time
+import cgi
+from sat.core.log import getLogger
+
+name = u'event_view'
+access = C.PAGES_ACCESS_PROFILE
+template = u"event/invitation.html"
+log = getLogger(u'pages/' + name)
+
+
+@defer.inlineCallbacks
+def prepare_render(self, request):
+    template_data = request.template_data
+    guest_session = self.host.getSessionData(request, session_iface.ISATGuestSession)
+    try:
+        event_uri = guest_session.data['event_uri']
+    except KeyError:
+        log.warning(_(u"event URI not found, can't render event page"))
+        self.pageError(request, C.HTTP_SERVICE_UNAVAILABLE)
+
+    data = self.getRData(request)
+
+    ## Event ##
+
+    event_uri_data = uri.parseXMPPUri(event_uri)
+    if event_uri_data[u'type'] != u'pubsub':
+        self.pageError(request, C.HTTP_SERVICE_UNAVAILABLE)
+
+    event_service = template_data[u'event_service'] = jid.JID(event_uri_data[u'path'])
+    event_node = template_data[u'event_node'] = event_uri_data[u'node']
+    event_id = template_data[u'event_id'] = event_uri_data.get(u'item','')
+    profile = self.getProfile(request)
+    event_timestamp, event_data = yield self.host.bridgeCall(u"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(u"""
+            html {
+                background-image: url("%s");
+                background-size: 15em;
+            }
+            """ % cgi.escape(background_image, True))
+    template_data['event'] = event_data
+    event_invitee_data = yield self.host.bridgeCall(
+        u"eventInviteeGet",
+        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[u'service'] = jid.JID(event_data[u'blog_service'])
+    data[u'node'] = event_data[u'blog_node']
+    data[u'allow_commenting'] = u'simple'
+
+    # we now need blog items, using blog common page
+    # this will fill the "items" template data
+    blog_page = self.getPageByName(u'blog_view')
+    yield blog_page.prepare_render(self, request)
+
+@defer.inlineCallbacks
+def on_data_post(self, request):
+    type_ = self.getPostedData(request, u'type')
+    if type_ == u'comment':
+        blog_page = self.getPageByName(u'blog_view')
+        yield blog_page.on_data_post(self, request)
+    elif type_ == u'attendance':
+        profile = self.getProfile(request)
+        service, node, attend, guests = self.getPostedData(request, (u'service', u'node', u'attend', u'guests'))
+        data = {u'attend': attend,
+                u'guests': guests}
+        yield self.host.bridgeCall(u"eventInviteeSet", service, node, data, profile)
+    else:
+        log.warning(_(u"Unhandled data type: {}").format(type_))
--- a/src/server/constants.py	Thu Jun 21 01:19:47 2018 +0200
+++ b/src/server/constants.py	Thu Jun 21 01:19:57 2018 +0200
@@ -58,7 +58,7 @@
     PAGES_ACCESS_ADMIN = u"admin"  # only profiles set in admins_list can access the page
     PAGES_ACCESS_ALL = (PAGES_ACCESS_NONE, PAGES_ACCESS_PUBLIC, PAGES_ACCESS_PROFILE, PAGES_ACCESS_ADMIN)
     # names of the page to use for menu
-    DEFAULT_MENU = ['login', 'chat', 'blog', 'forums', 'photos', 'files', 'tickets', 'merge-requests', 'app']
+    DEFAULT_MENU = ['login', 'chat', 'blog', 'forums', 'photos', 'files', 'events', 'tickets', 'merge-requests', 'app']
 
     ## Session flags ##
     FLAG_CONFIRM = u"CONFIRM"