-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_database_model.py
366 lines (315 loc) · 11.7 KB
/
_database_model.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
from datetime import timedelta
from flask import Flask, request, session
import os
from os import path as ps
from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.engine.base import Transaction
from sqlalchemy.orm import relationship
from flask_sqlalchemy import BaseQuery, SQLAlchemy
import datetime
import rsa
create_engine("mysql+pymysql://isokoin:pw@host/db", pool_pre_ping=True)
ROOT_DIR = os.path.dirname(os.getcwd())
app = Flask(
__name__,
template_folder='templates',
static_folder='static'
)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///isokoin.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'top secret!'
app.secret_key = 'password'
app.permanent_session_lifetime = timedelta(minutes=50)
db = SQLAlchemy(app)
#------------------------------ BACKEND FUNCTIONALITY-----------------------------------------------
# this is for index and block
def textfile_i_o(filename, text, write, _seek=True):
try:
file = open(filename, 'r+')
except FileNotFoundError:
file = open(filename, 'w')
file.close()
file = open(filename, 'r+')
content = file.read()
if write:
if _seek:
file.seek(0)
else:
pass
file.write(text)
else:
pass
file.close()
return content
class File(object):
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
def __enter__(self):
self.file = open(self.filename, self.mode)
return self.file
def __exit__(self, exec_type, exec_val, traceback):
self.file.close()
class FileUpdateSystem(File):
def __init__(self, *a ,**kw):
self.a = a
self.kw = kw
def convert_data_to_html_table(self):
pass
# make sure to copy the line as its represented
def deleteLine(self, filename, lines_to_delete):
# filtering
for line_to_delete in lines_to_delete:
with File(filename, 'r+') as f:
output = []
content = f.readlines()
for line in content:
if line.startswith(line_to_delete):
print('deleted line: ',line)
pass
else:
output.append(line)
# rewriting
with File(filename, 'w') as f:
for line in output:
f.write(line)
def series_to_list(self, series):
list_ = [i for i in series]
return list_
def generate_table(self, _blockchain):
for block in _blockchain.chain:
transactions = block.transactions
for transaction in transactions:
i: Transaction = transaction
print(i)
table_of_contents = f'''
<tr>
<th>{i.index}</th>
<td>{i.sender}</td>
<td>{i.ammount}</td>
<td>{i.receiver}</td>
<td>{i.time}</td>
<td>{i.hash}</td>
</tr>
'''
with File('info.html', 'w') as f:
f.write(table_of_contents)
def append_from_pending(self, blockchain):
for transaction in blockchain.transactions:
i: Transaction = transaction
print(i)
table_of_contents = f'''
<tr>
<th>{i.index}</th>
<td>{i.sender}</td>
<td>{i.ammount}</td>
<td>{i.receiver}</td>
<td>{i.time}</td>
<td>{i.hash}</td>
</tr>
'''
with File('info.html', 'w') as f:
try:
f.write(table_of_contents)
except UnboundLocalError:
pass
# find a way to append the tables int the destianation files
def append_table(self, blockchain, section_to_target):
filename = os.path.join('templates','account.html')
print(filename)
try:
self.generate_table(blockchain)
except UnboundLocalError:
self.append_from_pending(blockchain)
with File(filename, 'r') as fin, File('output.html', 'w') as fout, File('info.html', 'r') as content:
table = content.read()
fin_list = fin.readlines()
print(fin_list)
for line in fin_list:
fout.write(line)
if line == section_to_target + '\n':
print(line)
fout.write(table)
fout.write('\n')
else:
pass
with File(filename, 'w') as fout, File('output.html', 'r') as fin:
content = fin.read()
fout.write(content)
def update_block_list(self, _blockchain, section_to_target):
output_updated = False
filename = os.path.join('templates','blockchain.html')
print(filename)
for block in _blockchain.chain:
new_block = f'''
<div class="card flex">
<p>Block: {block.index}</p>
<p>Hash: {block.hash}</p>
<p>Nonse: {block.nonse}</p>
<p>Previous Hash: {block.previous}</p>
<p>Time: {block.time}</p>
<a href="../view_transactions" class="btn btn-outline">View Transactions</a>
<i class="fas fa-database fa-3x"></i>
</div>
'''
with File(filename, 'r') as fin, File('output.html', 'w') as fout:
content = fin.readlines()
for line in content:
print(line)
fout.write(line)
if line == section_to_target + '\n':
fout.write(new_block)
output_updated = True
fout.write('\n')
else:
pass
if output_updated:
with File(filename, 'w') as fout, File('output.html', 'r') as fin:
content = fin.read()
fout.write(content)
def generate_key(password):
public_key, private_key = rsa.newkeys(2048)
encrypted_password = password.encode('utf8')
encrypted_password = str(rsa.encrypt(encrypted_password, public_key))
return encrypted_password, str(private_key), str(public_key)
def retrive_password(encrypted_password, public_key):
password = rsa.decrypt(encrypted_password, public_key)
decrypted_password = password.decode('utf8')
return decrypted_password
def create_transaction_data(transactions, sender):
found_user = check_if_registered(sender)
try:
if found_user == False:
return None
else:
db.create_all()
session_data = Transactions(
session_id=increment_id(Transactions),
user_id= found_user.participant_id,
content = transactions.hash
)
db.session.add(session_data)
db.session.commit()
return session_data
except AttributeError:
return None
def update_user_balance(sender, ammount):
db.create_all()
user = db.session.query(UserAccount).filter_by(username=sender).first()
try:
user.balance -= ammount
except AttributeError:
pass
print('updating balance of: ',user)
db.session.commit()
return user
def generate_block_number():
_index = textfile_i_o('block_index.txt','', False)
print('block number: ',_index)
_index = int(_index) + 1
_index = textfile_i_o('block_index.txt',f'{_index}', True)
return int(_index)
def update_receiver_balance(receiver_username, ammount):
db.create_all()
receiver = db.session.query(UserAccount).filter_by(username=receiver_username).first()
receiver.balance += ammount
db.session.commit()
return receiver
def create_user_data(username, password, public_key):
public_key = str(public_key)
encrypted_password, private_key, _ = generate_key(password)
db.create_all()
session_data = UserAccount(
participant_id=increment_id(UserAccount),
username=username,
password=encrypted_password,
public_key=public_key,
private_key = private_key
)
db.session.add(session_data)
db.session.commit()
return session_data
def reset(reset_all_users=False, reset_all_posts=False):
if 'username' in session:
print(' loading reset .....')
db.create_all()
all_users = db.session.query(UserAccount).all()
all_posts = db.session.query(Transactions).all()
if reset_all_users:
for user in all_users:
users_to_delete = UserAccount.query.filter_by(username=user.participant_id).first()
db.session.delete(users_to_delete)
db.session.commit()
else:
pass
if reset_all_posts:
for post in all_posts:
posts_to_delete = users_to_delete = Transactions.query.filter_by(session_id=post.session_id).first()
db.session.delete(posts_to_delete)
db.session.commit()
else:
pass
print(all_users)
session.pop('username', None)
session.pop('password', None)
session.pop('date', None)
else:
pass
def increment_id(column_objct):
db.create_all()
users = db.session.query(column_objct).all()
return len(users) + 1
def init_session_(*args):
for attribute_values in args:
session[attribute_values] = request.form[attribute_values]
def check_database_column(column_objct):
db.create_all()
datasess = db.session.query(column_objct).all()
print(datasess)
def check_if_registered(sender):
db.create_all()
found_user = db.session.query(UserAccount).filter_by(username=sender).first()
print(f' checking if {sender} is registered.....')
if type(found_user) == BaseQuery:
return None
elif found_user == None:
return None
else:
return found_user
# -------------------------------- DATABASE MODEL OBJECTS----------------------------------------------
class UserAccount(db.Model):
default_password, default_key, public_key = generate_key('password')
__tablename__ = 'user_account'
participant_id = Column(Integer, primary_key=True, nullable=False)
username = Column(String(80), nullable=False, default=f'username{participant_id}', unique=True)
password = Column(String(80), nullable=False, default=f'{default_password}', unique=True)
balance = Column(Integer, nullable=False, default=10000)
public_key = Column(String(80), nullable=False, default=f'{public_key}', unique=True)
private_key = Column(String(80), nullable=False, default=f'{default_key}', unique=True)
transactions = relationship('Transactions', backref='author', lazy=True)
address = Column(String(80), nullable=False, default='127.0.0.1:5500')
def __repr__(self):
return f'''
UserAccount(
username : {self.username},
participant id : {self.participant_id},
transactions completed: {self.transactions},
balance : {self.balance} ISK,
address : {self.address},
public key: {self.public_key}
)
'''
class Transactions(db.Model):
session_id = Column(Integer, primary_key=True, nullable=False)
content = Column(String(80), nullable=False, default='start session')
user_id = Column(Integer, ForeignKey('user_account.participant_id'), nullable=False)
date = Column(DateTime, nullable=False, default=datetime.datetime.utcnow())
def __repr__(self):
return f'''
Transactions(
date : {self.date},
session id : {self.session_id},
content: {self.content},
author : {self.user_id}
)
'''