comparison frontends/src/jp/cmd_event.py @ 2300:173d56315529

jp (event/invitee): added list command to get R.S.V.P. : RSVP are gotten for invitees node, and R.S.V.P. are displayed. Then a summary display the number of yes, maybe and no, and the expected number of guests.
author Goffi <goffi@goffi.org>
date Sun, 02 Jul 2017 20:09:25 +0200
parents a3cd2ac25d58
children 137ed5fbcbfd
comparison
equal deleted inserted replaced
2299:a3cd2ac25d58 2300:173d56315529
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 20
21 import base 21 import base
22 from sat.core.i18n import _ 22 from sat.core.i18n import _
23 from sat.tools.common.ansi import ANSI as A
23 from sat_frontends.jp.constants import Const as C 24 from sat_frontends.jp.constants import Const as C
24 from sat_frontends.jp import common 25 from sat_frontends.jp import common
25 from functools import partial 26 from functools import partial
26 from dateutil import parser as du_parser 27 from dateutil import parser as du_parser
27 import calendar 28 import calendar
28 import time 29 import time
29 30
30 __commands__ = ["Event"] 31 __commands__ = ["Event"]
32
33 OUTPUT_OPT_TABLE = u'table'
31 34
32 # TODO: move date parsing to base, it may be useful for other commands 35 # TODO: move date parsing to base, it may be useful for other commands
33 36
34 37
35 class Get(base.CommandBase): 38 class Get(base.CommandBase):
196 errback=partial(self.errback, 199 errback=partial(self.errback,
197 msg=_(u"can't set event data: {}"), 200 msg=_(u"can't set event data: {}"),
198 exit_code=C.EXIT_BRIDGE_ERRBACK)) 201 exit_code=C.EXIT_BRIDGE_ERRBACK))
199 202
200 203
204 class InviteesList(base.CommandBase):
205
206 def __init__(self, host):
207 extra_outputs = {'default': self.default_output}
208 base.CommandBase.__init__(self,
209 host,
210 'list',
211 use_output=C.OUTPUT_DICT_DICT,
212 extra_outputs=extra_outputs,
213 use_pubsub_node_req=True,
214 use_verbose=True,
215 help=_(u'get event attendance'))
216 self.need_loop=True
217
218 def add_parser_options(self):
219 pass
220
221 def _attend_filter(self, attend):
222 if attend == u'yes':
223 attend_color = C.A_SUCCESS
224 elif attend == u'no':
225 attend_color = C.A_FAILURE
226 else:
227 attend_color = A.FG_WHITE
228 return A.color(attend_color, attend)
229
230 def _guests_filter(self, guests):
231 return u'(' + guests + ')' if guests else u''
232
233 def default_output(self, event_data):
234 data = []
235 attendees_yes = 0
236 attendees_maybe = 0
237 attendees_no = 0
238 guests = 0
239 guests_maybe = 0
240 for jid_, jid_data in event_data.iteritems():
241 jid_data[u'jid'] = jid_
242 try:
243 guests_int = int(jid_data['guests'])
244 except (ValueError, KeyError):
245 pass
246 if jid_data[u'attend'] == 'yes':
247 attendees_yes += 1
248 guests += guests_int
249 elif jid_data[u'attend'] == 'maybe':
250 attendees_maybe += 1
251 guests_maybe += guests_int
252 elif jid_data[u'attend'] == 'no':
253 attendees_no += 1
254 jid_data[u'guests'] = ''
255 data.append(jid_data)
256
257 show_table = OUTPUT_OPT_TABLE in self.args.output_opts
258
259 table = common.Table.fromDict(self.host,
260 data,
261 (u'nick',) + ((u'jid',) if self.host.verbosity else ()) + (u'attend', 'guests'),
262 headers=None,
263 filters = { u'nick': A.color(C.A_HEADER, u'{} '),
264 u'jid': u'{} ',
265 u'attend': self._attend_filter,
266 u'guests': u'{}' if show_table else self._guests_filter,
267 },
268 defaults = { u'nick': u'',
269 u'attend': u'',
270 u'guests': 1
271 }
272 )
273 if show_table:
274 table.display()
275 else:
276 table.display_blank(show_header=False, col_sep=u'')
277
278 self.disp(u'')
279 self.disp(A.color(
280 C.A_SUBHEADER,
281 _(u'Attendees: '),
282 A.RESET,
283 unicode(len(data)),
284 _(u' ('),
285 C.A_SUCCESS,
286 _(u'yes: '),
287 unicode(attendees_yes),
288 A.FG_WHITE,
289 _(u', maybe: '),
290 unicode(attendees_maybe),
291 u', ',
292 C.A_FAILURE,
293 _(u'no: '),
294 unicode(attendees_no),
295 A.RESET,
296 u')'
297 ))
298 self.disp(A.color(C.A_SUBHEADER, _(u'confirmed guests: '), A.RESET, unicode(guests)))
299 self.disp(A.color(C.A_SUBHEADER, _(u'unconfirmed guests: '), A.RESET, unicode(guests_maybe)))
300 self.disp(A.color(C.A_SUBHEADER, _(u'total: '), A.RESET, unicode(guests+guests_maybe)))
301
302 def eventInviteesListCb(self, event_data):
303 for jid_, data in event_data.iteritems():
304 id_data = self.host.bridge.identityGet(jid_, self.profile)
305 data[u'nick'] = id_data.get(u'nick', u'')
306 self.output(event_data)
307 self.host.quit()
308
309 def start(self):
310 common.checkURI(self.args)
311 self.host.bridge.eventInviteesList(
312 self.args.service,
313 self.args.node,
314 self.profile,
315 callback=self.eventInviteesListCb,
316 errback=partial(self.errback,
317 msg=_(u"can't get event data: {}"),
318 exit_code=C.EXIT_BRIDGE_ERRBACK))
319
320
201 class InviteeInvite(base.CommandBase): 321 class InviteeInvite(base.CommandBase):
202 322
203 def __init__(self, host): 323 def __init__(self, host):
204 base.CommandBase.__init__(self, host, 'invite', use_pubsub_node_req=True, help=_(u'invite someone to the event through email')) 324 base.CommandBase.__init__(self, host, 'invite', use_pubsub_node_req=True, help=_(u'invite someone to the event through email'))
205 self.need_loop=True 325 self.need_loop=True
237 msg=_(u"can't create invitation: {}"), 357 msg=_(u"can't create invitation: {}"),
238 exit_code=C.EXIT_BRIDGE_ERRBACK)) 358 exit_code=C.EXIT_BRIDGE_ERRBACK))
239 359
240 360
241 class Invitee(base.CommandBase): 361 class Invitee(base.CommandBase):
242 subcommands = (InviteeGet, InviteeSet, InviteeInvite) 362 subcommands = (InviteeGet, InviteeSet, InviteesList, InviteeInvite)
243 363
244 def __init__(self, host): 364 def __init__(self, host):
245 super(Invitee, self).__init__(host, 'invitee', use_profile=False, help=_(u'manage invities')) 365 super(Invitee, self).__init__(host, 'invitee', use_profile=False, help=_(u'manage invities'))
246 366
247 367