forked from punchagan/childrens-park
-
Notifications
You must be signed in to change notification settings - Fork 1
/
poll.py
214 lines (187 loc) · 6.58 KB
/
poll.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
#!/usr/bin/env python
import sqlite3
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def init_database():
db = sqlite3.connect("polls.db")
db.cursor().execute("CREATE TABLE IF NOT EXISTS polls (id INTEGER PRIMARY KEY, question TEXT, status INT DEFAULT 1, author TEXT, UNIQUE(question))")
db.cursor().execute("CREATE TABLE IF NOT EXISTS votes ( id INTEGER PRIMARY KEY, voter TEXT DEFAULT NULL, vote INTEGER, msg TEXT DEFAULT NULL, poll_id INTEGER, FOREIGN KEY(poll_id) REFERENCES polls(id), UNIQUE(voter, poll_id))")
db.close()
class PollFactory:
@staticmethod
def get_db():
"""Get a db connection. Caller MUST close it"""
pollsdb = sqlite3.connect("polls.db")
pollsdb.row_factory = dict_factory
return pollsdb
@staticmethod
def get_active_poll():
"""Get the current active poll"""
pollsdb = PollFactory.get_db()
c = pollsdb.cursor()
c.execute("SELECT id FROM polls WHERE status = 1 LIMIT 1")
row = c.fetchone()
c.close()
pollsdb.close()
if row is None:
poll = None
else:
poll = Poll(row["id"])
return poll
@staticmethod
def get_poll(poll_id):
"""Get a poll by using a poll id"""
pollsdb = PollFactory.get_db()
c = pollsdb.cursor()
try:
c.execute("SELECT id FROM polls WHERE id = ? LIMIT 1", (str(int(poll_id))))
except ValueError, e:
c.close()
pollsdb.close()
raise PollException('Invalid poll id: '+poll_id)
row = c.fetchone()
c.close()
pollsdb.close()
poll = Poll(row['id'])
return poll
@staticmethod
def get_polls():
"""Get all the polls"""
pollsdb = PollFactory.get_db()
c = pollsdb.cursor()
c.execute("SELECT id FROM polls WHERE status = 0 ORDER BY id")
rows = c.fetchall()
c.close()
pollsdb.close()
polls = []
for row in rows:
poll = Poll(row['id'])
polls.append(poll)
return polls
class BaseTable(object):
def __init__(self, table):
self.table = str(table)
self.pollsdb = PollFactory.get_db()
def __del__(self):
self.pollsdb.close()
def __fetch(self, instance_id):
c = self.pollsdb.cursor()
c.execute("SELECT * FROM "+self.table+" WHERE id = ? LIMIT 1", (str(instance_id)))
row = c.fetchone()
self.__init_attrs(row)
c.close()
def __init_attrs(self, attrs):
for field in self.__dict__.iteritems():
if field[1] is None:
try:
self.__dict__[field[0]] = attrs[field[0]]
except TypeError:
pass
class Poll(BaseTable):
def __init__(self, poll_id = None):
self.id = None
self.question = None
self.author = None
self.status = None
super(Poll, self).__init__('polls')
if poll_id is not None:
self._BaseTable__fetch(poll_id)
def new(self, question, author):
"""Creates a new poll"""
attrs = {}
attrs["question"] = question
attrs["author"] = author
self.__create_poll(attrs)
def __create_poll(self, attrs):
c = self.pollsdb.cursor()
try:
c.execute("INSERT INTO polls (question, status, author) VALUES(?, 1, ?)",\
(attrs["question"], attrs["author"]))
except sqlite3.IntegrityError:
self.pollsdb.rollback()
c.close()
raise PollException('Duplicated poll')
self.pollsdb.commit()
poll_id = c.lastrowid
c.close()
self._BaseTable__fetch(poll_id)
def close(self):
"""Close the poll"""
c = self.pollsdb.cursor()
c.execute("UPDATE polls SET status = 0 WHERE id = ?",\
(str(self.id)))
self.pollsdb.commit()
c.close()
self.status = 0
def vote(self, voter, vote_value, msg = None):
"""Vote"""
vote = VoteFactory.new_vote(self.id)
vote.new(voter, vote_value, msg)
def get_votes(self):
"""Get all the votes on a poll"""
c = VoteFactory.get_db().cursor()
c.execute("SELECT id FROM votes WHERE poll_id = ?", (str(self.id)))
rows = c.fetchall()
votes = []
for row in rows:
vote = Vote(self.id, row['id'])
votes.append(vote)
c.close()
return votes
class VoteFactory:
@staticmethod
def get_db():
"""Get a db connection. Caller MUST close it"""
pollsdb = sqlite3.connect("polls.db")
pollsdb.row_factory = dict_factory
pollsdb.cursor().execute("CREATE TABLE IF NOT EXISTS votes ( id INTEGER PRIMARY KEY, voter TEXT DEFAULT NULL, vote INTEGER, msg TEXT DEFAULT NULL, poll_id INTEGER, FOREIGN KEY(poll_id) REFERENCES polls(id), UNIQUE(voter, poll_id))")
return pollsdb
@staticmethod
def new_vote(poll_id):
"""Creates a new vote on a poll"""
return Vote(poll_id, None)
class Vote(BaseTable):
def __init__(self, poll_id, vote_id = None):
self.id = None
self.voter = None
self.vote = None
self.msg = None
self.poll_id = poll_id
super(Vote, self).__init__('votes')
if vote_id is not None:
self._BaseTable__fetch(vote_id)
def __str__(self):
if self.vote is None:
return "undefined"
if self.vote == 0:
return "no"
if self.vote == 1:
return "yes"
return "WTF!" # Should not happen...
def new(self, author, vote, msg = None):
"""Creates a new vote"""
attrs = {}
attrs["poll_id"] = self.poll_id
attrs["voter"] = author
attrs["vote"] = vote
attrs["msg"] = msg
self.__create_vote(attrs)
def __create_vote(self, attrs):
c = self.pollsdb.cursor()
try:
c.execute("INSERT INTO votes (voter, vote, msg, poll_id) VALUES(?, ?, ?, ?)",\
(attrs["voter"], attrs["vote"], attrs["msg"], attrs["poll_id"]))
except sqlite3.IntegrityError, e:
self.pollsdb.rollback()
c.close()
raise PollException('Duplicated vote')
self.pollsdb.commit()
vote_id = c.lastrowid
c.close()
self._BaseTable__fetch(vote_id)
class PollException(Exception):
"""Poll exception class"""
pass