Mercurial > libervia-backend
comparison src/plugins/plugin_exp_events.py @ 2231:230fc5b609a8
plugin event: first draft:
basic plugin to manage event attendance
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 27 Apr 2017 00:55:02 +0200 |
parents | |
children | 5e12fc5ae52a |
comparison
equal
deleted
inserted
replaced
2230:ebc0c1701811 | 2231:230fc5b609a8 |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin to detect language (experimental) | |
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
21 from sat.core.constants import Const as C | |
22 from sat.core.log import getLogger | |
23 log = getLogger(__name__) | |
24 from twisted.internet import defer | |
25 from twisted.words.protocols.jabber import jid | |
26 from twisted.words.xish import domish | |
27 from wokkel import pubsub | |
28 | |
29 | |
30 PLUGIN_INFO = { | |
31 C.PI_NAME: "Event plugin", | |
32 C.PI_IMPORT_NAME: "events", | |
33 C.PI_TYPE: "EXP", | |
34 C.PI_PROTOCOLS: [], | |
35 C.PI_DEPENDENCIES: ["XEP-0060"], | |
36 C.PI_MAIN: "Events", | |
37 C.PI_HANDLER: "no", | |
38 C.PI_DESCRIPTION: _("""Experimental implementation of XMPP events management""") | |
39 } | |
40 | |
41 NS_EVENT = 'org.salut-a-toi.event:0' | |
42 | |
43 | |
44 class Events(object): | |
45 """Q&D module to handle event attendance answer, experimentation only""" | |
46 | |
47 def __init__(self, host): | |
48 log.info(_(u"Event plugin initialization")) | |
49 self.host = host | |
50 self._p = self.host.plugins["XEP-0060"] | |
51 host.bridge.addMethod("eventGet", ".plugin", | |
52 in_sign='sss', out_sign='a{ss}', | |
53 method=self._eventGet, | |
54 async=True) | |
55 host.bridge.addMethod("eventSet", ".plugin", | |
56 in_sign='ssa{ss}s', out_sign='', | |
57 method=self._eventSet, | |
58 async=True) | |
59 | |
60 def _eventGet(self, service, node, profile_key): | |
61 service = jid.JID(service) if service else None | |
62 node = node if node else NS_EVENT | |
63 client = self.host.getClient(profile_key) | |
64 return self.eventGet(client, service, node) | |
65 | |
66 @defer.inlineCallbacks | |
67 def eventGet(self, client, service, node): | |
68 """Retrieve attendance from event node | |
69 | |
70 @param service(unicode, None): PubSub service | |
71 @param node(unicode): PubSub node of the event | |
72 @return (dict): a dict with current attendance status, | |
73 an empty dict is returned if nothing has been answered yed | |
74 """ | |
75 items, metadata = yield self._p.getItems(service, node, item_ids=[client.jid.userhost()], profile_key=client.profile) | |
76 try: | |
77 event_elt = next(items[0].elements((NS_EVENT, u'event'))) | |
78 except IndexError: | |
79 # no item found, event data are not set yet | |
80 defer.returnValue({}) | |
81 data = {} | |
82 for key in (u'attend', u'guests'): | |
83 try: | |
84 data[key] = event_elt[key] | |
85 except KeyError: | |
86 continue | |
87 defer.returnValue(data) | |
88 | |
89 def _eventSet(self, service, node, event_data, profile_key): | |
90 service = jid.JID(service) if service else None | |
91 node = node if node else NS_EVENT | |
92 client = self.host.getClient(profile_key) | |
93 return self.eventSet(client, service, node, event_data) | |
94 | |
95 def eventSet(self, client, service, node, data): | |
96 """Set or update attendance data in event node | |
97 | |
98 @param service(unicode, None): PubSub service | |
99 @param node(unicode): PubSub node of the event | |
100 @param data(dict[unicode, unicode]): data to update | |
101 key can be: | |
102 attend: one of "yes", "no", "maybe" | |
103 guests: an int | |
104 """ | |
105 event_elt = domish.Element((NS_EVENT, 'event')) | |
106 for key in (u'attend', u'guests'): | |
107 try: | |
108 event_elt[key] = data.pop(key) | |
109 except KeyError: | |
110 pass | |
111 item_elt = pubsub.Item(id=client.jid.userhost(), payload=event_elt) | |
112 return self._p.publish(service, node, items=[item_elt], profile_key=client.profile) |