annotate libervia/pages/lists/view/_browser/__init__.py @ 1447:907f519faaf0

pages: pubsub's `extra` is now serialised, following backend change
author Goffi <goffi@goffi.org>
date Thu, 29 Jul 2021 23:01:29 +0200
parents 0f3038f3a954
children 106bae41f5c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1395
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
1 from browser import window, document, aio, bind
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 from invitation import InvitationManager
1395
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
3 from javascript import JSON
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
4 from aio_bridge import Bridge, BridgeException
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
5 import dialog
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
7
1395
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
8 bridge = Bridge()
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 lists_ns = window.lists_ns
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 pubsub_service = window.pubsub_service
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 pubsub_node = window.pubsub_node
1395
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
12 list_type = window.list_type
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 try:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 affiliations = window.affiliations.to_dict()
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 except AttributeError:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 pass
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 @bind("#button_manage", "click")
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 def manage_click(evt):
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 evt.stopPropagation()
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 evt.preventDefault()
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 pubsub_data = {
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 "namespace": lists_ns,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 "service": pubsub_service,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 "node": pubsub_node
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 }
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 try:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 name = pubsub_node.split('_', 1)[1]
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 except IndexError:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 pass
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 else:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 name = name.strip()
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 if name:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 pubsub_data['name'] = name
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 manager = InvitationManager("pubsub", pubsub_data)
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 manager.attach(affiliations=affiliations)
1395
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
37
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
38
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
39 async def on_delete(evt):
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
40 item_elt = evt.target.closest(".item")
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
41 if item_elt is None:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
42 dialog.notification.show(
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
43 "Can't find parent item element",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
44 level="error"
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
45 )
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
46 return
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
47 item_elt.classList.add("selected_for_deletion")
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
48 item = JSON.parse(item_elt.dataset.item)
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
49 confirmed = await dialog.Confirm(
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
50 f"{item['name']!r} will be deleted, are you sure?",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
51 ok_label="delete",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
52 ok_color="danger",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
53 ).ashow()
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
54 item_elt.classList.remove("selected_for_deletion")
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
55 if confirmed:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
56 try:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
57 await bridge.psItemRetract(pubsub_service, pubsub_node, item["id"], True)
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
58 except Exception as e:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
59 dialog.notification.show(
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
60 f"Can't delete list item: {e}",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
61 level="error"
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
62 )
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
63 else:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
64 dialog.notification.show("list item deleted successfuly")
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
65 item_elt.remove()
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
66
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
67
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
68 async def on_next_state(evt):
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
69 """Update item with next state
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
70
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
71 Only used with grocery list at the moment
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
72 """
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
73 evt.stopPropagation()
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
74 evt.preventDefault()
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
75 # FIXME: states are currently hardcoded, it would be better to use schema
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
76 item_elt = evt.target.closest(".item")
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
77 if item_elt is None:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
78 dialog.notification.show(
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
79 "Can't find parent item element",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
80 level="error"
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
81 )
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
82 return
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
83 item = JSON.parse(item_elt.dataset.item)
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
84 try:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
85 status = item["status"]
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
86 except (KeyError, IndexError) as e:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
87 dialog.notification.show(
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
88 f"Can't get item status: {e}",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
89 level="error"
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
90 )
1430
0f3038f3a954 browser (lists/view): fix status check in grocery list following `value` property fix in template_xmlui.OptionsWidget
Goffi <goffi@goffi.org>
parents: 1412
diff changeset
91 status = "to_buy"
0f3038f3a954 browser (lists/view): fix status check in grocery list following `value` property fix in template_xmlui.OptionsWidget
Goffi <goffi@goffi.org>
parents: 1412
diff changeset
92 if status == "to_buy":
0f3038f3a954 browser (lists/view): fix status check in grocery list following `value` property fix in template_xmlui.OptionsWidget
Goffi <goffi@goffi.org>
parents: 1412
diff changeset
93 item["status"] = "bought"
1395
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
94 class_update_method = item_elt.classList.add
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
95 checked = True
1430
0f3038f3a954 browser (lists/view): fix status check in grocery list following `value` property fix in template_xmlui.OptionsWidget
Goffi <goffi@goffi.org>
parents: 1412
diff changeset
96 elif status == "bought":
0f3038f3a954 browser (lists/view): fix status check in grocery list following `value` property fix in template_xmlui.OptionsWidget
Goffi <goffi@goffi.org>
parents: 1412
diff changeset
97 item["status"] = "to_buy"
1395
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
98 checked = False
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
99 class_update_method = item_elt.classList.remove
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
100 else:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
101 dialog.notification.show(
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
102 f"unexpected item status: {status!r}",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
103 level="error"
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
104 )
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
105 return
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
106 item_elt.dataset.item = JSON.stringify(item)
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
107 try:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
108 await bridge.listSet(
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
109 pubsub_service,
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
110 pubsub_node,
1412
6fdef01f6671 browser (lists/view): Q&D fix for status change
Goffi <goffi@goffi.org>
parents: 1395
diff changeset
111 # FIXME: value type should be consistent, or we should serialise
6fdef01f6671 browser (lists/view): Q&D fix for status change
Goffi <goffi@goffi.org>
parents: 1395
diff changeset
112 {k: (v if isinstance(v, list) else [v]) for k,v in item.items()},
1395
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
113 "",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
114 item["id"],
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
115 ""
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
116 )
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
117 except BridgeException as e:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
118 dialog.notification.show(
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
119 f"Can't udate list item: {e.message}",
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
120 level="error"
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
121 )
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
122 else:
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
123 evt.target.checked = checked
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
124 class_update_method("list-item-closed")
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
125
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
126
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
127 if list_type == "grocery":
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
128 for elt in document.select('.click_to_delete'):
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
129 elt.bind("click", lambda evt: aio.run(on_delete(evt)))
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
130
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
131 for elt in document.select('.click_to_next_state'):
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
132 elt.bind("click", lambda evt: aio.run(on_next_state(evt)))
4ccf42d8aab7 browser (lists/view): `grocery` list specialised behaviour:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
133