-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
576 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import secrets | ||
import sqlite3 | ||
import string | ||
from pathlib import Path | ||
|
||
import requests | ||
import os | ||
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(4)) | ||
return f"{db_id}-" + ''.join(secrets.choice(alphabet) for _ in range(8)) | ||
|
||
|
||
def dict_factory(cursor, row): | ||
|
@@ -27,7 +27,6 @@ def db_connect(): | |
cursor = connection.cursor() | ||
return cursor, connection | ||
|
||
|
||
@app.route('/') | ||
def home(): | ||
cur, conn = db_connect() | ||
|
@@ -52,19 +51,25 @@ def list_cards(): | |
@app.route('/wish/<card_id>') | ||
def wish_form(card_id): | ||
cur, conn = db_connect() | ||
card = cur.execute(f'SELECT * FROM card WHERE cardid={card_id};').fetchone() | ||
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, cardid, uid): | ||
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(f'{os.getcwd()}/static/{cardid}.jpg', mode='rb').read()))], | ||
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", | ||
"text": f"You have been sent an AR greeting card from {sender}! The unique code for your personnel message i: " + uid}) | ||
"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 | ||
|
||
|
||
|
@@ -89,11 +94,12 @@ def wish_insert(): | |
uid = generate_uid(db_id) | ||
|
||
if send_method == "email": | ||
send_mail(send_destination,sender,card_id, uid) | ||
send_mail(send_destination, sender,receiver, card_id, uid) | ||
|
||
card = cur.execute('SELECT * FROM card WHERE cardid' + '=' + str(card_id) + ';').fetchone() | ||
card = cur.execute("SELECT * FROM card WHERE cardid=?", (card_id,)).fetchone() | ||
print(card) | ||
conn.execute( | ||
f"INSERT INTO wish (uid, sender, message, cardid) VALUES (\'{uid}\', \'{sender}\', \'{message}\', {card_id})") | ||
"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) | ||
|
@@ -108,7 +114,7 @@ def get_wish(): | |
def result_wish(): | ||
uid = request.form.get("uuid") | ||
cur, conn = db_connect() | ||
personal_card = cur.execute(f"SELECT * FROM wish WHERE uid=\'{uid}\';").fetchone() | ||
personal_card = cur.execute("SELECT * FROM wish WHERE uid=?", (uid,)).fetchone() | ||
conn.close() | ||
return render_template("show_wish.html", card=personal_card) | ||
|
||
|
@@ -124,11 +130,11 @@ def get_personal_wish(): | |
# 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(f"SELECT * FROM wish WHERE code={code}").fetchall() | ||
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 | ||
return response |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.