-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
300 lines (278 loc) · 11.4 KB
/
main.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
# -*- coding: utf-8 -*-
from flask import Flask, redirect, url_for, request,render_template
app = Flask(__name__)
from signature import *
from login import *
def verifyGroupLogin(group_id):
member_ids = findAllMemberIdsForGroup(group_id)
flag = False
for i in member_ids:
if session['user_id'] == int(i[0]):
flag = True
return flag
@app.route('/success/<name>')
def success(name):
return ' %s' % name
@app.route('/users/<user_id>/userAddTransaction')
def addTransaction(user_id):
if session['user_id']== int(user_id):
return render_template('addTransaction.html',user_id = user_id)
else:
return redirect('/signIn')
@app.route('/transaction/<transaction_id>')
def renderTransaction(transaction_id):
previous_transaction = findTransactionById(transaction_id)
if session['user_id'] == int(previous_transaction[5]) or session['user_id'] == int(previous_transaction[6]):
name = previous_transaction[1]
amount = previous_transaction[2]
reciver = findUserByUser_Id(previous_transaction[6])
reciver_name = reciver[1]
reciver_email = reciver[2]
sender = findUserByUser_Id(previous_transaction[5])
sender_name = sender[1]
sender_email = sender[2]
comments = previous_transaction[3]
return render_template("renderTransaction.html",name = name,amount = amount,
reciver = reciver_name,sender = sender_name,comments = comments,
reciver_email = reciver_email,sender_email = sender_email,transaction_id =transaction_id)
else:
return redirect('/signIn')
@app.route('/addCommentToTransaction',methods = ['POST'])
def addCommentToTransaction():
comment = request.form['comment']
transaction_id = request.form['transaction_id']
transactions = findTransactionById(transaction_id)
if session['user_id'] == int(transactions[5]) or session['user_id'] == int(transactions[6]):
changeCommentToTransaction(comment,transaction_id)
return redirect('/transaction/'+str(transaction_id))
else:
return redirect('/signIn')
@app.route('/<transaction_id>/deleteTransaction')
def deleteTransactionByUser(transaction_id):
transactions = findTransactionById(transaction_id)
if session['user_id'] == transactions[5] or session['user_id'] == transaction_id:
deleteTransaction(transaction_id)
return (url_for('success',name = 'Successfully Deleted'))
else:
return redirect('/signIn')
@app.route('/addTransactionToData',methods = ['POST','GET'])
def addTransactionToData():
if request.method == 'POST':
name = request.form['nm']
sender_id = int(request.form['sender'])
receiver_mail = request.form['receiver']
receiver_id = findUserByEmail(receiver_mail)[0]
amount = int(request.form['amt'])*-1
return_value = insertTransaction(name,amount,sender_id,receiver_id)
if return_value is 1:
msg = 'success'
elif return_value is -1:
msg = 'sender_id does not exist'
else:
msg = 'receiver_id does not exist'
return (redirect(url_for('success',name = msg)))
@app.route('/<user_id>/addFriends',methods = ['POST','GET'])
def addFriends(user_id):
if session['user_id'] == int(user_id):
if request.method == 'POST':
name = request.form['friend']
user = findUserByEmail(name)
if user:
friend_id = user[0]
insertFriend(user_id,friend_id)
return redirect(url_for('renderHome',user_id = user_id))
else:
return redirect(url_for('success',name = "Not valid email"))
else:
return redirect('/signIn')
@app.route('/users/<user_id>/passbook')
def renderPassbook(user_id):
if session['user_id'] == int(user_id):
user_name = findUserByUser_Id(user_id)[1]
transactions = findAllTransactionByUser_Id(user_id)
result = []
for i in transactions:
if int(user_id) == i[6]:
user = findUserByUser_Id(i[5])
result.append([[i[2],i[1],user[1],i[3]],i[0]])
else:
user = findUserByUser_Id(i[6])
result.append([[i[2]*-1,i[1],user[1],i[3]],i[0]])
return render_template('passbook_maker.html',transactions = result,user_name = user_name)
else:
return redirect('/signIn')
@app.route('/<user_id>/makeGroupForm')
def renderMakeGroupForm(user_id):
if session['user_id'] == int(user_id):
return render_template('makeGroupForm.html',user = user_id)
else:
return redirect('/signIn')
@app.route('/establishMembership',methods = ['POST','GET'])
def establishMembership():
if request.method == 'POST':
email = request.form['email']
group_id = request.form['group_id']
user = findUserByEmail(email)
if verifyGroupLogin(group_id):
if user:
user_id = user[0]
makeMember(group_id,user_id)
return (redirect("/group/{}".format(group_id)))
else:
return redirect('/signIn')
@app.route('/makeGroup',methods = ['POST','GET'])
def makeGroup():
if request.method == 'POST':
name = request.form['name']
creator = request.form['creator']
if session['user_id'] == int(creator):
group_id = addNewGroup(name)
makeMember(group_id,creator)
return (redirect("/group/{}".format(group_id)))
else:
return redirect('/signIn')
@app.route('/<group_id>/members/<user_id>')
def renderMember(group_id,user_id):
if session['user_id'] == user_id:
return redirect('/users/'+str(user_id))
else:
return redirect('/signIn')
@app.route('/group/<group_id>')
def renderGroup(group_id):
if verifyGroupLogin(group_id):
group = findGroupById(group_id)
members = findAllMembersForGroupHomeRendering(group_id)
transactions = findAllTransactionsForGroup(group_id)
return render_template("group_home.html",name = group[2],memberList = members,transactionList = transactions,group_id = group[0])
else:
return redirect('/signIn')
@app.route('/groups/addGroupTransaction',methods = ['POST'])
def addGroupTransaction():
name = request.form['groupTransactionName']
group_id = request.form['group_id']
if verifyGroupLogin(group_id):
groupTransaction_id = makeGroupTransaction(name,group_id)
member_ids = findAllMemberIdsForGroup(group_id)
payer_ids = findAllPayerForGroupTransaction(group_id)
members = []
payers = []
for i in member_ids:
user = findUserByUser_Id(i[0])
members.append([user[1],user[2]])
for i in payer_ids:
payer = findUserByUser_Id(i[0])
payers.append([user[1],user[2],i[1]])
return render_template("addGroupTransaction.html",memberList = members,name = name ,payerList = payers,groupTransaction_id = groupTransaction_id)
else:
return redirect('/signIn')
@app.route('/execGroupTransaction',methods = ['POST'])
def execGroupTransaction():
groupTransaction_id = request.form['groupTransaction_id']
groupTransaction = findGroupTransactionById(groupTransaction_id)
group_id = groupTransaction[2]
if verifyGroupLogin(group_id):
group = findGroupById(group_id)
title = groupTransaction[1]+"("+group[2]+")"
temp = findAllMemberIdsForGroup(group_id)
member_ids = []
members = []
for i in temp:
member_ids.append(i[0])
members.append([i[0],0,0,'true'])
total_amount = 0
email_passer = request.form['email_passer']
total_amount_recieved = request.form['amount_passer']
new_email = []
flag = []
payer = []
new_amount = []
payer_details = []
for i in email_passer:
if i == '(':
new_email = []
flag = 0
new_amount = []
elif i == '#':
flag = 1
elif i ==')':
flag = 3
new_email = ''.join(new_email)
new_amount = ''.join(new_amount)
payer = findUserByEmail(new_email)
payer_details.append([payer,new_amount])
elif flag == 0:
new_email.append(i)
elif flag == 1:
new_amount.append(i)
for i in payer_details:
payer_id = int(i[0][0])
amount = int(i[1])
total_amount += amount
addPayerToGroupTransaction(groupTransaction_id,payer_id,amount)
for j in members:
if j[0]==payer_id:
j[2] -= int(amount)
break
k = 1
total_shares=0
for i in members:
include = []
shares = request.form['share'+str(k)]
if not shares:
shares = 1
include = request.form.getlist('member'+str(k))
i[3] = include
if include:
i[1]=int(shares)
total_shares+=int(shares)
makeShare(int(shares),groupTransaction_id,i[0])
k = k + 1
payer_iterator = 0
addToGroupTransactionAmount(groupTransaction_id,total_amount)
dividends = total_amount/total_shares
print (payer_details[0][1])
print ("Shape of Water")
for i in members:
if i[3]:
i[2] += dividends * i[1]
if i[2]>0:
while True:
if i[2] <= int(payer_details[payer_iterator][1]):
insertTransaction(title,i[2],int(payer_details[payer_iterator][0][0]),i[0])
payer_details[payer_iterator][1] = int(payer_details[payer_iterator][1]) - i[2]
break
else:
insertTransaction(title,int(payer_details[payer_iterator][1]),int(payer_details[payer_iterator][0][0]),i[0])
i[2] -= int(payer_details[payer_iterator][1])
payer_iterator += 1
addToMembershipAmount(i[0],group_id,i[2])
return redirect(url_for('success',name = "Happy"))
else:
return redirect('/signIn')
@app.route('/renderGroupTransaction/<groupTransaction_id>')
def renderGroupTransaction(groupTransaction_id):
payers = findAllPayerForGroupTransaction(groupTransaction_id)
group_id = findGroupTransactionById(groupTransaction_id)[2]
if verifyGroupLogin(group_id):
payerList = []
for i in payers:
payer = findUserByUser_Id(i[0])
payerList.append([payer[1],payer[2],i[1]])
customers = findAllSharesForGroupTransaction(groupTransaction_id)
shares = []
for i in customers:
user = findUserByUser_Id(i[1])
shares.append([user[1],user[2],i[0]])
return render_template('renderGroupTransaction.html',payerList = payerList,customer = shares)
else:
return redirect('/signIn')
@app.route('/addPayer',methods = ['POST'])
def addASinglePayer():
email = request.form['email']
user = findUserByEmail(email)
if user:
user_id = user[0]
addPayerToGroupTransaction(group_id,user_id,amount)
return render_template("addGroupTransaction.html",memberList = members)
if __name__ == '__main__':
app.run(debug = True)