-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
314 lines (246 loc) · 11.5 KB
/
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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Copyright (c) 2018 Cole Nixon
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# pip/Homebrew imports
from flask import Flask, request, render_template, flash, redirect, url_for, session, logging
from flask_mysqldb import MySQL
from wtforms import Form, StringField, IntegerField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
from functools import wraps
import requests, sys, json, re
# Personal imports
from myForms import PageForm, CommentForm, RegisterForm, BookForm, OrderForm
from keys import pwinty_id, pwinty_key, maps_key
if len(sys.argv) != 2:
print("\tThis program expects a single argument, <databaseName> to be passed in.\n")
print("\tPlease execute with format :\t python app.py <databaseName>")
sys.exit()
pwinty_headers = {'Content-type': 'application/json',
'accept': 'application/json',
'X-Pwinty-MerchantId': pwinty_id,
'X-Pwinty-REST-API-Key': pwinty_key
}
baseurl = "https://sandbox.pwinty.com/v3.0"
order_p = "/orders"
image_p = "/orders/{}/images"
order_status_p = "/orders/{}/SubmissionStatus"
order_submission_p = "/orders/{}/status"
app = Flask(__name__)
# Config MySQL
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_DB'] = sys.argv[1]
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# Init MySQL
mysql = MySQL(app)
start_up = 0
# Wraps for Access control
def user_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please Login', 'danger')
return redirect(url_for('login'))
return wrap
@app.route('/')
def index():
global start_up
if start_up == 0:
cursorOnInit = mysql.connection.cursor()
try:
cursorOnInit.execute("SELECT * from USERS")
except:
cursorOnInit.execute(
"CREATE TABLE users(id INT(11) auto_increment primary key, email VARCHAR(100), username VARCHAR(30), password VARCHAR(100), register_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP)")
cursorOnInit.execute(
"CREATE TABLE books(id INT(11) auto_increment primary key, title VARCHAR(100), bio TEXT, author VARCHAR(50), owner VARCHAR(100), userId INT(11), FOREIGN KEY(userId) REFERENCES users(id) ON DELETE CASCADE, date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP)")
cursorOnInit.execute(
"CREATE TABLE comments(id INT(11) auto_increment primary key, page INT(6) NOT NULL, body TEXT, owner VARCHAR(100), userId INT(11), FOREIGN KEY(userId) REFERENCES users(id) ON DELETE CASCADE, bookId INT(11), FOREIGN KEY(bookId) REFERENCES books(id) ON DELETE CASCADE, date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP, lat FLOAT(10,7), lng FLOAT(10,7)) ")
start_up = 1
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
# All books a user has control over
@app.route('/books')
@user_logged_in
def books():
# Get the books
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM books WHERE userId = %s", [session['userId']])
if result > 0:
allbooks = cur.fetchall()
return render_template('booksHome.html', allbooks=allbooks)
else:
msg = 'No Books for you!'
return render_template('booksHome.html', msg=msg)
# Close it
cur.close()
# Page for a book
@app.route('/books/<int:id>', methods=['GET', 'POST'])
@user_logged_in
def book(id):
# Set variable for page to filter comments
currentPage = 0
pageForm = PageForm(request.form)
if request.method == 'POST' and pageForm.validate():
currentPage = pageForm.currentPage.data
# grab comments from MySQL
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM books WHERE id = %s", [id])
if result > 0:
book = cur.fetchone()
result = cur.execute("SELECT * FROM comments WHERE bookId = %s ORDER BY page", [book['id']])
if result > 0:
comments = cur.fetchall()
return render_template('book.html', book=book, comments=comments, pageForm=pageForm,
currentPage=currentPage, maps_key=maps_key)
else:
msg = "No comments here yet! Be the first :)"
comments = ''
return render_template('book.html', book=book, msg=msg, comments=comments, pageForm=pageForm,
currentPage=currentPage, maps_key=maps_key)
else:
error = "There is no Book with this ID"
return render_template('book.html', error=error, maps_key=maps_key)
# Cant forget!
cur.close()
# Add a comment
@app.route('/book/<int:id>/add_comment', methods=['GET', 'POST'])
@user_logged_in
def add_comment(id):
form = CommentForm(request.form)
if request.method == 'POST' and form.validate():
page = form.page.data
body = form.body.data
coords = re.sub('[()]', '', form.location.data)
latLng = coords.split(', ')
# Create cursor
cur = mysql.connection.cursor()
cur.execute("INSERT INTO comments(page, body, owner, userId, bookId, lat, lng) VALUES(%s, %s, %s, %s, %s, %s, %s)",
(int(page), body, session['username'], int(session['userId']), int(id), float(latLng[0]), float(latLng[1])))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('Comment Added', 'success')
return redirect(url_for('book', id=id))
return render_template('add_comment.html', form=form, maps_key=maps_key)
# Login
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# Get the form fields
username = request.form['username']
password_attempt = request.form['password']
# Get user by username
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM users WHERE username = %s", [username])
if result > 0:
data = cur.fetchone()
password = data['password']
if sha256_crypt.verify(password_attempt, password):
session['logged_in'] = True
session['username'] = username
session['userId'] = data['id']
flash('You are logged in', 'success')
return redirect(url_for('index'))
else:
error = "Invalid Login"
return render_template('login.html', error=error)
cur.close()
else:
app.logger.info('NO USER')
error = "Invalid Login"
return render_template('login.html', error=error)
return render_template('login.html')
# User Registration
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
# register user
email = form.email.data
username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))
# Create cursor
cur = mysql.connection.cursor()
cur.execute("INSERT INTO users(email, username, password) VALUES(%s, %s, %s)", (email, username, password))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('You are now registered and can log in.', 'success')
return redirect(url_for('login'))
return render_template('register.html', form=form)
@app.route('/add_book', methods=['GET', 'POST'])
@user_logged_in
def add_book():
form = BookForm(request.form)
if request.method == 'POST' and form.validate():
title = form.title.data
author = form.author.data
bio = form.bio.data
# Create cursor
cur = mysql.connection.cursor()
cur.execute("INSERT INTO books(title, author, bio, owner, userId) VALUES(%s, %s, %s, %s, %s)",
(title, author, bio, session['username'], int(session['userId'])))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('Book Added', 'success')
return redirect(url_for('books'))
return render_template('add_book.html', form=form)
@app.route('/qr/<int:id>', methods=['GET', 'POST'])
@user_logged_in
def qrdl(id):
form = OrderForm(request.form)
if request.method == 'POST' and form.validate():
order_data = {'countryCode': 'US',
'recipientName': form.name.data,
'address1': form.address.data,
'addressTownOrCity': form.address.data,
'stateOrCounty': form.state.data,
'postalOrZipCode': form.zip.data,
'preferredShippingMethod': 'Budget'}
order_resp = requests.post(baseurl + order_p, headers=pwinty_headers, json=order_data)
if order_resp.status_code == 200:
order_id = json.loads(order_resp.text)['data']['id']
image_path = image_p.format(order_id)
image_data = {'orderId': order_id,
'sku': "M-STI-3X4",
'copies': 1,
'size': 'ShrinkToFit',
'url': 'https://api.qrserver.com/v1/create-qr-code/?data=http://localhost:5000/book/'+str(id)+'size=600x600'
}
image_resp = requests.post(baseurl+image_path, headers=pwinty_headers, json=image_data)
if image_resp.status_code == 200:
order_status_path = order_status_p.format(order_id)
status_resp = requests.get(baseurl+order_status_path, headers=pwinty_headers)
if json.loads(status_resp.text)['data']['isValid'] == True:
# finalize order
order_submission_path = order_submission_p.format(order_id)
wow = requests.post(baseurl+order_submission_path, headers=pwinty_headers, json={'status': 'Submitted'})
if wow.status_code == 200:
flash('Order Successfully Submitted. Order ID: '+str(order_id), 'success')
return redirect(url_for('books'))
return render_template('order_sticker.html', form=form)
# Redirect to the home...
@app.route('/logout')
@user_logged_in
def logout():
session.clear()
flash('Successfully logged out', 'success')
return redirect(url_for('login'))
# Actual Script...
if __name__ == '__main__':
app.secret_key = 'secret123'
app.run(debug=True) # Debug mode turns on automatic server refresh on save