comparison libervia/pages/photos/album/_browser/__init__.py @ 1331:fe353fceec38

browser (invitation, photos/album): invitation manager improvments: invitation manager has been moved to a separated module, it is generic so it can be used with other activities. It has been simplified, and contact to add are dynamically filtered using a text input. Invitation are done by email using a the new modal. New notification module is used to indicate when invitation has been done.
author Goffi <goffi@goffi.org>
date Fri, 14 Aug 2020 09:31:32 +0200
parents b525fdcb393b
children a75d82713a02
comparison
equal deleted inserted replaced
1330:b525fdcb393b 1331:fe353fceec38
1 from browser import document, window, bind, html, DOMNode, timer 1 from browser import document, window, bind, html, DOMNode
2 from javascript import JSON 2 from javascript import JSON
3 from bridge import Bridge 3 from bridge import Bridge
4 from template import Template 4 from template import Template
5 import dialog 5 import dialog
6 from slideshow import SlideShow 6 from slideshow import SlideShow
7 from invitation import InvitationManager
7 8
9 cache_path = window.cache_path
8 files_service = window.files_service 10 files_service = window.files_service
9 files_path = window.files_path 11 files_path = window.files_path
12 try:
13 affiliations = window.affiliations.to_dict()
14 except AttributeError:
15 pass
10 bridge = Bridge() 16 bridge = Bridge()
11 17
12 # file upload 18 # file upload
13 19
14 def on_progress(ev, photo_elt): 20 def on_progress(ev, photo_elt):
175 for elt in document.select('.action_delete'): 181 for elt in document.select('.action_delete'):
176 elt.bind("click", on_delete) 182 elt.bind("click", on_delete)
177 183
178 # manage 184 # manage
179 185
180 def on_manager_close(manager_panel_elt):
181 side_panel = manager_panel_elt.select_one('.invitation_manager_side_panel')
182 side_panel.classList.remove('open')
183 side_panel.bind("transitionend", lambda evt: manager_panel_elt.remove())
184
185 def _on_invitation_cb(field_elt, entity):
186 print(f"invitation for {entity!r} sent successfully")
187 submit_elt = document['invitation_submit']
188 submit_elt.disabled = False
189 form_elt = document['invitation_form']
190 form_elt.disabled = False
191 jids_elt = form_elt.select_one('*[name="jids"]')
192 emails_elt = form_elt.select_one('*[name="emails"]')
193 new = [d.strip() for d in field_elt.value.split('\n') if d and d.strip() != entity]
194 field_elt.value = '\n'.join(new)
195 if not jids_elt.value.strip() and not emails_elt.value.strip():
196 # FIXME: Q&D notification, needs to do this properly in a separated module with
197 # some animations
198 notifs_elt = document['invitation_notifications']
199 notification_elt = html.DIV(Class="notification is-success has-text-centered")
200 notification_elt <= "invitations sent successfully"
201 notifs_elt <= notification_elt
202 timer.set_timeout(lambda: notification_elt.remove(), 5000)
203
204
205 def invitationSimpleCreateCb(invitation_data, email):
206 invitee_jid = invitation_data['jid']
207 album_name = files_path.rsplit('/')[-1]
208 form_elt = document['invitation_form']
209 emails_elt = form_elt.select_one('*[name="emails"]')
210 bridge.FISInvite(
211 invitee_jid,
212 files_service,
213 "photos",
214 "",
215 files_path,
216 album_name,
217 '',
218 callback=lambda: _on_invitation_cb(emails_elt, email),
219 errback=lambda e: window.alert(f"invitation failed for {email}: {e}")
220 )
221
222
223 def invite_by_email(email):
224 guest_url_tpl = f'{window.URL.new("/g", document.baseURI).href}/{{uuid}}'
225 bridge.invitationSimpleCreate(
226 email,
227 guest_url_tpl,
228 '',
229 callback=lambda data: invitationSimpleCreateCb(data, email),
230 errback=lambda e: window.alert(f"can't send email invitation: {e}")
231 )
232
233
234 def on_invitation_submit(evt, manager_panel_elt):
235 evt.stopPropagation()
236 evt.preventDefault()
237 submit_elt = document['invitation_submit']
238 submit_elt.disabled = True
239 form_elt = document['invitation_form']
240 form_elt.disabled = True
241 jids_elt = form_elt.select_one('*[name="jids"]')
242 emails_elt = form_elt.select_one('*[name="emails"]')
243 jids = [j.strip() for j in jids_elt.value.split('\n') if j.strip()]
244 emails = [e.strip() for e in emails_elt.value.split('\n') if e.strip()]
245 album_name = files_path.rsplit('/')[-1]
246 for entity_jid in jids:
247 print(f"inviting {entity_jid}")
248 bridge.FISInvite(
249 entity_jid,
250 files_service,
251 "photos",
252 "",
253 files_path,
254 album_name,
255 '',
256 callback=lambda entity=entity_jid: _on_invitation_cb(jids_elt, entity),
257 errback=lambda e: window.alert(f"invitation failed: {e}")
258 )
259
260 for email in emails:
261 invite_by_email(email)
262
263 print(f"{jids=}, {emails=}")
264
265
266 def _FISAffiliationSetCb(affiliation_elt):
267 affiliation_elt.remove()
268
269
270 def on_affiliation_remove(entity_jid, affiliation_elt):
271 bridge.FISAffiliationsSet(
272 files_service,
273 "",
274 files_path,
275 {entity_jid: "none"},
276 callback=lambda: _FISAffiliationSetCb(affiliation_elt),
277 errback=lambda e: window.alert(f"can't remove affiliation: {e}")
278 )
279
280 186
281 @bind("#button_manage", "click") 187 @bind("#button_manage", "click")
282 def manage_click(evt): 188 def manage_click(evt):
283 evt.stopPropagation() 189 evt.stopPropagation()
284 evt.preventDefault() 190 evt.preventDefault()
285 manager_panel_tpl = Template('invitation/manager.html') 191 manager = InvitationManager("photos", {"service": files_service, "path": files_path})
286 manager_panel_elt = manager_panel_tpl.get_elt() 192 manager.attach(affiliations=affiliations)
287 document.body <= manager_panel_elt
288 document['invitation_submit'].bind(
289 "click", lambda evt: on_invitation_submit(evt, manager_panel_elt))
290 side_panel = manager_panel_elt.select_one('.invitation_manager_side_panel')
291 timer.set_timeout(lambda: side_panel.classList.add("open"), 0)
292 for close_elt in manager_panel_elt.select('.click_to_close'):
293 close_elt.bind("click", lambda evt: on_manager_close(manager_panel_elt))
294 side_panel.bind("click", lambda evt: evt.stopPropagation())
295 affiliations = window.affiliations.to_dict()
296 affiliation_tpl = Template('invitation/affiliation_item.html')
297 for entity_jid, affiliation in affiliations.items():
298 affiliation_elt = affiliation_tpl.get_elt({
299 "entity_jid": entity_jid,
300 "affiliation": affiliation,
301 })
302 document['affiliations'] <= affiliation_elt
303 for elt in affiliation_elt.select(".click_to_delete"):
304 elt.bind(
305 "click",
306 lambda evt,
307 entity_jid=entity_jid,
308 affiliation_elt=affiliation_elt: on_affiliation_remove(
309 entity_jid,
310 affiliation_elt
311 )
312 )