Mercurial > libervia-web
comparison libervia/pages/g/page_meta.py @ 1216:b2d067339de3
python 3 port:
/!\ Python 3.6+ is now needed to use libervia
/!\ instability may occur and features may not be working anymore, this will improve with time
/!\ TxJSONRPC dependency has been removed
The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs
for details). Removed now deprecated code (Pyjamas compiled browser part, legacy blog,
JSON RPC related code).
Adapted code to work without `html` and `themes` dirs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:12:31 +0200 |
parents | bfbfe04209e9 |
children | f511f8fbbf8a |
comparison
equal
deleted
inserted
replaced
1215:f14ab8a25e8b | 1216:b2d067339de3 |
---|---|
1 #!/usr/bin/env python2.7 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 from libervia.server.constants import Const as C | 4 from libervia.server.constants import Const as C |
5 from sat.core.i18n import _ | 5 from sat.core.i18n import _ |
6 from twisted.internet import defer | 6 from twisted.internet import defer |
8 from sat.core.log import getLogger | 8 from sat.core.log import getLogger |
9 | 9 |
10 log = getLogger(__name__) | 10 log = getLogger(__name__) |
11 | 11 |
12 access = C.PAGES_ACCESS_PUBLIC | 12 access = C.PAGES_ACCESS_PUBLIC |
13 template = u"invitation/welcome.html" | 13 template = "invitation/welcome.html" |
14 | 14 |
15 | 15 |
16 @defer.inlineCallbacks | 16 @defer.inlineCallbacks |
17 def parse_url(self, request): | 17 def parse_url(self, request): |
18 """check invitation id in URL and start session if needed | 18 """check invitation id in URL and start session if needed |
30 current_id = guest_session.id | 30 current_id = guest_session.id |
31 | 31 |
32 if current_id is not None and current_id != invitation_id: | 32 if current_id is not None and current_id != invitation_id: |
33 log.info( | 33 log.info( |
34 _( | 34 _( |
35 u"killing guest session [{old_id}] because it is connecting with an other ID [{new_id}]" | 35 "killing guest session [{old_id}] because it is connecting with an other ID [{new_id}]" |
36 ).format(old_id=current_id, new_id=invitation_id) | 36 ).format(old_id=current_id, new_id=invitation_id) |
37 ) | 37 ) |
38 self.host.purgeSession(request) | 38 self.host.purgeSession(request) |
39 sat_session, guest_session = self.host.getSessionData( | 39 sat_session, guest_session = self.host.getSessionData( |
40 request, session_iface.ISATSession, session_iface.ISATGuestSession | 40 request, session_iface.ISATSession, session_iface.ISATGuestSession |
44 | 44 |
45 profile = sat_session.profile | 45 profile = sat_session.profile |
46 if profile is not None and current_id is None: | 46 if profile is not None and current_id is None: |
47 log.info( | 47 log.info( |
48 _( | 48 _( |
49 u"killing current profile session [{profile}] because a guest id is used" | 49 "killing current profile session [{profile}] because a guest id is used" |
50 ).format(profile=profile) | 50 ).format(profile=profile) |
51 ) | 51 ) |
52 self.host.purgeSession(request) | 52 self.host.purgeSession(request) |
53 sat_session, guest_session = self.host.getSessionData( | 53 sat_session, guest_session = self.host.getSessionData( |
54 request, session_iface.ISATSession, session_iface.ISATGuestSession | 54 request, session_iface.ISATSession, session_iface.ISATGuestSession |
55 ) | 55 ) |
56 profile = None | 56 profile = None |
57 | 57 |
58 if current_id is None: | 58 if current_id is None: |
59 log.debug(_(u"checking invitation [{id}]").format(id=invitation_id)) | 59 log.debug(_("checking invitation [{id}]").format(id=invitation_id)) |
60 try: | 60 try: |
61 data = yield self.host.bridgeCall("invitationGet", invitation_id) | 61 data = yield self.host.bridgeCall("invitationGet", invitation_id) |
62 except Exception: | 62 except Exception: |
63 self.pageError(request, C.HTTP_FORBIDDEN) | 63 self.pageError(request, C.HTTP_FORBIDDEN) |
64 else: | 64 else: |
66 guest_session.data = data | 66 guest_session.data = data |
67 else: | 67 else: |
68 data = guest_session.data | 68 data = guest_session.data |
69 | 69 |
70 if profile is None: | 70 if profile is None: |
71 log.debug(_(u"connecting profile [{}]").format(profile)) | 71 log.debug(_("connecting profile [{}]").format(profile)) |
72 # we need to connect the profile | 72 # we need to connect the profile |
73 profile = data["guest_profile"] | 73 profile = data["guest_profile"] |
74 password = data["password"] | 74 password = data["password"] |
75 try: | 75 try: |
76 yield self.host.connect(request, profile, password) | 76 yield self.host.connect(request, profile, password) |
77 except Exception as e: | 77 except Exception as e: |
78 log.warning(_(u"Can't connect profile: {msg}").format(msg=e)) | 78 log.warning(_("Can't connect profile: {msg}").format(msg=e)) |
79 # FIXME: no good error code correspond | 79 # FIXME: no good error code correspond |
80 # maybe use a custom one? | 80 # maybe use a custom one? |
81 self.pageError(request, code=C.HTTP_SERVICE_UNAVAILABLE) | 81 self.pageError(request, code=C.HTTP_SERVICE_UNAVAILABLE) |
82 | 82 |
83 log.info( | 83 log.info( |
84 _( | 84 _( |
85 u"guest session started, connected with profile [{profile}]".format( | 85 "guest session started, connected with profile [{profile}]".format( |
86 profile=profile | 86 profile=profile |
87 ) | 87 ) |
88 ) | 88 ) |
89 ) | 89 ) |
90 | 90 |
91 # we copy data useful in templates | 91 # we copy data useful in templates |
92 template_data = request.template_data | 92 template_data = request.template_data |
93 template_data["norobots"] = True | 93 template_data["norobots"] = True |
94 if u"name" in data: | 94 if "name" in data: |
95 template_data[u"name"] = data[u"name"] | 95 template_data["name"] = data["name"] |
96 if u"language" in data: | 96 if "language" in data: |
97 template_data[u"locale"] = data[u"language"] | 97 template_data["locale"] = data["language"] |
98 | 98 |
99 def handleEventInterest(self, interest): | 99 def handleEventInterest(self, interest): |
100 if C.bool(interest.get("creator", C.BOOL_FALSE)): | 100 if C.bool(interest.get("creator", C.BOOL_FALSE)): |
101 page_name = u"event_admin" | 101 page_name = "event_admin" |
102 else: | 102 else: |
103 page_name = u"event_rsvp" | 103 page_name = "event_rsvp" |
104 | 104 |
105 interest["url"] = self.getPageByName(page_name).getURL( | 105 interest["url"] = self.getPageByName(page_name).getURL( |
106 interest.get("service", ""), | 106 interest.get("service", ""), |
107 interest.get("node", ""), | 107 interest.get("node", ""), |
108 interest.get("item"), | 108 interest.get("item"), |
109 ) | 109 ) |
110 | 110 |
111 if u"thumb_url" not in interest and u"image" in interest: | 111 if "thumb_url" not in interest and "image" in interest: |
112 interest[u"thumb_url"] = interest[u"image"] | 112 interest["thumb_url"] = interest["image"] |
113 | 113 |
114 def handleFISInterest(self, interest): | 114 def handleFISInterest(self, interest): |
115 path = interest.get(u'path', u'') | 115 path = interest.get('path', '') |
116 path_args = [p for p in path.split(u'/') if p] | 116 path_args = [p for p in path.split('/') if p] |
117 subtype = interest.get(u'subtype') | 117 subtype = interest.get('subtype') |
118 | 118 |
119 if subtype == u'files': | 119 if subtype == 'files': |
120 page_name = u"files_view" | 120 page_name = "files_view" |
121 elif interest.get(u'subtype') == u'photos': | 121 elif interest.get('subtype') == 'photos': |
122 page_name = u"photos_album" | 122 page_name = "photos_album" |
123 else: | 123 else: |
124 log.warning(u"unknown interest subtype: {subtype}".format(subtype=subtype)) | 124 log.warning("unknown interest subtype: {subtype}".format(subtype=subtype)) |
125 return False | 125 return False |
126 | 126 |
127 interest["url"] = self.getPageByName(page_name).getURL( | 127 interest["url"] = self.getPageByName(page_name).getURL( |
128 interest[u'service'], *path_args) | 128 interest['service'], *path_args) |
129 | 129 |
130 @defer.inlineCallbacks | 130 @defer.inlineCallbacks |
131 def prepare_render(self, request): | 131 def prepare_render(self, request): |
132 template_data = request.template_data | 132 template_data = request.template_data |
133 profile = self.getProfile(request) | 133 profile = self.getProfile(request) |
135 # interests | 135 # interests |
136 try: | 136 try: |
137 interests = yield self.host.bridgeCall( | 137 interests = yield self.host.bridgeCall( |
138 "interestsList", "", "", "", profile) | 138 "interestsList", "", "", "", profile) |
139 except Exception: | 139 except Exception: |
140 log.warning(_(u"Can't get interests list for {profile}").format( | 140 log.warning(_("Can't get interests list for {profile}").format( |
141 profile=profile)) | 141 profile=profile)) |
142 else: | 142 else: |
143 # we only want known interests (photos and events for now) | 143 # we only want known interests (photos and events for now) |
144 # this dict map namespaces of interest to a callback which can manipulate | 144 # this dict map namespaces of interest to a callback which can manipulate |
145 # the data. If it returns False, the interest is skipped | 145 # the data. If it returns False, the interest is skipped |
146 ns_data = {} | 146 ns_data = {} |
147 template_data[u'interests_map'] = interests_map = {} | 147 template_data['interests_map'] = interests_map = {} |
148 | 148 |
149 for short_name, cb in ((u'event', handleEventInterest), | 149 for short_name, cb in (('event', handleEventInterest), |
150 (u'fis', handleFISInterest), | 150 ('fis', handleFISInterest), |
151 ): | 151 ): |
152 try: | 152 try: |
153 namespace = self.host.ns_map[short_name] | 153 namespace = self.host.ns_map[short_name] |
154 except KeyError: | 154 except KeyError: |
155 pass | 155 pass |
156 else: | 156 else: |
157 ns_data[namespace] = (cb, short_name) | 157 ns_data[namespace] = (cb, short_name) |
158 | 158 |
159 for interest in interests: | 159 for interest in interests: |
160 namespace = interest.get(u'namespace') | 160 namespace = interest.get('namespace') |
161 if namespace not in ns_data: | 161 if namespace not in ns_data: |
162 continue | 162 continue |
163 cb, short_name = ns_data[namespace] | 163 cb, short_name = ns_data[namespace] |
164 if cb(self, interest) == False: | 164 if cb(self, interest) == False: |
165 continue | 165 continue |
166 key = interest.get(u'subtype', short_name) | 166 key = interest.get('subtype', short_name) |
167 interests_map.setdefault(key, []).append(interest) | 167 interests_map.setdefault(key, []).append(interest) |
168 | 168 |
169 # main URI | 169 # main URI |
170 guest_session = self.host.getSessionData(request, session_iface.ISATGuestSession) | 170 guest_session = self.host.getSessionData(request, session_iface.ISATGuestSession) |
171 main_uri = guest_session.data.get("event_uri") | 171 main_uri = guest_session.data.get("event_uri") |
172 if main_uri: | 172 if main_uri: |
173 include_url = self.getPagePathFromURI(main_uri) | 173 include_url = self.getPagePathFromURI(main_uri) |
174 if include_url is not None: | 174 if include_url is not None: |
175 template_data[u"include_url"] = include_url | 175 template_data["include_url"] = include_url |