comparison libervia/web/pages/g/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/g/page_meta.py@106bae41f5c8
children 197350e8bf3b
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3
4 from libervia.web.server.constants import Const as C
5 from libervia.backend.core.i18n import _
6 from libervia.web.server import session_iface
7 from libervia.backend.core.log import getLogger
8
9 log = getLogger(__name__)
10
11 access = C.PAGES_ACCESS_PUBLIC
12 template = "invitation/welcome.html"
13
14
15 async def parse_url(self, request):
16 """check invitation id in URL and start session if needed
17
18 if a session already exists for an other guest/profile, it will be purged
19 """
20 try:
21 invitation_id = self.next_path(request)
22 except IndexError:
23 self.page_error(request)
24
25 web_session, guest_session = self.host.get_session_data(
26 request, session_iface.IWebSession, session_iface.IWebGuestSession
27 )
28 current_id = guest_session.id
29
30 if current_id is not None and current_id != invitation_id:
31 log.info(
32 _(
33 "killing guest session [{old_id}] because it is connecting with an other ID [{new_id}]"
34 ).format(old_id=current_id, new_id=invitation_id)
35 )
36 self.host.purge_session(request)
37 web_session, guest_session = self.host.get_session_data(
38 request, session_iface.IWebSession, session_iface.IWebGuestSession
39 )
40 current_id = None # FIXME: id not reset here
41 profile = None
42
43 profile = web_session.profile
44 if profile is not None and current_id is None:
45 log.info(
46 _(
47 "killing current profile session [{profile}] because a guest id is used"
48 ).format(profile=profile)
49 )
50 self.host.purge_session(request)
51 web_session, guest_session = self.host.get_session_data(
52 request, session_iface.IWebSession, session_iface.IWebGuestSession
53 )
54 profile = None
55
56 if current_id is None:
57 log.debug(_("checking invitation [{id}]").format(id=invitation_id))
58 try:
59 data = await self.host.bridge_call("invitation_get", invitation_id)
60 except Exception:
61 self.page_error(request, C.HTTP_FORBIDDEN)
62 else:
63 guest_session.id = invitation_id
64 guest_session.data = data
65 else:
66 data = guest_session.data
67
68 if profile is None:
69 log.debug(_("connecting profile [{}]").format(profile))
70 # we need to connect the profile
71 profile = data["guest_profile"]
72 password = data["password"]
73 try:
74 await self.host.connect(request, profile, password)
75 except Exception as e:
76 log.warning(_("Can't connect profile: {msg}").format(msg=e))
77 # FIXME: no good error code correspond
78 # maybe use a custom one?
79 self.page_error(request, code=C.HTTP_SERVICE_UNAVAILABLE)
80
81 log.info(
82 _(
83 "guest session started, connected with profile [{profile}]".format(
84 profile=profile
85 )
86 )
87 )
88
89 # we copy data useful in templates
90 template_data = request.template_data
91 template_data["norobots"] = True
92 if "name" in data:
93 template_data["name"] = data["name"]
94 if "language" in data:
95 template_data["locale"] = data["language"]
96
97 def handle_event_interest(self, interest):
98 if C.bool(interest.get("creator", C.BOOL_FALSE)):
99 page_name = "event_admin"
100 else:
101 page_name = "event_rsvp"
102
103 interest["url"] = self.get_page_by_name(page_name).get_url(
104 interest.get("service", ""),
105 interest.get("node", ""),
106 interest.get("item"),
107 )
108
109 if "thumb_url" not in interest and "image" in interest:
110 interest["thumb_url"] = interest["image"]
111
112 def handle_fis_interest(self, interest):
113 path = interest.get('path', '')
114 path_args = [p for p in path.split('/') if p]
115 subtype = interest.get('subtype')
116
117 if subtype == 'files':
118 page_name = "files_view"
119 elif interest.get('subtype') == 'photos':
120 page_name = "photos_album"
121 else:
122 log.warning("unknown interest subtype: {subtype}".format(subtype=subtype))
123 return False
124
125 interest["url"] = self.get_page_by_name(page_name).get_url(
126 interest['service'], *path_args)
127
128 async def prepare_render(self, request):
129 template_data = request.template_data
130 profile = self.get_profile(request)
131
132 # interests
133 template_data['interests_map'] = interests_map = {}
134 try:
135 interests = await self.host.bridge_call(
136 "interests_list", "", "", "", profile)
137 except Exception:
138 log.warning(_("Can't get interests list for {profile}").format(
139 profile=profile))
140 else:
141 # we only want known interests (photos and events for now)
142 # this dict map namespaces of interest to a callback which can manipulate
143 # the data. If it returns False, the interest is skipped
144 ns_data = {}
145
146 for short_name, cb in (('event', handle_event_interest),
147 ('fis', handle_fis_interest),
148 ):
149 try:
150 namespace = self.host.ns_map[short_name]
151 except KeyError:
152 pass
153 else:
154 ns_data[namespace] = (cb, short_name)
155
156 for interest in interests:
157 namespace = interest.get('namespace')
158 if namespace not in ns_data:
159 continue
160 cb, short_name = ns_data[namespace]
161 if cb(self, interest) == False:
162 continue
163 key = interest.get('subtype', short_name)
164 interests_map.setdefault(key, []).append(interest)
165
166 # main URI
167 guest_session = self.host.get_session_data(request, session_iface.IWebGuestSession)
168 main_uri = guest_session.data.get("event_uri")
169 if main_uri:
170 include_url = self.get_page_path_from_uri(main_uri)
171 if include_url is not None:
172 template_data["include_url"] = include_url