-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
206 lines (166 loc) · 8.26 KB
/
routes.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
#routes.py
from flask import Blueprint, render_template, request, redirect, url_for
from flask_login import login_user, login_required, logout_user, current_user
from datetime import datetime
from model import db, Participant, CentralBank, Transaction # Adjust import here
from werkzeug.security import generate_password_hash, check_password_hash
from flask import flash
from werkzeug.security import generate_password_hash
bp = Blueprint('routes', __name__)
@bp.route('/', methods=['GET', 'POST'])
def index():
if current_user.is_authenticated:
return redirect(url_for('routes.homepage'))
else:
return redirect(url_for('routes.login'))
@bp.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
# Check if the username already exists
existing_user = Participant.query.filter_by(name=request.form['name']).first()
if existing_user:
flash('Username already exists. Please choose a different username.', 'error')
return redirect(url_for('routes.register'))
# If the username is unique, proceed with registration
hashed_password = generate_password_hash(request.form['password'], method='pbkdf2:sha256')
# hashed_password = generate_password_hash(request.form['password'])
# hashed_password = request.form['password']# generate_password_hash(request.form['password'])
new_user = Participant(name=request.form['name'], password=hashed_password, coins=100)
db.session.add(new_user)
db.session.commit()
# Add transaction for entering the system
transaction = Transaction(timestamp=datetime.utcnow(), sender=new_user, receiver=new_user, amount=100, subject="entered the system")
db.session.add(transaction)
db.session.commit()
return redirect(url_for('routes.login'))
return render_template('register.html')
@bp.route('/login', methods=['GET', 'POST'])
def login():
error = None # Initialize error message
if request.method == 'POST':
user = Participant.query.filter_by(name=request.form['name']).first()
if user and check_password_hash(user.password, request.form['password']):
if user.is_active:
login_user(user)
return redirect(url_for('routes.homepage'))
else:
error = "User is not active. Please contact the administrator." # Set error message
else:
error = "Invalid username or password" # Set error message
return render_template('login.html', error=error)
@bp.route('/homepage', methods=['GET'])
@login_required
def homepage():
all_participants = Participant.query.all()
central_bank = CentralBank.query.first()
active_participants = Participant.query.filter_by(is_active=True).all()
if central_bank:
cb_coins = central_bank.coins
else:
cb_coins = 0
total_activity = sum([participant.coins for participant in active_participants]) + cb_coins
perc_user = current_user.coins / total_activity
transactions = Transaction.query.order_by(Transaction.timestamp.desc()).all()
# Calculate the ratio of each participant's coins to the total activity
participant_ratios = []
for participant in all_participants:
ratio = participant.coins / total_activity
participant_ratios.append(ratio)
return render_template('homepage.html', perc_user=perc_user, participants=all_participants, central_bank=central_bank,
transactions=transactions, total_activity=total_activity, participant_ratios=participant_ratios)
@bp.route('/profile', methods=['GET', 'POST'])
@login_required
def profile():
if request.method == 'POST':
current_user.name = request.form['name']
profile_picture_url = request.form['profile_picture_url']
current_user.profile_picture_url = profile_picture_url
db.session.commit()
return redirect(url_for('routes.homepage'))
return render_template('profile.html')
@bp.route('/transact', methods=['GET', 'POST'])
@login_required
def transact():
error = None # Initialize error message
if request.method == 'POST':
receiver_name = request.form['receiver']
receiver = Participant.query.filter_by(name=receiver_name).first()
# Check if receiver exists and is active
if receiver:
if receiver.is_active:
amount_str = request.form['coins']
# Check if the input contains a comma instead of a period
if ',' in amount_str:
error = "Please use a period (.) as the decimal separator for coins."
else:
try:
amount = float(amount_str)
# Check if sender is not the receiver
if receiver != current_user:
# Check if sender has enough coins
if current_user.coins >= amount:
current_user.coins -= amount
receiver.coins += amount
transaction = Transaction(timestamp=datetime.utcnow(), sender=current_user, receiver=receiver, amount=amount, subject=request.form['subject'])
db.session.add(transaction)
db.session.commit()
return redirect(url_for('routes.homepage'))
else:
error = "Insufficient coins for transaction"
else:
error = "You cannot transact with yourself"
except ValueError:
error = "Invalid input for coins. Please enter a valid number."
else:
error = "Receiver is inactive"
else:
error = "Receiver not found"
return render_template('transact.html', error=error)
@bp.route('/redistribute-coins', methods=['POST'])
@login_required
def redistribute_coins():
if current_user.coins == 0:
return redirect(url_for('routes.homepage'))
active_participants = Participant.query.filter_by(is_active=True).all()
num_participants = len(active_participants)
if num_participants == 0:
return redirect(url_for('routes.homepage'))
redistribution_amount = current_user.coins / (num_participants - 1)
for participant in active_participants:
if participant != current_user: # Skip redistribution to the current user
participant.coins += redistribution_amount
# Log redistribution transaction for each participant
transaction = Transaction(timestamp=datetime.utcnow(), sender=current_user, receiver=participant, amount=redistribution_amount, subject="Redistribution spot")
db.session.add(transaction)
current_user.coins = 0
db.session.commit()
return redirect(url_for('routes.homepage'))
@bp.route('/exit-system', methods=['POST'])
@login_required
def exit_system():
if 'exit-system' in request.form:
if current_user.coins > 0:
active_participants = Participant.query.filter(Participant.is_active == True).all()
num_participants = len(active_participants)
if num_participants > 0:
redistribution_amount = current_user.coins / (num_participants - 1)
for participant in active_participants:
if participant != current_user: # Skip redistribution to the current user
participant.coins += redistribution_amount
# Log redistribution transaction for each participant
transaction = Transaction(timestamp=datetime.utcnow(), sender=current_user, receiver=participant, amount=redistribution_amount, subject="Redistribution exit")
db.session.add(transaction)
db.session.commit()
current_user.is_active = False
current_user.coins = 0
db.session.commit()
logout_user()
return redirect(url_for('routes.login'))
@bp.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('routes.login'))
@bp.route('/faq')
def faq():
return render_template('faq.html')