/
appInit.py
661 lines (560 loc) · 21.7 KB
/
appInit.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
from extensions import db, migrate, csrf, ma, heroku, login_manager, mail
def create_app():
import os
from os import environ
import psycopg2
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_heroku import Heroku
from flask_admin.contrib.sqla import ModelView
from flask_wtf.csrf import CSRFProtect
from flask import render_template, request, redirect, jsonify, url_for, flash, session
from sqlalchemy.orm import relationship, sessionmaker
from models import User, Order, Customer, Role, OrderDetails, Product, Supplier, Payment, Shipper, users_schema, orders_schema, products_schema, customers_schema, order_details_schema, suppliers_schema, payments_schema, shippers_schema, MyAdminIndexView, UserAdmin, RoleAdmin
from flask_admin import Admin
from flask_login import UserMixin, LoginManager, current_user, login_user, logout_user
from flask_security import SQLAlchemyUserDatastore, Security, utils, login_required
from wtforms.fields import PasswordField
from passlib.hash import pbkdf2_sha256
from flask_marshmallow import Marshmallow
import stripe
import datetime
from flask_mail import Mail, Message
from decimal import Decimal
from flask_caching import Cache
from flask_assets import Environment, Bundle
from PIL import Image
APPLICATION_NAME = "Kokeshi"
app = Flask(__name__)
csrf.init_app(app)
app.config.from_pyfile('config_default.cfg')
assets = Environment(app)
try:
app.config.from_envvar('KOKESHI_SETTINGS')
except:
pass
# Flask Security Config
app.config['SECURITY_REGISTERABLE'] = True
# Flask-Mail Config
app.config.update(dict(
MAIL_SERVER=os.environ['MAIL_SERVER'],
MAIL_PORT=465,
MAIL_USERNAME=os.environ['MAIL_USERNAME'],
MAIL_USE_TLS=False,
MAIL_USE_SSL=True,
MAIL_PASSWORD=os.environ['MAIL_PASSWORD'],
SECURITY_PASSWORD_HASH='pbkdf2_sha512',
SECURITY_PASSWORD_SALT=os.environ['SECURITY_PASSWORD_SALT']
))
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
# Flask-Assets config
scss_bundle = Bundle('scss/*.scss',
filters='pyscss, cssmin',
output='css/styles.css',
extra={'rel': 'stylesheet/scss'},
)
js_bundle = Bundle('js/*.js',
filters='jsmin',
output='js/main.min.js',
)
assets.register('scss_all', scss_bundle)
assets.register('js_all', js_bundle)
scss_bundle.build()
js_bundle.build()
imagePath = "static/img/artisan-placeholder.jpg"
outputPath = "static/img/artisan-placeholder.webp"
quality = "80"
im = Image.open(imagePath)
im.save(outputPath, 'webp', quality=quality)
app.url_map.strict_slashes = False
# Initialize the SQLAlchemy data store and Flask-Security.
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
# Create admin views.
admin = Admin(app, index_view=MyAdminIndexView())
admin.add_view(UserAdmin(User, db.session))
admin.add_view(RoleAdmin(Role, db.session))
# Stripe payments implementation.
STRIPE_PUBLISHABLE_KEY = 'pk_test_GM1d2F2DgrIl6jnkIwSaZ8Dd'
stripe_keys = {
'secret_key': os.environ['STRIPE_SECRET_KEY'],
'publishable_key': STRIPE_PUBLISHABLE_KEY
}
stripe.api_key = stripe_keys['secret_key']
#################
# App functions #
#################
#######################
# User administration #
#######################
# Populate the db with products and placeholder data
"""@app.before_first_request
def before_first_request():
db.create_all()
user_datastore.find_or_create_role(
name='super', description='Super User')
user_datastore.find_or_create_role(
name='admin', description='Administrator')
user_datastore.find_or_create_role(
name='artisan', description='Artisan')
encrypted_password = utils.encrypt_password('password')
if not user_datastore.get_user('super@example.com'):
user_datastore.create_user(
email='super@example.com',
password=encrypted_password
)
if not user_datastore.get_user('admin@example.com'):
user_datastore.create_user(
email='admin@example.com',
password=encrypted_password
)
if not user_datastore.get_user('artisan@example.com'):
user_datastore.create_user(
email='artisan@example.com',
password=encrypted_password
)
db.session.commit()
user_datastore.add_role_to_user('super@example.com', 'super')
user_datastore.add_role_to_user('admin@example.com', 'admin')
user_datastore.add_role_to_user('artisan@example.com', 'artisan')
db.session.commit()
db_items = [
Product(
name="Zao Kokeshi",
description="A traditional handcrafted Japanese Kokeshi doll, made with care to the height of the newborn child",
price=200,
is_available=True
),
Product(
name="Yonezawa Kokeshi",
description="A traditional handcrafted Japanese Kokeshi doll, made with care to the height of the newborn child",
price=200,
is_available=True
),
Product(
name="Sagae Kokeshi",
description="A traditional handcrafted Japanese Kokeshi doll, made with care to the height of the newborn child",
price=200,
is_available=True
),
Product(
name="Tendo Kokeshi",
description="A traditional handcrafted Japanese Kokeshi doll, made with care to the height of the newborn child",
price=200,
is_available=True
),
Product(
name="Message",
description="A handwritten message on the back of the kokeshi doll",
price=50,
is_available=True
),
Supplier(
name="Sato",
phone="08011112222",
email="admin@example.com"
),
Supplier(
name="Suzuki",
phone="08011199002",
email="super@example.com"
),
Supplier(
name="Miyagi",
phone="090000009999",
email="artisan@example.com"
),
Shipper(
companyName="Kuro Neko",
phone="08051516161",
email="blackCat@example.com",
contactName="Kurosawa"
)
]
for item in db_items:
db.session.add(item)
db.session.commit()"""
@app.route('/login', methods=['GET', 'POST'])
def showLogin():
"""
Use a custom LoginForm to validate with WTForms
"""
form = LoginForm()
if form.validate_on_submit():
# Login and validate the user.
# user should be an instance of the `User` class
login_user(user)
flask.flash('Logged in successfully.')
next = flask.request.args.get('next')
# is_safe_url should check if the url is safe for redirects.
# See http://flask.pocoo.org/snippets/62/ for an example.
if not is_safe_url(next):
return flask.abort(400)
return flask.redirect(next or flask.url_for('index'))
return flask.render_template('login.html', form=form)
@app.route('/logout')
def showLogout():
logout_user()
flask.flash('Logged out successfully.')
return 'Logged out'
##########################
# General Administration #
##########################
@app.before_request
def force_https():
"""
Redirect from http to https
"""
if os.environ.get('DATABASE_URL') is not None:
if not request.is_secure:
url = request.url.replace('http://', 'https://', 1)
code = 301
return redirect(url, code=code)
##################
# JSON API calls #
##################
@app.route('/suppliers/JSON')
@login_required
def showSuppliersJSON():
"""
Return order data in JSON
"""
suppliers = Supplier.query.all()
return jsonify(suppliers_schema.dump(suppliers).data)
@app.route('/shippers/JSON')
@login_required
def showShippersJSON():
"""
Return order data in JSON
"""
shippers = Shipper.query.all()
return jsonify(shippers_schema.dump(shippers).data)
@app.route('/products/JSON')
@login_required
def showProductsJSON():
"""
Return order data in JSON
"""
products = Product.query.all()
return jsonify(products_schema.dump(products).data)
@app.route('/orders/JSON')
@login_required
def showOrdersJSON():
"""
Return order data in JSON
"""
orders = Order.query.all()
return jsonify(orders_schema.dump(orders).data)
@app.route('/orders/unfulfilled/JSON')
@login_required
def showUnfulfilledOrdersJSON():
"""
Return unfulfilled order data in JSON
"""
unful_orders = Order.query.filter_by(wasOrdered=False).all()
return jsonify(orders_schema.dump(unful_orders).data)
@app.route('/customers/JSON')
@login_required
def showCustomersJSON():
"""
Return customer data in JSON
"""
customers = Customer.query.all()
return jsonify(customers_schema.dump(customers).data)
#######################
# Client facing pages #
#######################
@app.route('/orders')
@login_required
def showOrders():
"""
Display the supplier - facing orders page. The supplier can accept
the job, then is directed to the showAcceptOrder
"""
unaccepted_orders = Order.query.filter_by(wasAccepted=False).all()
session['supplier'] = current_user.email
return render_template('auth/unassigned_orders.html', orders=unaccepted_orders)
@app.route('/orders/<int:order_id>/accepted', methods=['GET', 'POST'])
@login_required
def showAcceptOrder(order_id):
"""
Display the supplier-facing order accepted page.
"""
order = Order.query.filter_by(orderID=order_id).first()
supplier = Supplier.query.filter_by(
email=session['supplier']).first()
selected_order = Order.query.filter_by(orderID=order_id).first()
selected_order_details = OrderDetails.query.filter_by(
order_ID=order_id).first()
supplier_id = supplier.supplierID
order.wasAccepted = True
selected_order.supplier_ID = supplier_id
db.session.add(selected_order)
db.session.commit()
return render_template('auth/selected_order.html', order=selected_order_details)
@app.route('/', methods=["GET", "POST"])
@app.route('/home')
@cache.cached(timeout=86400)
def showHome():
"""
Display the landing page.
"""
return render_template('home.html')
@app.route('/about')
@cache.cached(timeout=86400)
def showAbout():
"""
Display the About Us page.
"""
return render_template('about.html')
@app.route('/design', methods=['GET', 'POST'])
@cache.cached(timeout=86400)
def showDesign():
"""
Display a kokeshi designing page that, when submitted, updates the shopping cart with the order and redirects to the order page.
"""
# Create a new cart list.
if 'cart' not in session:
session['cart'] = []
if request.method == 'POST':
# Create a customer object without an email, to attach to the cart
# items in the session.
customer = Customer(email="")
db.session.add(customer)
db.session.commit()
# Create an order object and tie it to the customer.
order = Order(customer_ID=customer.customerID)
db.session.add(order)
db.session.commit()
product = Product.query.filter_by(
name=request.form['item']).one()
# Assign the order to the customer using the orderID
customer.order_ID = order.orderID
if request.form.get('is-message', False) == 'on':
order_details = OrderDetails(
item=request.form['item'],
name=request.form['name'],
dob=request.form['dob'],
height=request.form['height'],
weight=request.form['weight'],
is_message=True,
message=request.form['message'],
order_ID=order.orderID,
customer_ID=customer.customerID,
product_ID=product.productID
)
else:
order_details = OrderDetails(
item=request.form['item'],
name=request.form['name'],
dob=request.form['dob'],
height=request.form['height'],
weight=request.form['weight'],
is_message=False,
order_ID=order.orderID,
customer_ID=customer.customerID,
product_ID=product.productID
)
# Set the price of the item, dependent on the presence or absence of a message.
if request.form.get('is-message', False) == 'on':
price = 250
else:
price = 200
order.price = price
db.session.add(customer)
db.session.add(order)
db.session.add(order_details)
db.session.commit()
# Append the item to the cart.
session['cart'].append(
{
'itemID': order_details.orderDetailsID,
'item': order_details.item,
'name': order_details.name,
'dob': order_details.dob,
'height': order_details.height,
'weight': order_details.weight,
'isMessage': order_details.is_message,
'message': order_details.message,
'product': product.name,
'price': price,
'orderID': order_details.order_ID
}
)
# Create a session variable to select the customer in order to append information.
session['customer_ID'] = customer.customerID
flash("Success! Your order for '%s kokeshi' has been added to your cart." %
order_details.name)
return redirect(url_for('showOrder'))
else:
return render_template('design.html')
@app.route('/setCart')
def setCart():
cartObj = session['cart']
cartJSON = jsonify(cartObj)
return cartJSON
@app.route('/removeItem/<int:item_id>', methods=["GET", "POST"])
def removeItem(item_id):
"""
Remove the selected item from the cart.
"""
# Check for the existence of an item, then convert it to an int
if item_id is not None:
item_id = int(item_id)
try:
session['cart'][:] = [d for d in session['cart']
if d.get('itemID') != item_id]
except:
msg = "There were no items to remove"
print(msg)
return redirect(url_for('showOrder'))
@app.route('/order', methods=["GET", "POST"])
def showOrder():
"""
Display the order page - - a list of all the items in the cart.
"""
return render_template('order.html')
@app.route('/checkout', methods=['GET', 'POST'])
def showCheckout():
"""
Display the checkout page, which displays the total and a Stripe payments button.
"""
amount_usd = 0
# Add up the total of all the items in the cart
for item in session['cart']:
amount_usd += item['price']
# Calculate the amount in US cents for Stripe
amount_cents = amount_usd * 100
return render_template(
'auth/index.html',
key=stripe_keys['publishable_key'],
amount_usd=amount_usd,
amount_cents=amount_cents,
cart=session['cart']
)
@app.route('/charge', methods=['GET', 'POST'])
def charge():
db_customer = Customer.query.filter_by(
customerID=session['customer_ID']).one()
# Amount in cents
amount = 0
items = []
for item in session['cart']:
# Add the item prices in cents.
amount += item['price'] * 100
items.append(item['item'])
customer = stripe.Customer.create(
email=request.form['stripeEmail'],
source=request.form['stripeToken']
)
# Create a session variable with the customer's email for sending a
# confirmation email.
session['customer_email'] = request.form['stripeEmail']
# Create a Stripe charge object which sends a confirmation email.
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='KokeMama Charge',
receipt_email=session['customer_email']
)
# Attempt to add customer data from the stripe input to the customer
# object
try:
db_customer.email = request.form['stripeEmail']
except:
print("There is no 'stripeEmail' key")
try:
db_customer.name = charge.source.name
except:
print("There is no 'stripeName' key")
try:
db_customer.address1 = charge.source.address_line1
except:
print("There is no 'stripeShippingAddressLine1' key")
try:
db_customer.zipCode = charge.source.address_zip
except:
print("There is no 'stripeShippingAddressZip' key")
try:
db_customer.state = charge.source.address_state
except:
print("There is no 'stripeShippingAddressState' key")
try:
db_customer.city = charge.source.address_city
except:
print("There is no 'stripeShippingAddressCity' key")
try:
db_customer.country = charge.source.address_country
except:
print("There is no 'stripeShippingAddressCountry' key")
db.session.add(db_customer)
db.session.commit()
return redirect(url_for('showConfirm'))
@app.route('/confirmation')
def showConfirm():
"""
Display the order confirmation page after an order is submitted.
"""
# Get the customer from the db using the 'customer_ID' session variable
db_customer = Customer.query.filter_by(
customerID=session['customer_ID']).one()
# Create a list of the cart items for use in the email's message body
items = [dic['item'] for dic in session['cart'] if 'item' in dic]
# Use the first item in the cart to obtain the 'orderID'. If None,
# display 'design' link
try:
firstItem = session['cart'][0]
orderID = firstItem['orderID']
msg = Message(
'Confirmation', sender='administrator@peraperaexchange.com', recipients=[session['customer_email']])
msg.body = "Thank you for your order of: %s. Your order number is: %d." % (
items, orderID)
mail.send(msg)
# Clear the cart after payment is received and confirmation is sent.
session['cart'] = []
return render_template('auth/confirmation.html')
except:
return redirect(url_for('showHome'))
@app.route('/contact', methods=['GET', 'POST'])
@cache.cached(timeout=86400)
def showContact():
"""
Display the contact information page.
"""
if request.method == 'POST':
msg = Message(
'Contact', sender='administrator@peraperaexchange.com', recipients=['administrator@peraperaexchange.com'])
msg.body = "Customer name: %s\n" % (
request.form['customer-name'])
msg.body += "Customer email: %s\n" % (
request.form['customer-email'])
msg.body += "Message: %s" % (request.form['customer-message'])
mail.send(msg)
customer = Customer(
name=request.form['customer-name'],
email=request.form['customer-email']
)
db.session.add(customer)
db.session.commit()
return redirect(url_for('showContactComplete'))
else:
return render_template('contact.html')
@app.route('/contact/complete')
def showContactComplete():
"""
Show a success message for the contact form.
"""
return render_template('auth/contact_complete.html')
db.init_app(app)
login_manager.init_app(app)
migrate.init_app(app, db)
ma.init_app(app)
heroku.init_app(app)
mail.init_app(app)
# Load the current logged in user.
@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)
return app