forked from abdallahsoliman/FRN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
115 lines (101 loc) · 3.85 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
from flask import Flask, render_template, request
from settings import Common
from flask_mail import Mail, Message
from smtplib import SMTPException
import yaml, os, re
import stripe
application = Flask(__name__)
application.config.from_object(Common)
mail = Mail(application)
stripe.api_key = application.config['STRIPE_KEYS']['secret']
@application.route("/")
def index():
sponsors = get_config("sponsors")
events = get_config("events")
stripeKey = application.config['STRIPE_KEYS']['publishable']
gallery = get_gallery()
print(gallery)
return render_template(
"index.html",
sponsors=sponsors,
events=events,
stripeKey=stripeKey,
gallery=gallery,
)
@application.route("/charge", methods=['POST'])
def donate():
try:
stripe.Charge.create(
amount = request.form['amount'],
currency= 'usd',
source = request.form['token'],
description="Donation to CWRU Food Recovery Network from %s" % request.form['email']
)
status = "success"
message= "Your donation has been processed. Thank you for your support!"
except stripe.CardError as e:
status = "danger"
message = e.json_body['message']
except (stripe.APIConnectionError, stripe.APIError, stripe.AuthenticationError, stripe.InvalidRequestError, stripe.RateLimitError) as e:
status = "danger"
message = "An error occurred while trying to process your donation. Please try again at another time."
return render_template('base/alert.html', message=message, status=status)
def get_config(filename):
"""
Retrieves contents of yaml configuration file
Config directory is "FRN/config", file format is yml
:param filename: name of the yaml file holding the desired configuration
:return: contents of the file
"""
filename = "%s.yaml" % filename if re.search('_*\.yaml$', filename) is None else filename
data = []
with open(os.path.join(application.config['CONFIG_DIR'], filename)) as f:
for item in yaml.load_all(f):
data.append(item)
return data
def get_gallery():
gallery = []
for file in os.listdir(application.config['GALLERY_DIR_ABSOLUTE']):
if file.endswith(".png") or file.endswith(".jpg") or file.endswith(".jpeg"):
gallery.append(os.path.join(application.config['STATIC_DIR'], application.config['GALLERY_DIR'], file))
return gallery
@application.context_processor
def group_rows():
def _group_rows(data, n):
"""
Groups data into rows with n items each
:param data: data to group
:param n: number of items per row
:return: data grouped into rows with n items each
"""
result = []
for i, item in enumerate(data):
if i % n == 0:
result.append(list())
result[-1].append(item)
return result
return dict(group_rows=_group_rows)
@application.context_processor
def is_last():
def _is_last(item, data):
return data[-1] == item
return dict(is_last=_is_last)
@application.route('/mail', methods=['POST'])
def send_mail():
msg = Message(
subject="%s from %s via soliman.io" % (request.form['subject'], request.form['name']),
sender=request.form['email'],
recipients=application.config['MAIL_RECIPIENTS'],
body=request.form['message']
)
try:
mail.send(msg)
message = "Thank you for reaching out, we will get back to you as soon as possible."
status="success"
except SMTPException as ex:
print(ex)
message = "Sorry, we are having trouble sending your email at the moment."
status = "danger"
return render_template('base/alert.html', message=message, status=status)
if __name__ == "__main__":
application.run(host="0.0.0.0", port=8000)