Mercurial > libervia-web
comparison libervia/web/pages/lists/view/_browser/__init__.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/lists/view/_browser/__init__.py@5ea06e8b06ed |
children | d7c78722e4f8 |
comparison
equal
deleted
inserted
replaced
1517:b8ed9726525b | 1518:eb00d593801d |
---|---|
1 from browser import window, document, aio, bind | |
2 from invitation import InvitationManager | |
3 from javascript import JSON | |
4 from bridge import Async as Bridge, BridgeException | |
5 import dialog | |
6 | |
7 | |
8 bridge = Bridge() | |
9 lists_ns = window.lists_ns | |
10 pubsub_service = window.pubsub_service | |
11 pubsub_node = window.pubsub_node | |
12 list_type = window.list_type | |
13 try: | |
14 affiliations = window.affiliations.to_dict() | |
15 except AttributeError: | |
16 pass | |
17 | |
18 @bind("#button_manage", "click") | |
19 def manage_click(evt): | |
20 evt.stopPropagation() | |
21 evt.preventDefault() | |
22 pubsub_data = { | |
23 "namespace": lists_ns, | |
24 "service": pubsub_service, | |
25 "node": pubsub_node | |
26 } | |
27 try: | |
28 name = pubsub_node.split('_', 1)[1] | |
29 except IndexError: | |
30 pass | |
31 else: | |
32 name = name.strip() | |
33 if name: | |
34 pubsub_data['name'] = name | |
35 manager = InvitationManager("pubsub", pubsub_data) | |
36 manager.attach(affiliations=affiliations) | |
37 | |
38 | |
39 async def on_delete(evt): | |
40 item_elt = evt.target.closest(".item") | |
41 if item_elt is None: | |
42 dialog.notification.show( | |
43 "Can't find parent item element", | |
44 level="error" | |
45 ) | |
46 return | |
47 item_elt.classList.add("selected_for_deletion") | |
48 item = JSON.parse(item_elt.dataset.item) | |
49 confirmed = await dialog.Confirm( | |
50 f"{item['name']!r} will be deleted, are you sure?", | |
51 ok_label="delete", | |
52 ok_color="danger", | |
53 ).ashow() | |
54 item_elt.classList.remove("selected_for_deletion") | |
55 if confirmed: | |
56 try: | |
57 await bridge.ps_item_retract(pubsub_service, pubsub_node, item["id"], True) | |
58 except Exception as e: | |
59 dialog.notification.show( | |
60 f"Can't delete list item: {e}", | |
61 level="error" | |
62 ) | |
63 else: | |
64 dialog.notification.show("list item deleted successfuly") | |
65 item_elt.remove() | |
66 | |
67 | |
68 async def on_next_state(evt): | |
69 """Update item with next state | |
70 | |
71 Only used with grocery list at the moment | |
72 """ | |
73 evt.stopPropagation() | |
74 evt.preventDefault() | |
75 # FIXME: states are currently hardcoded, it would be better to use schema | |
76 item_elt = evt.target.closest(".item") | |
77 if item_elt is None: | |
78 dialog.notification.show( | |
79 "Can't find parent item element", | |
80 level="error" | |
81 ) | |
82 return | |
83 item = JSON.parse(item_elt.dataset.item) | |
84 try: | |
85 status = item["status"] | |
86 except (KeyError, IndexError) as e: | |
87 dialog.notification.show( | |
88 f"Can't get item status: {e}", | |
89 level="error" | |
90 ) | |
91 status = "to_buy" | |
92 if status == "to_buy": | |
93 item["status"] = "bought" | |
94 class_update_method = item_elt.classList.add | |
95 checked = True | |
96 elif status == "bought": | |
97 item["status"] = "to_buy" | |
98 checked = False | |
99 class_update_method = item_elt.classList.remove | |
100 else: | |
101 dialog.notification.show( | |
102 f"unexpected item status: {status!r}", | |
103 level="error" | |
104 ) | |
105 return | |
106 item_elt.dataset.item = JSON.stringify(item) | |
107 try: | |
108 await bridge.list_set( | |
109 pubsub_service, | |
110 pubsub_node, | |
111 # FIXME: value type should be consistent, or we should serialise | |
112 {k: (v if isinstance(v, list) else [v]) for k,v in item.items()}, | |
113 "", | |
114 item["id"], | |
115 "" | |
116 ) | |
117 except BridgeException as e: | |
118 dialog.notification.show( | |
119 f"Can't udate list item: {e.message}", | |
120 level="error" | |
121 ) | |
122 else: | |
123 evt.target.checked = checked | |
124 class_update_method("list-item-closed") | |
125 | |
126 | |
127 if list_type == "grocery": | |
128 for elt in document.select('.click_to_delete'): | |
129 elt.bind("click", lambda evt: aio.run(on_delete(evt))) | |
130 | |
131 for elt in document.select('.click_to_next_state'): | |
132 elt.bind("click", lambda evt: aio.run(on_next_state(evt))) | |
133 |