This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
draw_member.py
147 lines (121 loc) · 3.94 KB
/
draw_member.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
import csv
from datetime import datetime
import random
import sqlite3
from flask import Flask, g, render_template, request
app = Flask(__name__)
SQLITE_DB_PATH = 'members.db'
SQLITE_DB_SCHEMA = 'create_db.sql'
MEMBER_CSV_PATH = 'members.csv'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/draw', methods=['POST'])
def draw():
# Get the database connection
db = get_db()
# Draw member ids from given group
# If ALL is given then draw from all members
group_name = request.form.get('group_name', 'ALL')
valid_members_sql = 'SELECT id FROM members '
if group_name == 'ALL':
cursor = db.execute(valid_members_sql)
else:
valid_members_sql += 'WHERE group_name = ?'
cursor = db.execute(valid_members_sql, (group_name, ))
valid_member_ids = [
row[0] for row in cursor
]
# If no valid members return 404 (unlikely)
if not valid_member_ids:
err_msg = "<p>No member in group '%s'</p>" % group_name
return err_msg, 404
# Randomly choice a member
lucky_member_id = random.choice(valid_member_ids)
# Obtain the lucy member's information
member_name, member_group_name = db.execute(
'SELECT name, group_name FROM members WHERE id = ?',
(lucky_member_id, )
).fetchone()
# Update draw history
with db:
db.execute('INSERT INTO draw_histories (memberid) VALUES (?)',
(lucky_member_id, ))
# Render template
return render_template(
'draw.html',
name=member_name,
group=member_group_name,
)
@app.route('/history')
def history():
db = get_db()
c = db.execute(
'SELECT m.name, m.group_name, d.time AS "draw_time [timestamp]"'
'FROM draw_histories AS d, members as m '
'WHERE m.id == d.memberid '
'ORDER BY d.time DESC '
'LIMIT 10'
)
recent_histories = []
for row in c:
recent_histories.append({
'name': row[0],
'group': row[1],
'draw_time': datetime.strptime(row[2], '%Y-%m-%d %H:%M:%S'),
})
return render_template('history.html', recent_histories=recent_histories)
# SQLite3-related operations
# See SQLite3 usage pattern from Flask official doc
# http://flask.pocoo.org/docs/0.10/patterns/sqlite3/
def get_db():
"""Get the SQLite database connection.
If called outside Flask, e.g. Python shell,
one should wrap it with app.app_context()::
# Get all members
with app.app_context():
db = get_db()
for row in db.execute('SELECT * FROM members'):
print(row)
Use the return value out of app_context() will raise
`sqlite3.ProgrammingError` because the database connection
is closed::
db.execute('...')
"""
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(SQLITE_DB_PATH)
# Enable foreign key check
db.execute("PRAGMA foreign_keys = ON")
return db
def reset_db():
with open(SQLITE_DB_SCHEMA, 'r') as f:
create_db_sql = f.read()
db = get_db()
# Reset database
# Note that CREATE/DROP table are *immediately* committed
# even inside a transaction
with db:
db.execute("DROP TABLE IF EXISTS draw_histories")
db.execute("DROP TABLE IF EXISTS members")
db.executescript(create_db_sql)
# Read members CSV data
with open(MEMBER_CSV_PATH, newline='') as f:
csv_reader = csv.DictReader(f)
members = [
(row['名字'], row['團體'])
for row in csv_reader
]
# Write members into database
with db:
db.executemany(
'INSERT INTO members (name, group_name) VALUES (?, ?)',
members
)
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
if __name__ == '__main__':
app.run(debug=True)