/
plugin.py
959 lines (738 loc) · 35.1 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
###
# Copyright (c) 2013, Arik Baratz
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import os
import sqlite3
import collections
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.ircmsgs as ircmsgs
meeting_singleton = None
VALID_VOTE = ['aye', 'nay', 'abstain']
class Meeting(callbacks.Plugin, plugins.ChannelDBHandler):
"""This plugin deals with these entities:
Meetings - the scope of the meeting containing agenda items and motions, and participants.
Adenda - items on the agenda, splitting the discussion along topica
Motion - a motion (voted on by the members present in the meeting
list this module to see the different options"""
def __init__(self, irc):
global meeting_singleton
#self.__parent = super(Meeting, self)
#self.__parent.__init__(irc)
callbacks.Plugin.__init__(self, irc)
plugins.ChannelDBHandler.__init__(self)
# cache channel vote results and verify uniqueness
self._voter_decision = {}
# keep a reference to the singleton so that we can reference it from sub commands
meeting_singleton = self
def die(self):
"""so you're going to die"""
global meeting_singleton
# allow efficient GC by removing the module level reference to the object
meeting_singleton = None
plugins.ChannelDBHandler.die(self)
callbacks.Plugin.die(self)
def makeDb(self, filename):
need_to_create = not os.path.exists(filename)
db = sqlite3.connect(filename)
db.text_factory = str
if need_to_create:
cursor = db.cursor()
cursor.execute("""CREATE TABLE meeting (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
start_time TIMESTAMP NULL,
end_time TIMESTAMP NULL
)""")
cursor.execute("""CREATE TABLE agenda (
id INTEGER PRIMARY KEY,
meeting_id INTEGER,
item_order INTEGER,
item_text TEXT,
FOREIGN KEY(meeting_id) REFERENCES meeting(id)
)""")
cursor.execute("""CREATE TABLE motion (
id INTEGER PRIMARY KEY,
meeting_id INTEGER,
item_order INTEGER,
motion_text TEXT,
vote_open BOOLEAN,
votes_aye INTEGER,
votes_nay INTEGER,
votes_abstain INTEGER,
carries BOOLEAN,
decision_at TIMESTAMP,
FOREIGN KEY(meeting_id) REFERENCES meeting(id)
)""")
cursor.execute("""CREATE TABLE currents (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
value INTEGER
)""")
cursor.execute("""CREATE TABLE vote (
id INTEGER PRIMARY KEY AUTOINCREMENT,
motion_id INTEGER,
voter TEXT,
vote TEXT,
FOREIGN KEY(motion_id) REFERENCES motion(id)
)""")
for current_name in ['meeting', 'agenda', 'motion', 'vote']:
cursor.execute("""INSERT INTO currents
VALUES (NULL, ?, NULL)""", (current_name,))
db.commit()
return db
def _get_current(self, channel, current_name):
"""returns the meeting ID or None if doesn't exist"""
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT value
FROM currents
WHERE name=?""", (current_name, ))
results = cursor.fetchall()
if len(results)==0:
return None
return results[0][0]
def _set_current(self, channel, current_name, value):
"""returns the meeting ID or None if doesn't exist"""
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""UPDATE currents
SET value=?
WHERE name=?""", (value, current_name))
db.commit()
def _start_vote_cache(self, channel):
"""initialise the voter decision cache"""
if channel in self._voter_decision:
return False
# prepare the voter decision cache for the channel
self._voter_decision[channel] = {}
# get the current meeting ID
meeting_id = self._get_current(channel, 'meeting')
if meeting_id is None:
return False
# get the motion order
motion_item_order = self._get_current(channel, 'motion')
if motion_item_order is None:
return False
# Get the database
db = self.getDb(channel)
# get the motion ID from the database
cursor = db.cursor()
# update the motion
cursor.execute("""UPDATE motion
SET vote_open=1
WHERE meeting_id=?
AND item_order=?""",
(meeting_id, motion_item_order))
db.commit()
return True
def _end_vote_cache(self, channel):
"""store the votes in the vote database"""
if not channel in self._voter_decision:
return
# get the current meeting ID
meeting_id = self._get_current(channel, 'meeting')
if meeting_id is None:
return
# get the motion order
motion_item_order = self._get_current(channel, 'motion')
if motion_item_order is None:
return
# Get the database
db = self.getDb(channel)
# get the motion ID from the database
cursor = db.cursor()
cursor.execute("""SELECT id
FROM motion
WHERE meeting_id=?
AND item_order=?""", (meeting_id, motion_item_order))
results = cursor.fetchall()
if len(results) == 0:
return
motion_id = results[0][0]
# do it
cursor = db.cursor()
count = {}
for vote in VALID_VOTE:
count[vote] = 0
# insert the lines into the vote table
for voter, vote in self._voter_decision[channel].items():
cursor.execute("""INSERT INTO vote
VALUES (NULL, ?, ?, ?)""",
(motion_id, voter, vote))
count[vote] += 1
# update the motion
cursor.execute("""UPDATE motion
SET vote_open=0,
votes_aye=?,
votes_nay=?,
votes_abstain=?,
carries=?,
decision_at=datetime('now')
WHERE id=?""",
(count['aye'], count['nay'], count['abstain'],
count['aye'] > count['nay'], motion_id))
# done
db.commit()
# delete the local cache
del self._voter_decision[channel]
return (count['aye'], count['nay'], count['abstain'])
def doPrivmsg(self, irc, msg):
"""check regular IRC chat for votes"""
# check for a regular channel message
if ircmsgs.isCtcp(msg) and not ircmsgs.isAction(msg):
return
channel = msg.args[0]
if not irc.isChannel(channel):
return
# is there an active vote in the channel?
if not (channel in self._voter_decision):
return
# get the text of the vote
if ircmsgs.isAction(msg):
text = ircmsgs.unAction(msg)
else:
text = msg.args[1]
# is it a vote?
if not text in VALID_VOTE:
return
# get the voter
voter = msg.prefix
# update the cache
self._voter_decision[channel][voter] = text
def prepare(self, irc, msg, args, channel, meet_name):
"""[<channel>] <meeting name>
Initialises a meeting
"""
# get the database
db = self.getDb(channel)
# insert the new meeting
cursor = db.cursor()
cursor.execute("""INSERT INTO meeting
VALUES (NULL, ?, NULL, NULL)""", (meet_name, ))
meeting_id = cursor.lastrowid
db.commit()
# set it as current
self._set_current(channel, 'meeting', meeting_id)
irc.reply("Meeting initialised, meeting id %d on channel %s" % (meeting_id, channel))
prepare = wrap(prepare, ['channel', 'text'])
def start(self, irc, msg, args, channel):
"""[<channel>]
Starts the meeting
"""
# get the meeting id
meeting_id = self._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("No active meeting on channel %s" % channel)
return
# get the database
db = self.getDb(channel)
# get the meeting details
cursor = db.cursor()
cursor.execute("""SELECT name, start_time, end_time
FROM meeting
WHERE id=?""", (meeting_id, ))
results = cursor.fetchall()
if len(results)==0:
irc.error("This shouldn't happen... current meeting id %d invalid" % meeting_id)
return
meeting_name, start_time, end_time = results[0]
# check for a current meeting and prevent starting it if it's in progress or adjourned
if end_time is not None:
irc.error("The meeting has already adjourned. Try preparing a new one.")
return
if start_time is not None:
irc.error("This meeting has already started")
return
# mark the meeting as started
cursor.execute("""UPDATE meeting
SET start_time=datetime('now')
WHERE id=? """, (meeting_id,))
db.commit()
# initialise the agenda and motion pointers
self._set_current(channel, 'agenda', None)
self._set_current(channel, 'motion', None)
irc.queueMsg(ircmsgs.topic(channel, meeting_name))
irc.reply("The meeting has started. Meeting topic: %s (meeting id %d)" % (meeting_name, meeting_id))
start = wrap(start, ['channel'])
def adjourn(self, irc, msg, args, channel):
"""[<channel>]
Adjourns the meeting
"""
# get the meeting id
meeting_id = self._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("No active meeting on channel %s" % channel)
return
# get the database
db = self.getDb(channel)
# get the meeting details
cursor = db.cursor()
cursor.execute("""SELECT name
FROM meeting
WHERE id=?""", (meeting_id, ))
results = cursor.fetchall()
if len(results)==0:
irc.error("This shouldn't happen... current meeting id %d invalid" % meeting_id)
return
meeting_name = results[0][0]
# mark the meeting as ended
cursor.execute("""UPDATE meeting
SET end_time=datetime('now')
WHERE id=? """, (meeting_id,))
db.commit()
irc.queueMsg(ircmsgs.topic(channel, meeting_name))
irc.reply("The meeting has adjourned. Meeting topic: %s (meeting id %d)" % (meeting_name, meeting_id))
adjourn = wrap(adjourn, ['channel'])
def switchid(self, irc, msg, args, channel, meeting_id):
"""[<channel>] <meeting_id>
Switch to a meeting with a given ID
"""
# get the database
db = self.getDb(channel)
# get the meeting details
cursor = db.cursor()
cursor.execute("""SELECT name
FROM meeting
WHERE id=?""", (meeting_id, ))
results = cursor.fetchall()
if len(results)==0:
irc.error("Cannot switch - meeting id %d doesn't belong in channel %s or is invalid" % (meeting_id, channel))
return
meeting_name = results[0][0]
# switch
self._set_current(channel, 'meeting', meeting_id)
irc.reply("Switched to meeting id %d, meeting name %s" % (meeting_id, meeting_name))
switchid = wrap(switchid, ['channel', 'positiveInt'])
def status(self, irc, msg, args, channel):
"""[<channel>]
Reply with the status of the current meeting
"""
# get the meeting id
meeting_id = self._get_current(channel, 'meeting')
if meeting_id is None:
irc.reply("Channel %s does not have a current meeting" % channel)
return
# get the database
db = self.getDb(channel)
# get the meeting details
cursor = db.cursor()
cursor.execute("""SELECT name, start_time, end_time
FROM meeting
WHERE id=?""", (meeting_id, ))
results = cursor.fetchall()
if len(results)==0:
irc.error("Current meeting for channel %s is %d, but it seems to be invalid." % (channel, meeting_id))
return
meeting_name, start_time, end_time = results[0]
irc.reply("Current meeting for channel %s is %s (id %d)" % (channel, meeting_name, meeting_id))
if end_time:
irc.reply("The meeting has adjourned")
elif start_time:
irc.reply("The meeting is currently in progress")
else:
irc.reply("The meeting has not started yet")
status = wrap(status, ['channel'])
class agenda(callbacks.Commands):
def get_max_item_order(self, db, meeting_id):
"""return the latest agenda item order, or zero if no items"""
cursor = db.cursor()
cursor.execute("""SELECT max(item_order)
FROM agenda
WHERE meeting_id=?""", (meeting_id, ))
results = cursor.fetchall()
if not results[0][0]: # no agenda item yet
return 0
return results[0][0]
def add(self, irc, msg, args, channel, agenda_text):
"""[<channel>] agenda text...
Add an agenda item to the end of the agenda
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the database
db = meeting_singleton.getDb(channel)
# figure out the highest agenda item id used so far
agenda_item_order = self.get_max_item_order(db, meeting_id) + 1
# insert the new item
cursor = db.cursor()
cursor.execute("""INSERT INTO agenda
VALUES (NULL, ?, ?, ?)""",
(meeting_id, agenda_item_order, agenda_text))
db.commit()
meeting_singleton._set_current(channel, 'agenda', agenda_item_order)
irc.reply("Agenda item %d added to the current meeting" % agenda_item_order)
add = wrap(add, ['channel', 'text'])
def list(self, irc, msg, args, channel):
"""[<channel>]
List the agenda for the current meeting in the channel
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the database
db = meeting_singleton.getDb(channel)
# get the agenda items
cursor = db.cursor()
cursor.execute("""SELECT item_order, item_text
FROM agenda
WHERE meeting_id=?
ORDER BY item_order ASC""", (meeting_id, ))
results = cursor.fetchall()
if len(results)==0:
irc.reply("The current meeting does not have an agenda yet")
return
for item_order, item_text in results:
irc.reply("Item %d: %s" % (item_order, item_text))
list = wrap(list, ['channel'])
def delete(self, irc, msg, args, channel, item_id):
"""[<channel>] <item_id>
Delete an item from the agenda, renumbering following items
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the database
db = meeting_singleton.getDb(channel)
# see how many we have now
total_items = self.get_max_item_order(db, meeting_id)
# check parameter
if item_id > total_items:
irc.error("Cannot delete non-existent item %d" % item_id)
return
# do the delete
cursor = db.cursor()
cursor.execute("""DELETE FROM agenda
WHERE meeting_id=?
AND item_order=?""", (meeting_id, item_id))
# renumber the rest of the items
for item in range(item_id+1, total_items+1):
cursor.execute("""UPDATE agenda
SET item_order=?
WHERE meeting_id=?
AND item_order=?""", (item-1, meeting_id, item))
db.commit()
# handle current item
if total_items == 1:
# no more agenda items
meeting_singleton._set_current(channel, 'agenda', None)
else:
current_agenda = meeting_singleton._get_current(channel, 'agenda')
# was it among the items that were shifted down?
if current_agenda > item_id:
meeting_singleton._set_current(channel, 'agenda', current_agenda - 1)
irc.reply("Agenda item %d has been deleted" % item_id)
delete = wrap(delete, ['channel', 'positiveInt'])
def next(self, irc, msg, args, channel):
"""[<channel>]
Progresses the agenda to the next item
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the database
db = meeting_singleton.getDb(channel)
# see how many we have now
total_items = self.get_max_item_order(db, meeting_id)
if total_items == 0:
irc.reply("Current meeting has no agenda.")
return
# check current item
current_item = meeting_singleton._get_current(channel, 'agenda')
if current_item == total_items:
irc.reply("No more items on the agenda for the current meeting")
return
# progress to the next item
if current_item is None:
current_item = 1
else:
current_item += 1
# set the new value
meeting_singleton._set_current(channel, 'agenda', current_item)
# get the agenda item
cursor = db.cursor()
cursor.execute("""SELECT item_order, item_text
FROM agenda
WHERE meeting_id=?
AND item_order=?""", (meeting_id, current_item))
results = cursor.fetchall()
if len(results)==0:
irc.error("Something went wrong, couldn't retrieve agenda item")
return
# display
item_order, item_text = results[0]
irc.reply("Current agenda item no. %d: %s" % (item_order, item_text))
next = wrap(next, ['channel'])
class motion(callbacks.Commands):
def get_max_item_order(self, db, meeting_id):
"""return the latest motion item order, or zero if no items"""
cursor = db.cursor()
cursor.execute("""SELECT max(item_order)
FROM motion
WHERE meeting_id=?""", (meeting_id, ))
results = cursor.fetchall()
if not results[0][0]: # no motion item yet
return 0
return results[0][0]
def add(self, irc, msg, args, channel, motion_text):
"""[<channel>] motion text...
Add a motion
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the database
db = meeting_singleton.getDb(channel)
# figure out the highest motion item id used so far
motion_item_order = self.get_max_item_order(db, meeting_id) + 1
# insert the new item
cursor = db.cursor()
cursor.execute("""INSERT INTO motion
VALUES (NULL, ?, ?, ?, 0,
NULL, NULL, NULL, NULL, NULL)""",
(meeting_id, motion_item_order, motion_text))
db.commit()
meeting_singleton._set_current(channel, 'motion', motion_item_order)
irc.reply("Motion %d added to the current meeting" % motion_item_order)
add = wrap(add, ['channel', 'text'])
def amend(self, irc, msg, args, channel, motion_text):
"""[<channel>] motion text...
Modify the current motion
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the current motion
motion_order = meeting_singleton._get_current(channel, 'motion')
if motion_order is None:
irc.error("There is no current motion in the current meeting")
return
# get the database
db = meeting_singleton.getDb(channel)
# check that the motion has not yet been decided
cursor = db.cursor()
cursor.execute("""SELECT carries
FROM motion
WHERE meeting_id=?
AND item_order=?""", (meeting_id, motion_order))
results = cursor.fetchall()
if len(results)==0:
irc.error("This shouldn't happen - current motion could not be retrieved")
return
if results[0][0] is not None:
irc.reply("The motion has already been decided, it cannot be amended")
return
# update the motion
cursor = db.cursor()
cursor.execute("""UPDATE motion
SET motion_text=?
WHERE meeting_id=?
AND item_order=?""", (meeting_id, motion_order))
db.commit()
irc.reply("Motion %d has been amended as requested." % motion_item_order)
amend = wrap(amend, ['channel', 'text'])
def list(self, irc, msg, args, channel):
"""[<channel>]
List the motions for the current meeting in the channel
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the database
db = meeting_singleton.getDb(channel)
# get the motion items
cursor = db.cursor()
cursor.execute("""SELECT item_order, motion_text,
carries, votes_aye, votes_nay, decision_at
FROM motion
WHERE meeting_id=?
ORDER BY item_order ASC""", (meeting_id, ))
results = cursor.fetchall()
if len(results)==0:
irc.reply("The current meeting does not have any motions")
return
for item_order, motion_text, carries, aye, nay, decision_time in results:
if carries is None:
carries_text = "Motion has not been up for vote yet"
elif carries:
carries_text = "Motion carries, votes %d:%d at %s" % (aye, nay, decision_time)
else:
carries_text = "Motion dismissed, votes %d:%d" % (aye, nay)
irc.reply("Motion %d: %s - %s" % (item_order, motion_text, carries_text))
list = wrap(list, ['channel'])
def delete(self, irc, msg, args, channel, item_id):
"""[<channel>] <item_id>
Delete a motion from the motion table, renumbering following motions.
Cannot delete motions that have carried.
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the database
db = meeting_singleton.getDb(channel)
# see how many we have now
total_items = self.get_max_item_order(db, meeting_id)
# check parameter
if item_id > total_items:
irc.error("Cannot delete the non-existent motion %d" % item_id)
return
# check the motion being deleted, if it carries it can't be deleted
cursor = db.cursor()
cursor.execute("""SELECT carries
FROM motion
WHERE meeting_id=?
AND item_order=?""", (meeting_id, item_id))
results = cursor.fetchall()
if len(results)==0:
irc.error("Something's wrong, this motion should have existed")
return
if results[0][0] is not None and result[0][0]:
irc.error("Motion %d cannot be deleted because it has carried. It's not a time machine, James." % item_id)
# do the delete
cursor = db.cursor()
cursor.execute("""DELETE FROM motion
WHERE meeting_id=?
AND item_order=?""", (meeting_id, item_id))
# renumber the rest of the items
for item in range(item_id+1, total_items+1):
cursor.execute("""UPDATE motion
SET item_order=?
WHERE meeting_id=?
AND item_order=?""", (item-1, meeting_id, item))
# handle current item
if total_items == 1:
# no more agenda items
meeting_singleton._set_current(channel, 'motion', None)
else:
current_motion = meeting_singleton._get_current(channel, 'motion')
# was it among the items that were shifted down?
if current_motion > item_id:
meeting_singleton._set_current(channel, 'motion', current_motion - 1)
db.commit()
irc.reply("Motion %d has been deleted" % item_id)
delete = wrap(delete, ['channel', 'positiveInt'])
class vote(callbacks.Commands):
def start(self, irc, msg, args, channel):
"""[<channel>]
Start the voting on the current motion
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the current motion
motion_item_order = meeting_singleton._get_current(channel, 'motion')
if motion_item_order is None:
irc.error("There is no current motion in the meeting")
# get the database
db = meeting_singleton.getDb(channel)
# get the motion details
cursor = db.cursor()
cursor.execute("""SELECT motion_text, vote_open, carries
FROM motion
WHERE meeting_id=?
AND item_order=?""", (meeting_id, motion_item_order))
results = cursor.fetchall()
if len(results)==0:
irc.error("Database error - invalid current motion")
return
motion_text, vote_open, motion_carries = results[0]
# check that the motion hasn't been decided yet
if motion_carries is not None:
irc.error("Current motion has already been decided")
return
if vote_open:
irc.error("Voting on the current motion is already open")
return
# set the vote flag in the singleton
meeting_singleton._start_vote_cache(channel)
irc.reply("Voting open! Please vote aye, nay or abstain for motion: %s" % motion_text)
start = wrap(start, ['channel'])
def end(self, irc, msg, args, channel):
"""[<channel>]
Finishes (and tallies) the vote results
"""
# get the current meeting ID
meeting_id = meeting_singleton._get_current(channel, 'meeting')
if meeting_id is None:
irc.error("There is no current meeting in channel %s" % channel)
return
# get the current motion
motion_item_order = meeting_singleton._get_current(channel, 'motion')
if motion_item_order is None:
irc.error("There is no current motion in the meeting")
# get the database
db = meeting_singleton.getDb(channel)
# get the motion details
cursor = db.cursor()
cursor.execute("""SELECT motion_text, vote_open, carries
FROM motion
WHERE meeting_id=?
AND item_order=?""", (meeting_id, motion_item_order))
results = cursor.fetchall()
if len(results)==0:
irc.error("Database error - invalid current motion")
return
motion_text, vote_open, motion_carries = results[0]
# check that the motion hasn't been decided yet
if motion_carries is not None:
irc.error("Current motion has already been decided")
return
if not vote_open:
irc.error("Voting on the current motion has not been started yet")
return
# set the vote flag in the singleton
result = meeting_singleton._end_vote_cache(channel)
if result is None:
irc.error("Vote counting failed.")
return
irc.reply("Voting closed - %d aye | %d nay | %d abstained" % result)
end = wrap(end, ['channel'])
Class = Meeting
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: