diff 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
line wrap: on
line diff
--- a/frontends/src/jp/cmd_event.py	Sun Jul 02 20:09:24 2017 +0200
+++ b/frontends/src/jp/cmd_event.py	Sun Jul 02 20:09:25 2017 +0200
@@ -20,6 +20,7 @@
 
 import base
 from sat.core.i18n import _
+from sat.tools.common.ansi import ANSI as A
 from sat_frontends.jp.constants import Const as C
 from sat_frontends.jp import common
 from functools import partial
@@ -29,6 +30,8 @@
 
 __commands__ = ["Event"]
 
+OUTPUT_OPT_TABLE = u'table'
+
 # TODO: move date parsing to base, it may be useful for other commands
 
 
@@ -198,6 +201,123 @@
                             exit_code=C.EXIT_BRIDGE_ERRBACK))
 
 
+class InviteesList(base.CommandBase):
+
+    def __init__(self, host):
+        extra_outputs = {'default': self.default_output}
+        base.CommandBase.__init__(self,
+                                  host,
+                                  'list',
+                                  use_output=C.OUTPUT_DICT_DICT,
+                                  extra_outputs=extra_outputs,
+                                  use_pubsub_node_req=True,
+                                  use_verbose=True,
+                                  help=_(u'get event attendance'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        pass
+
+    def _attend_filter(self, attend):
+        if attend == u'yes':
+            attend_color = C.A_SUCCESS
+        elif attend == u'no':
+            attend_color = C.A_FAILURE
+        else:
+            attend_color = A.FG_WHITE
+        return A.color(attend_color, attend)
+
+    def _guests_filter(self, guests):
+        return u'(' + guests + ')' if guests else u''
+
+    def default_output(self, event_data):
+        data = []
+        attendees_yes = 0
+        attendees_maybe = 0
+        attendees_no = 0
+        guests = 0
+        guests_maybe = 0
+        for jid_, jid_data in event_data.iteritems():
+            jid_data[u'jid'] = jid_
+            try:
+                guests_int = int(jid_data['guests'])
+            except (ValueError, KeyError):
+                pass
+            if jid_data[u'attend'] == 'yes':
+                attendees_yes += 1
+                guests += guests_int
+            elif jid_data[u'attend'] == 'maybe':
+                attendees_maybe += 1
+                guests_maybe += guests_int
+            elif jid_data[u'attend'] == 'no':
+                attendees_no += 1
+                jid_data[u'guests'] = ''
+            data.append(jid_data)
+
+        show_table = OUTPUT_OPT_TABLE in self.args.output_opts
+
+        table = common.Table.fromDict(self.host,
+            data,
+            (u'nick',) + ((u'jid',) if self.host.verbosity else ()) +  (u'attend', 'guests'),
+            headers=None,
+            filters = { u'nick': A.color(C.A_HEADER, u'{} '),
+                        u'jid': u'{} ',
+                        u'attend': self._attend_filter,
+                        u'guests': u'{}' if show_table else self._guests_filter,
+                      },
+            defaults = { u'nick': u'',
+                         u'attend': u'',
+                         u'guests': 1
+                       }
+            )
+        if show_table:
+            table.display()
+        else:
+            table.display_blank(show_header=False, col_sep=u'')
+
+        self.disp(u'')
+        self.disp(A.color(
+            C.A_SUBHEADER,
+            _(u'Attendees: '),
+            A.RESET,
+            unicode(len(data)),
+            _(u' ('),
+            C.A_SUCCESS,
+            _(u'yes: '),
+            unicode(attendees_yes),
+            A.FG_WHITE,
+            _(u', maybe: '),
+            unicode(attendees_maybe),
+            u', ',
+            C.A_FAILURE,
+            _(u'no: '),
+            unicode(attendees_no),
+            A.RESET,
+            u')'
+            ))
+        self.disp(A.color(C.A_SUBHEADER, _(u'confirmed guests: '), A.RESET, unicode(guests)))
+        self.disp(A.color(C.A_SUBHEADER, _(u'unconfirmed guests: '), A.RESET, unicode(guests_maybe)))
+        self.disp(A.color(C.A_SUBHEADER, _(u'total: '), A.RESET, unicode(guests+guests_maybe)))
+
+    def eventInviteesListCb(self, event_data):
+        for jid_, data in event_data.iteritems():
+            id_data = self.host.bridge.identityGet(jid_, self.profile)
+            data[u'nick'] = id_data.get(u'nick', u'')
+        self.output(event_data)
+        self.host.quit()
+
+    def start(self):
+        common.checkURI(self.args)
+        self.host.bridge.eventInviteesList(
+            self.args.service,
+            self.args.node,
+            self.profile,
+            callback=self.eventInviteesListCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't get event data: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
 class InviteeInvite(base.CommandBase):
 
     def __init__(self, host):
@@ -239,7 +359,7 @@
 
 
 class Invitee(base.CommandBase):
-    subcommands = (InviteeGet, InviteeSet, InviteeInvite)
+    subcommands = (InviteeGet, InviteeSet, InviteesList, InviteeInvite)
 
     def __init__(self, host):
         super(Invitee, self).__init__(host, 'invitee', use_profile=False, help=_(u'manage invities'))