changeset 1179:bfbfe04209e9

pages (g): retrieve interests for events/photo albums/file sharing + only set "include_url" if "main_uri" is set
author Goffi <goffi@goffi.org>
date Sat, 04 May 2019 00:02:50 +0200
parents 92ca86e417e3
children cc16d93d4181
files libervia/pages/g/page_meta.py
diffstat 1 files changed, 74 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/g/page_meta.py	Sat May 04 00:01:08 2019 +0200
+++ b/libervia/pages/g/page_meta.py	Sat May 04 00:02:50 2019 +0200
@@ -39,7 +39,7 @@
         sat_session, guest_session = self.host.getSessionData(
             request, session_iface.ISATSession, session_iface.ISATGuestSession
         )
-        current_id = None  # FIXME: id non mis à zéro ici
+        current_id = None  # FIXME: id not reset here
         profile = None
 
     profile = sat_session.profile
@@ -96,9 +96,80 @@
     if u"language" in data:
         template_data[u"locale"] = data[u"language"]
 
+def handleEventInterest(self, interest):
+    if C.bool(interest.get("creator", C.BOOL_FALSE)):
+        page_name = u"event_admin"
+    else:
+        page_name = u"event_rsvp"
 
+    interest["url"] = self.getPageByName(page_name).getURL(
+        interest.get("service", ""),
+        interest.get("node", ""),
+        interest.get("item"),
+        )
+
+    if u"thumb_url" not in interest and u"image" in interest:
+        interest[u"thumb_url"] = interest[u"image"]
+
+def handleFISInterest(self, interest):
+    path = interest.get(u'path', u'')
+    path_args = [p for p in path.split(u'/') if p]
+    subtype = interest.get(u'subtype')
+
+    if subtype == u'files':
+        page_name = u"files_view"
+    elif interest.get(u'subtype') == u'photos':
+        page_name = u"photos_album"
+    else:
+        log.warning(u"unknown interest subtype: {subtype}".format(subtype=subtype))
+        return False
+
+    interest["url"] = self.getPageByName(page_name).getURL(
+        interest[u'service'], *path_args)
+
+@defer.inlineCallbacks
 def prepare_render(self, request):
     template_data = request.template_data
+    profile = self.getProfile(request)
+
+    # interests
+    try:
+        interests = yield self.host.bridgeCall(
+            "interestsList", "", "", "", profile)
+    except Exception:
+        log.warning(_(u"Can't get interests list for {profile}").format(
+            profile=profile))
+    else:
+        # we only want known interests (photos and events for now)
+        # this dict map namespaces of interest to a callback which can manipulate
+        # the data. If it returns False, the interest is skipped
+        ns_data = {}
+        template_data[u'interests_map'] = interests_map = {}
+
+        for short_name, cb in ((u'event', handleEventInterest),
+                               (u'fis', handleFISInterest),
+                              ):
+            try:
+                namespace = self.host.ns_map[short_name]
+            except KeyError:
+                pass
+            else:
+                ns_data[namespace] = (cb, short_name)
+
+        for interest in interests:
+            namespace = interest.get(u'namespace')
+            if namespace not in ns_data:
+                continue
+            cb, short_name = ns_data[namespace]
+            if cb(self, interest) == False:
+                continue
+            key = interest.get(u'subtype', short_name)
+            interests_map.setdefault(key, []).append(interest)
+
+    # main URI
     guest_session = self.host.getSessionData(request, session_iface.ISATGuestSession)
-    main_uri = guest_session.data.get("main_uri")
-    template_data[u"include_url"] = self.getPagePathFromURI(main_uri)
+    main_uri = guest_session.data.get("event_uri")
+    if main_uri:
+        include_url = self.getPagePathFromURI(main_uri)
+        if include_url is not None:
+            template_data[u"include_url"] = include_url