-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsvp.py
executable file
·205 lines (177 loc) · 8.43 KB
/
rsvp.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
#!/usr/bin/python3
# -*- coding; UTF-8 -*-# enable debugging
from mysql.connector import connect, Error
import cgi
import cgitb
cgitb.enable()
host = "localhost"
database = "wedding"
user = "root"
password = "l4m4k1ng"
def run_sql(command, values):
with connect(host=host, user=user, password=password, database=database) as connection:
with connection.cursor() as cursor:
cursor.execute(command, values)
return cursor.fetchall()
def get_rsvp(code):
command = "SELECT * FROM rsvp WHERE code = %s"
values = [code.upper()]
return run_sql(command, values)
def get_guests(rsvp_id):
command = "SELECT * FROM guests WHERE rsvp_id = %s"
values = [rsvp_id]
return run_sql(command, values)
def guest_attendance(guest):
guest_id = guest[0]
guest_attending = guest[3]
guest_evening = guest[8]
guest_attendance = "day"
if guest_attending == 1 and guest_evening == 1:
guest_attendance = "evening"
elif guest_attending == 0 and guest_evening == 0:
guest_attendance = "neither"
options = guest_label(guest)
option_clicked = " checked" if guest_attendance == "day" else ""
options += "<br><input type='radio' name='attendance_{}' id='day_{}' value='day' onclick='attendance_changed(\"day\", {})'{}/> ".format(guest_id, guest_id, guest_id, option_clicked)
options += "<label for='day_{}'>Full day</label>".format(guest_id)
option_clicked = " checked" if guest_attendance == "evening" else ""
options += "<br><input type='radio' name='attendance_{}' id='evening_{}' value='evening' onclick='attendance_changed(\"evening\", {})'{}/> ".format(guest_id, guest_id, guest_id, option_clicked)
options += "<label for='evening_{}'>Evening only</label>".format(guest_id)
option_clicked = " checked" if guest_attendance == "neither" else ""
options += "<br><input type='radio' name='attendance_{}' id='neither_{}' value='neither' onclick='attendance_changed(\"neither\", {})'{}/>".format(guest_id, guest_id, guest_id, option_clicked)
options += "<label for='neither_{}'>Unable to attend</label>".format(guest_id)
return options
def guest_label(guest):
guest_name = guest[1]
guest_id = guest[0]
guest_plus_one = guest[10]
if guest_plus_one == 1:
guest_name = "<b>Guest:</b> <input name='plus_one_{}' id='plus_one_{}' value='{}'/>".format(guest_id, guest_id, guest_name)
else:
guest_name = "<b>{}</b>".format(guest_name)
return guest_name
def guest_row(title, contents):
return "<tr><td valign='top'>{}</td><td valign='top'>{}</td></tr>".format(title, contents)
def attendance_form(code):
try:
rsvp = get_rsvp(code)
if rsvp is not None and len(rsvp) > 0 and len(rsvp[0]) > 0:
rsvp_id = rsvp[0][0]
guests = get_guests(rsvp_id)
table = "<table>"
for guest in guests:
table += guest_attendance(guest)
table += "<br><br>"
table += "</table>"
return table
else:
return "Invalid code"
except Error as e:
return "An error occurred, please reload the page."
def menu_item(item, value, course, guest_id, selected = False):
item_id = "{}_{}".format(value, guest_id)
item_name = "{}_{}".format(course, guest_id)
checked = "checked" if selected else ""
item_checkbox = "<input type='radio' name='{}' id='{}' value='{}' {}/>".format(item_name, item_id, value, checked)
item_label = " <label for='{}'>{}</label>".format(item_id, item)
return guest_row(item_checkbox, item_label)
def menu_table(guest):
guest_name = guest[1].split(" ")[0]
guest_attending = guest[3]
guest_evening = guest[8]
guest_id = guest[0]
guest_starter = guest[4]
guest_main = guest[5]
guest_dessert = guest[6]
guest_dietary = guest[7]
child = guest[9]
guest_attendance = "day"
if guest_attending == 0 or guest_evening == 1:
guest_attendance = "other"
if child == 1:
table = "<b>{}</b>".format(guest_name)
table += "<div id='choices_{}'>Kid's Menu</div>".format(guest_id)
table += "<div id='evening_only_{}' style='display: none;'>None</div>".format(guest_id)
return table
if guest_starter == None: guest_starter = "soup"
if guest_main == None: guest_main = "chicken"
if guest_dessert == None: guest_dessert = "crumble"
options_selected = "" if guest_main != 'vegan' else " checked"
options = "<input type='checkbox' id='vegan_{}' name='vegan_{}' onclick='vegan_selected({})'{}/> <label for='vegan_{}'>Vegan</label>".format(guest_id, guest_id, guest_id, options_selected, guest_id)
menu_display = "" if guest_main != 'vegan' else " style='display: none;'"
menu = "<table id='menu_{}'{}>".format(guest_id, menu_display)
menu += guest_row("<u>Starter<u/>", "")
menu += menu_item("Roasted tomato and basil soup. (v)", "soup", "starter", guest_id, (guest_starter == 'soup'))
menu += menu_item("Chicken and bacon terrine with mixed leaves.", "terrine", "starter", guest_id, (guest_starter == 'terrine'))
menu += guest_row("<u>Main<u/>", "")
menu += menu_item("Chicken stuffed with haggis, fondant potato, carrots, whisky sauce.", "chicken", "main", guest_id, (guest_main == 'chicken'))
menu += menu_item("Steak pie with roast potatoes and seasonal vegetables.", "steak", "main", guest_id, (guest_main == 'steak'))
menu += menu_item("Vegetarian main course. (v)", "vegetarian", "main", guest_id, (guest_main == 'vegetarian'))
menu += guest_row("<u>Dessert</u>", "")
menu += menu_item("Roasted apple crumble tart, vanilla cream, almond tuille. (v)", "crumble", "dessert", guest_id, (guest_dessert == 'crumble'))
menu += menu_item("Sticky toffee pudding and vanilla ice cream. (v)", "pudding", "dessert", guest_id, (guest_dessert == 'pudding'))
menu += "</table>"
div_display = "" if guest_attendance != 'day' else " style='display: none;'"
table_display = "" if guest_attendance == 'day' else " style='display: none;'"
table = "<b>{}</b>".format(guest_name)
table += "<div id='evening_only_{}'{}>None</div>".format(guest_id, div_display)
table += "<table id='choices_{}'{}>".format(guest_id, table_display)
table += "<tr><td colspan='2'>{}</td></tr>".format(options)
table += "<tr><td colspan='2'>{}</td></tr>".format(menu)
table += guest_row(" ", " ")
table += "<tr><td colspan='2'>Dietary Requirements: <input type='text' name='dietary_{}' id='dietary_{}' value='{}'/></td></tr>".format(guest_id, guest_id, guest_dietary)
table += "<table>"
return table
def menu_choices(code):
try:
rsvp = get_rsvp(code)
if rsvp is not None and len(rsvp) > 0 and len(rsvp[0]) > 0:
rsvp_id = rsvp[0][0]
guests = get_guests(rsvp_id)
table = ""
for guest in guests:
table += menu_table(guest)
table += "<br><br>"
return table
except Error as e:
return "An error has occurred, please reload the page."
def code_value(code):
html = code.upper()
html += " <input type='hidden' name='code' id='code' value='{}'/>".format(code.upper())
return html
def day_guest_form(code):
print("<form method='post' action='submitday.py'>")
print("<table cellspacing='10'>")
print(guest_row("<b>Your Code</b>", code_value(code)))
print(guest_row("<b>Guests</b>", attendance_form(code)))
print(guest_row(" ", " "))
print(guest_row("<b>Menu Choices</b>", menu_choices(code)))
print("<tr><td><button type='submit'>Submit</button></td><td></td></tr>")
print("</table>")
print("</form>")
print("Content-Type: text/html;charset=utf-8")
print()
print("<html>")
print("<head>")
print("<title>Laird / Hackett Wedding - RSVP</title>")
print('<link rel="stylesheet" href="./wedding.css" type="text/css" />')
print("<script src='./rsvp.js'></script>")
print("</head>")
print("<body>")
print("<h1>RSVP</h1>")
args = cgi.FieldStorage()
if 'code' in args and len(args['code'].value) == 4 and args['code'].value is not '----':
code = args['code'].value
day_guest_form(code)
else:
print("<form method='post'>")
print("Enter your 4 letter code: <input name='code'/>")
print("<br><button type='submit'>Submit</button>")
print("</form>")
print("<br>")
print("<b>OR</b>")
print("<br><br>")
print("<form action='./evening.py'>")
print("<button type='submit'>I don't have a code</button>")
print("</form>")
print("</body>")