-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
183 lines (132 loc) · 4.9 KB
/
app.py
File metadata and controls
183 lines (132 loc) · 4.9 KB
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
from flask import Flask, request
from datetime import datetime
import pandas as pd
app = Flask(__name__)
# Get the current date in second since 01-01-2023
time = datetime(2023,1,1).timestamp()
# Tuple initialization
add1 = ("Antoine","Christian",time,10)
add2 = ("Antoine","Christian",time,200)
# Add the hash in the tuple
add1 = ("Antoine","Christian",time,10,hash(add1))
add2 = ("Antoine","Christian",time,200,hash(add2))
# Dictionary initialization
transaction = [add1,add2]
# Function to return all of the dictionary
@app.route("/display_list", methods=['GET'])
def getList():
if request.method == 'GET':
# Sort the dictionary by date
transaction.sort()
return str(transaction)
# Function to return all of the dictionary of a person
@app.route("/display_list/<Person>", methods=['GET'])
def getListPerson(Person):
if request.method == 'GET':
# Initialize the result
result = ""
# Sort the dictionary by date
transaction.sort()
# Loop through the dictionary
for i in transaction:
# Check if the person in the dictonary is egal to the person in the GET
if i[0] == Person:
result+= str(i)
if i[1] == str(Person):
result+= str(i)
# If the person is not in the dictionary
if(result == ""):
return "Person not found"
# If the person is in the dictionary return the solde of transaction
else:
return result
# Function to display the solde of a person
@app.route("/display_solde/<Person>", methods=['GET'])
def getSolde(Person):
# Initialize the solde
solde = 0
# Loop through the dictionary
for i in transaction:
# Check if the person in the dictonary is egal to the person in the GET
if i[0] == Person:
# Remove the solde of the person
solde -= i[3]
if i[1] == Person:
# Add the solde of the person
solde += i[3]
return str(solde)
# Function to add an element in the dictionary
@app.route("/add_element/", methods=['POST','GET'])
def addElement():
if request.method == 'POST':
# Get the data from the form
person1=str(request.form.get("p1"))
person2=str(request.form.get("p2"))
solde=int(request.form.get("solde"))
# Get the current date in second since 2023
time = datetime(2023,1,1).timestamp()
# Add the element in a tuple
add = (person1,person2,time,solde)
add = (person1,person2,solde,time,hash(add))
# Add the tuple in the dictionary
transaction.append(add)
return "You have successfully added a new element:" + str(add)
return "You have not added a new element"
# Function to import a CSV file
@app.route("/importeCSV", methods=['GET'])
def importeCSV():
if request.method == 'GET':
# CVS Column Names
filePath = str(request.form.get("filePath"))
print(filePath)
# CSV Column Names
# This code is used to generate an array of column names for a dataframe
# The column names are first_person, second_person, date, value, and hash
col_names = ['first_person','second_person','date', 'value','hash']
# Use Pandas to parse the CSV file
csvData = pd.read_csv(filePath,names = col_names,engine='python',sep=';')
# Loop through the Rows
for i,row in csvData.iterrows():
# Get the values from the row
first_person = row['first_person']
second_person = row['second_person']
date = row['date']
value = row['value']
hash = row['hash']
# Add the element in a tuple
add = (first_person,second_person,date,value,hash)
# Add the tuple in the dictionary
transaction.append(add)
return "ok"
# Hash verification
@app.route("/hash_verification", methods=['GET'])
def hash_vefication():
info = ""
# Loop through the dictionary
for i in transaction:
# Delete the last parameters of the tuple
a= (i[0],i[1],i[2],i[3])
# Check if the hash is correct
if i[4] != hash(a):
info += "Hash is not correct for "+ str(i) + "\n"
else:
info += "Hash is correct for " + str(i) + "\n"
return info
# Hash correction
@app.route("/hash_correction", methods=['GET'])
def hash_correction():
info = ""
for i in transaction:
# Create a new tuple with the first three parameters of the tuple
a= (i[0],i[1],i[2])
# Check if the hash is correct
if i[4] != hash(a):
# If not correct, correct it
info += "Hash is not correct for "+ str(i) + " the new hash is " + str(hash(a)) + "\n"
i = (i[0],i[1],i[2],i[3],hash(a))
else:
# If correct, do nothing
info += "Hash is correct for " + str(i) + "\n"
return info
if __name__ =='__main__':
app.run()