-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
140 lines (108 loc) · 4.66 KB
/
flask_app.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
import secrets
import sqlite3
import string
from pathlib import Path
import requests
from flask import Flask, jsonify, request, render_template
app = Flask(__name__)
def generate_uid(db_id):
alphabet = string.ascii_lowercase + string.digits
return f"{db_id}-" + ''.join(secrets.choice(alphabet) for _ in range(8))
def dict_factory(cursor, row):
dic = dict()
for idx, col in enumerate(cursor.description):
dic[col[0]] = row[idx]
return dic
def db_connect():
connection = sqlite3.connect('database.db')
connection.row_factory = dict_factory
cursor = connection.cursor()
return cursor, connection
@app.route('/')
def home():
cur, conn = db_connect()
cards = cur.execute('SELECT * FROM card;').fetchall()
conn.close()
return render_template("index.html", cards=cards)
@app.route('/about')
def about():
return render_template("about.html")
@app.route('/cards')
def list_cards():
cur, conn = db_connect()
cards = cur.execute('SELECT * FROM card;').fetchall()
conn.close()
return render_template("cards.html", cards=cards)
@app.route('/wish/<card_id>')
def wish_form(card_id):
cur, conn = db_connect()
card = cur.execute("SELECT * FROM card WHERE cardid=?", (card_id,)).fetchone()
conn.close()
return render_template("send_wish.html", card=card)
def send_mail(email, sender,receiver, cardid, uid):
card_img = f"http://zappsters.pythonanywhere.com/static/{cardid}.jpg"
banner_img = f"http://zappsters.pythonanywhere.com/static/mail_banner.jpeg"
logo_img = f"http://zappsters.pythonanywhere.com/static/logo_mail.png"
git_img = f"http://zappsters.pythonanywhere.com/static/git.png"
x = requests.post("https://api.mailgun.net/v3/sandboxa39931aba4ea43a885c240d815b0a2c2.mailgun.org/messages",
auth=("api", "9374b99615d0f43ff1e12995ef3c3317-8d821f0c-6df1ae28"),
files=[('inline[0]', ('card.jpg', open(Path.cwd() / f'static/{cardid}.jpg', mode='rb').read()))],
data={"from": "[email protected]",
"to": [f"{email}"],
"subject": "Greeting card",
"html": render_template("mail.html", sender=sender, code=uid, receiver=receiver,
card=card_img, banner=banner_img, logo=logo_img, git=git_img)})
return x.text
@app.route('/wish_insert', methods=['POST'])
def wish_insert():
sender = request.form.get("sender")
message = request.form.get("message")
receiver = request.form.get("receiver")
card_id = request.form.get("card_id")
send_method = request.form.get("send_method")
send_destination = request.form.get("send_destination")
print("s: ", sender, "r: ", receiver, "m: ", message, "id: ", card_id, "send: ", send_method, "dest: ",
send_destination)
cur, conn = db_connect()
max_id = cur.execute('select MAX(wishid) as wishid from wish;').fetchone()['wishid']
if max_id is None:
db_id = 1
else:
db_id = max_id + 1
uid = generate_uid(db_id)
if send_method == "email":
send_mail(send_destination, sender,receiver, card_id, uid)
card = cur.execute("SELECT * FROM card WHERE cardid=?", (card_id,)).fetchone()
print(card)
conn.execute(
"INSERT INTO wish (uid, sender, message, cardid) VALUES (?,?,?,?)", (uid, sender, message, card_id))
conn.commit()
conn.close()
return render_template("confirm_wish.html", card=card)
@app.route('/get_wish')
def get_wish():
return render_template("get_wish.html")
@app.route('/show_wish', methods=['POST'])
def result_wish():
uid = request.form.get("uuid")
cur, conn = db_connect()
personal_card = cur.execute("SELECT * FROM wish WHERE uid=?", (uid,)).fetchone()
conn.close()
return render_template("show_wish.html", card=personal_card)
# extra route for our API to request the personal message
# use the http get method in the web request
@app.route('/api/get_wish', methods=['GET'])
def get_personal_wish():
# the function has one parameter, the unique code
# get the value of this parameter
code = request.args.get('code')
# connect and open the database file database.db
cur, conn = db_connect()
# read the associated personal wish, you will need an extra integer field code in your wish table!
wish = cur.execute("SELECT * FROM wish WHERE code=?", (code,)).fetchall()
conn.close()
# there's only one wish because the code is unique
response = jsonify(wish[0])
# allow cross-domain Ajax requests, more info in later years
response.headers.add("Access-Control-Allow-Origin", "*")
return response