-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
171 lines (155 loc) · 5.6 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
from flask import Flask, redirect
from flask import request, render_template, send_file, request
import config
from bigchaindb_driver import BigchainDB
from bigchaindb_driver.crypto import generate_keypair
from mongoUtil import *
from utils import bdb_donate, bdb_pay, add_transaction_to_collection, get_transactions, get_transaction_by_id
from pymongo import MongoClient
import json
import sys
from random import *
import random
app = Flask(__name__,static_url_path='/static')
blockchain_db = BigchainDB(config.BLOCKCHAIN_URL)
client = MongoClient(config.MONGO_HOST, 27017).bitdonate
user = generate_keypair()
countries=['Algeria', 'Bahrain', 'Egypt', 'Iran', 'Iraq', 'Palestine', 'Jordan', 'Kuwait', 'Lebanon', 'Libya', 'Morocco', 'Oman', 'Qatar', 'Saudi Arabia', 'Syria', 'Tunisia', 'United Arab Emirates', 'Yemen', 'Ethiopia' , 'Sudan']
@app.route('/')
def index():
return render_template('index.html')
@app.route('/donate', methods=['GET', 'POST'])
def donate():
"""
Take a donater name and an amount and put a transaction into the blockchain
"""
if request.method == 'GET':
return render_template('donate.html')
elif request.method == 'POST':
name = request.form.get('name').split()
first = name[0]
try:
last = name[1]
except IndexError:
last = ""
cc=request.form.get('cc')
try:
email = request.form['email']
except KeyError:
email = '[email protected]'
amount= request.form.get('amount')
country = countries[randrange(len(countries))]
donater_name=first+"_"+last+"_"+email
sent_txid = bdb_donate(blockchain_db, user, donater_name, amount)
userId = addDonation(client,first,last,email,sent_txid,country)
add_transaction_to_collection(client, 'donate', sent_txid)
return redirect("/user_donations?id={}".format(userId))
@app.route('/pay', methods=['GET', 'POST'])
def pay():
"""
Take a vendor name and an amount and put that expenditure transaction into the blockchain
"""
if request.method == 'GET':
return render_template('pay.html')
elif request.method == 'POST':
vendor_name = request.form.get('vendor_name')
amount = request.form.get('amount')
item = request.form.get('item')
print(vendor_name)
print(amount)
sent_txid = bdb_pay(blockchain_db, user, vendor_name, amount, item)
add_transaction_to_collection(client, 'pay', sent_txid)
print("added transaction id to mongo")
return redirect('/portal')
@app.route('/user_donations', methods=['GET'])
def userDonations():
if request.args.get('id'):
donations=getDonersAllDonations(client,request.args.get('id'))
tids=list(map(lambda x: x['tid'],donations))
transactoions=list(map(lambda x: get_transaction_by_id(blockchain_db,x), tids))
amounts= list(map(lambda x: x['amount'],transactoions))
amounts= list(map(lambda x: int(x),amounts))
total=0
benifit=["you Helped with the cost of buying pens for children","you Helped with the cost of buying bags for children",'you Helped with the cost of buying computers for chilren','you Helped with the cost of going on a trip']
for amount in amounts:
total+=amount
DonationsList=list()
dates=list()
amount=list()
for i in range(len(amounts)):
DonationsList.append({"amount": amounts[i],
"timestamp":donations[i]['timestamp'],
'benefit': random.choice(benifit)
})
dates.append(donations[i]['timestamp'])
return render_template('user.html',total=total,list=DonationsList)
# return "total: {} \n donations: \n you donated in {}".format(total,DonationsList)
return redirect("/")
@app.route('/donate_transactions', methods=['GET'])
def donate_transactions():
"""
Shows all the donations made to the charity
"""
collection = client.donate_transactions
tx_list = get_transactions(client, blockchain_db, 'donate')
sum = 0
for tx in tx_list:
try:
sum += int(tx['amount'])
except Exception as e:
pass
return render_template(
'donate_transactions.html',
tx_list=get_transactions(client, blockchain_db, 'donate'),
sum=sum,
)
@app.route('/pay_transactions', methods=['GET'])
def pay_transactions():
"""
Shows all the vendor payments made by the charity.
"""
collection = client.pay_transactions
tx_list = get_transactions(client, blockchain_db, 'pay')
sum = 0
for tx in tx_list:
try:
sum += int(tx['amount'])
print("hello", file=sys.stderr)
except Exception as e:
print(str(e), file=sys.stderr)
print("error", file=sys.stderr)
pass
return render_template(
'pay_transactions.html',
tx_list=get_transactions(client, blockchain_db, 'pay'),
sum=sum,
)
@app.route('/portal')
def portal():
dtx_list = get_transactions(client, blockchain_db, 'donate')
dsum = 0
for tx in dtx_list:
try:
dsum += int(tx['amount'])
except:
pass
ptx_list = get_transactions(client, blockchain_db, 'pay')
psum=0
for tx in ptx_list:
try:
psum += int(tx['amount'])
except:
pass
return render_template(
'bootstrap.html',
dtx_list=dtx_list,
ptx_list=ptx_list,
dsum=dsum,
psum=psum,
)
if __name__ == '__main__':
app.run(
debug=True,
host='0.0.0.0',
port=3031,
)