-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
455 lines (429 loc) · 20.3 KB
/
Copy pathapp.py
File metadata and controls
455 lines (429 loc) · 20.3 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from datetime import datetime,date
from flask import Flask, render_template, request, url_for, flash, session,redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import exc
from flask_mail import Mail, Message
import os
import psycopg2
from os.path import join, dirname
from dotenv import load_dotenv
from werkzeug.security import generate_password_hash, check_password_hash
from itsdangerous import URLSafeTimedSerializer, SignatureExpired, BadSignature
from flask_login import LoginManager, UserMixin, login_manager, logout_user, current_user, AnonymousUserMixin, login_user
from flask_login.utils import login_required
try:
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
except:
print("No .env file found Its Not Dev Mode")
app = Flask(__name__)
mail = Mail(app)
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = os.environ['EMAIL']
app.config['MAIL_PASSWORD'] = os.environ['PASSWORD']
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
s = URLSafeTimedSerializer(os.environ['SALT'])
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL'].replace("://", "ql://", 1)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
class Anonymous(AnonymousUserMixin):
def __init__(self):
self.username = 'Guest'
app.secret_key = os.environ['SECRET_KEY_2']
class UserProfile(db.Model, UserMixin):
__tablename__ = 'userprofile'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(120), nullable=False)
imgstr = db.Column(db.String(120),default='0.6709606409289153')
salary = db.Column(db.Integer, default=0)
saving = db.Column(db.Integer, default=0)
def __repr__(self) -> str:
return f"{self.id} - {self.username}"
def verify_password(self, pwd):
return check_password_hash(self.password, pwd)
class Account(db.Model):
__tablename__ = 'account'
aid = db.Column(db.Integer, primary_key=True)
amount = db.Column(db.Integer, default=0)
amounttype = db.Column(db.String(120), nullable=False)
category = db.Column(db.String(120), nullable=False)
date = db.Column(db.Date, default=datetime.now())
day = db.Column(db.Integer, nullable=False)
month = db.Column(db.Integer, nullable=False)
year = db.Column(db.Integer, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('userprofile.id'))
class Category(db.Model):
__tablename__ = 'category'
cid = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(120), nullable=False)
amounttype = db.Column(db.String(120), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('userprofile.id'))
@login_manager.user_loader
def load_user(user_id):
return UserProfile.query.get(int(user_id))
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
try:
username = request.form["username"]
password = request.form["password"]
email = request.form["email"]
print(username, password, email)
check_if_exsisit_email = UserProfile.query.filter_by(
email=email).first()
check_if_exsisit_username = UserProfile.query.filter_by(
name=username).first()
hashed_Value = generate_password_hash(password)
check_if_exsisit_password = UserProfile.query.filter_by(
password=hashed_Value).first()
if not check_if_exsisit_email and not check_if_exsisit_username and not check_if_exsisit_password:
token = s.dumps({"Email": email, "Password": hashed_Value,
"Username": username}, salt=os.environ['TOKEN_SALT'])
print("--->> Token",token)
msg = Message(
'Hello', sender=os.environ['EMAIL'], recipients=[email])
link = url_for('confirm_email', token=token, _external=True)
msg.body = 'Your Token Is {}'.format(link)
mail.send(msg)
flash('Check Mail For Authentication')
return render_template('index.html', flag=1)
else:
flash('User Already Exist')
return render_template('index.html', flag=1)
except Exception as e:
print(e)
flash('Use Diffrent Username and Password')
return render_template('index.html', flag=1)
@app.route('/confirm_email/<token>')
def confirm_email(token):
try:
print("-->> Token Emai ",token)
Salt = os.environ["TOKEN_SALT"]
print("-->> Salt ",Salt)
data = s.loads(token, salt=Salt, max_age=60)
conf = UserProfile(
name=data["Username"], email=data["Email"], password=data["Password"])
db.session.add(conf)
db.session.commit()
except SignatureExpired:
db.session.rollback()
db.session.commit()
return "The Token Is Expired"
except BadSignature:
db.session.rollback()
db.session.commit()
return "The token is invalid"
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
return "Some Error Occured"
return redirect(url_for('login'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
usnl = request.form['usnl']
psdl = request.form['psdl']
eml = request.form['eml']
hashed_Value = generate_password_hash(psdl)
print(hashed_Value)
try:
cheking = UserProfile.query.filter_by(
name=usnl, email=eml).first()
print(check_password_hash(cheking.password, psdl))
if not cheking:
flash('Username and Email Are Incorrect')
return render_template('index.html', flag=2)
elif cheking and check_password_hash(cheking.password, psdl):
login_user(cheking)
session['visits'] = cheking.id
print("LOl", cheking.id)
# resp = make_response()
# resp.set_cookie('userID', cheking.id)
# print("--->>>",request.cookies.get('userID'))
# flash('login In Succesfully')
user = UserProfile.query.filter_by(
id=session.get('visits')).first()
print("--->>>",user.id)
db.session.commit()
return redirect(url_for('dashboard'))
else:
flash('Password Is Incorrect')
return render_template('index.html', flag=2)
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
flash('Username and Password Are Incorrect')
return render_template('index.html', flag=2)
return render_template('index.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('Logout Succesfully')
return render_template('index.html', flag=1)
@app.route('/profile',methods=['GET', 'POST'])
@login_required
def profile():
if request.method == 'GET':
try:
print("current_user",current_user.id)
user = UserProfile.query.filter_by(id=current_user.id).first()
return render_template('profile.html',user=user)
except Exception as e:
print(e)
return render_template('profile.html')
if request.method == 'POST':
try:
imgsrc = request.form["imgsrc"]
salary = request.form["salary"]
saving = request.form["saving"]
usernmae = request.form["usernmae"]
print(imgsrc,salary,saving)
user = UserProfile.query.filter_by(id=current_user.id).first()
user.imgstr = imgsrc
user.salary = salary
user.saving = saving
user.name = usernmae
db.session.add(user)
db.session.commit()
except exc.SQLAlchemyError:
flash('Use Different Username')
db.session.rollback()
db.session.commit()
except Exception as e:
print(e)
flash('Use Different Username')
db.session.rollback()
db.session.commit()
return render_template('profile.html',user=user)
@app.route('/dashboard')
@login_required
def dashboard():
try:
today_date = date.today()
user = UserProfile.query.filter_by(id=current_user.id).first()
my_data = list(Account.query.filter_by(user_id=current_user.id).all())
my_catgeory = Category.query.filter_by(user_id=current_user.id).all()
my_expense = db.session.execute(f"select SUM(amount) AS ex from account where amounttype = 'Expenses' and user_id = {current_user.id}").all()
my_income = db.session.execute(f"select SUM(amount) AS inc from account where amounttype = 'Income' and user_id = {current_user.id}").all()
this_month_exp = db.session.execute(f"select sum(amount) AS ex from account where amounttype = 'Expenses' and user_id = {current_user.id} and month = {today_date.month} and year = {today_date.year}").all()
this_month_income = db.session.execute(f"select sum(amount) AS inc from account where amounttype = 'Income' and user_id = {current_user.id} and month = {today_date.month} and year = {today_date.year}").all()
print("--->>>",my_expense,my_income,this_month_exp,this_month_income)
print("--->>>",session.get('visits'))
db.session.commit()
return render_template('Dashboard.html',user=user,data= my_data,category=my_catgeory,expenses=this_month_exp,income=this_month_income)
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
return "Some Error occurred"
@app.route('/report')
@login_required
def report():
try:
today_date = date.today()
pie_data = db.session.execute(f"select SUM(amount) as am,category from account where amounttype = 'Expenses' and user_id = {current_user.id} and month = {today_date.month} and year = {today_date.year} group by category").all()
db.session.commit()
pie_data_income = db.session.execute(f"select SUM(amount) as am,category from account where amounttype = 'Income' and user_id = {current_user.id} and month = {today_date.month} and year = {today_date.year} group by category").all()
db.session.commit()
area_data_expenses = db.session.execute(f"select SUM(amount) AS am,date from account where amounttype = 'Expenses' and user_id = {current_user.id} group by date")
db.session.commit()
area_data_expenses_2 = db.session.execute(f"select SUM(amount) AS am,date from account where amounttype = 'Expenses' and user_id = {current_user.id} group by date")
db.session.commit()
area_data_income = db.session.execute(f"select SUM(amount) AS am,date from account where amounttype = 'Income' and user_id = {current_user.id} group by date")
db.session.commit()
print("--->>>",pie_data)
user = UserProfile.query.filter_by(id=current_user.id).first()
column_chart_income = db.session.execute(f"select sum(amount) AS inc, month from account where amounttype = 'Income' and user_id = {current_user.id} and year = {today_date.year} group by month").all()
column_chart_expenses = db.session.execute(f"select sum(amount) AS inc, month from account where amounttype = 'Expenses' and user_id = {current_user.id} and year = {today_date.year} group by month").all()
user = UserProfile.query.filter_by(id=current_user.id).first()
this_month_exp = db.session.execute(f"select sum(amount) AS ex from account where amounttype = 'Expenses' and user_id = {current_user.id} and month = {today_date.month} and year = {today_date.year}").all()
this_month_income = db.session.execute(f"select sum(amount) AS inc from account where amounttype = 'Income' and user_id = {current_user.id} and month = {today_date.month} and year = {today_date.year}").all()
print("--->>>",this_month_exp,this_month_income)
db.session.commit()
print("--->>> cc ",column_chart_income,column_chart_expenses)
return render_template('report.html',user=user,pie_data=pie_data,pie_data_income=pie_data_income,area_data_expenses=area_data_expenses,area_data_income=area_data_income,area_data_expenses_2=area_data_expenses_2,column_chart_income=column_chart_income,column_chart_expenses=column_chart_expenses,expenses=this_month_exp,income=this_month_income)
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
return "Some Error occurred"
@app.route('/item', methods=['GET', 'POST','DELETE'])
@login_required
def item():
if request.method == 'POST':
try:
today_date = date.today()
gridRadios = request.form["gridRadios"]
category = request.form["category"]
amount = request.form["amount"]
dates = request.form["date"]
new_date=datetime.strptime(dates, "%Y-%m-%d")
print("--->>> ",dates,new_date.month)
add_acc = Account(amount = amount, amounttype = gridRadios,category= category,date = dates,user_id=current_user.id,day=new_date.day,month=new_date.month,year=new_date.year)
is_add_befor=db.session.execute(f"SELECT COUNT(category) as ct FROM account WHERE category = '{category}' and user_id = {current_user.id} and amounttype = 'Expenses' and day = {new_date.day} and month={new_date.month} and year = {new_date.year}").all()
db.session.add(add_acc)
db.session.commit()
usese = UserProfile.query.filter_by(id=current_user.id).first()
this_month_exp = db.session.execute(f"select sum(amount) AS ex from account where amounttype = 'Expenses' and user_id = {current_user.id} and month = {today_date.month} and year = {today_date.year}").all()
this_month_income = db.session.execute(f"select sum(amount) AS inc from account where amounttype = 'Income' and user_id = {current_user.id} and month = {today_date.month} and year = {today_date.year}").all()
print("--->>> RR ",this_month_exp,this_month_income)
try:
if this_month_income == None or this_month_exp== None:
calculate_budget = usese.salary - usese.saving
else:
calculate_budget = usese.salary - usese.saving - this_month_exp[0].ex + this_month_income[0].inc
except Exception as e:
print(e)
calculate_budget = usese.salary - usese.saving
print("--->>>",calculate_budget)
print("--->>>",is_add_befor)
if calculate_budget <= 1000 and calculate_budget >= 1:
flash("You are in danger of going over your budget")
return redirect(url_for('dashboard'))
elif calculate_budget <= 0:
flash("Your Budget is Over")
return redirect(url_for('dashboard'))
if int(is_add_befor[0].ct) >=1:
flash('You Are Investing This Again')
return redirect(url_for('dashboard'))
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
return "Some Error occurred"
if request.method == 'DELETE':
try:
print("--->>>",request.form["id"])
acc = Account.query.filter_by(aid=request.form["id"],user_id=current_user.id).first()
db.session.delete(acc)
db.session.commit()
return "Done"
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
return "Some Error occurred"
@app.route('/edit',methods=['POST'])
@login_required
def edit():
try:
# text = request.form["text"]
# category = request.form["category"]
# money = request.form["money"]
# date = request.form["date"]
# id = request.form["id"]
# new_date=datetime.strptime(date, "%Y-%m-%d")
gridRadios = request.form["gridRadios"]
category = request.form["category"]
amount = request.form["amount"]
dates = request.form["date"]
id = request.form["id"]
print("---->>>> ID ",id)
new_date=datetime.strptime(dates, "%Y-%m-%d")
print(gridRadios,category,amount,dates,(id))
edit_acc = Account.query.filter_by(aid=id,user_id=current_user.id).first()
amount= amount.replace("$", " ")
amount=amount.replace("+", " ")
amount=amount.replace("-", " ")
print("--->>> MO ",amount)
edit_acc.amount = int(amount.strip())
print("--->>> MO ",amount)
edit_acc.amounttype = gridRadios
edit_acc.category = category
edit_acc.date = dates
edit_acc.day = new_date.day
edit_acc.month = new_date.month
edit_acc.year = new_date.year
db.session.add(edit_acc)
db.session.commit()
return redirect(url_for('dashboard'))
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
return "Some Error occurred"
@app.route('/addcategory',methods=['POST'])
@login_required
def addcategory():
try:
category = request.form["category"]
gridRadios = request.form["gridRadios"]
print(category)
add_cat = Category(category=category,amounttype= gridRadios,user_id=current_user.id)
db.session.add(add_cat)
db.session.commit()
return redirect(url_for('dashboard'))
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
return "Some Error occurred"
@app.route('/reset_link',methods=['GET','POST'])
def reset_link():
if request.method == 'POST':
reset_email = request.form['emailforget']
cheking = UserProfile.query.filter_by(email=reset_email).first()
if not cheking:
return "User Doesn't Exist"
reset_token = s.dumps({"Email":reset_email},salt=os.environ['RESET_PASSWORD_SALT'])
print(reset_token)
msg = Message('Hello',sender =os.environ['EMAIL'],recipients = [reset_email])
link = url_for('reset_password',token=reset_token,_external=True)
msg.body = 'Your Token Is {}'.format(link)
mail.send(msg)
return "Check Mail For Reset Password"
@app.route('/reset_password/<token>',methods=['GET','POST'])
def reset_password(token):
try:
data = s.loads(token , salt=os.environ['RESET_PASSWORD_SALT'],max_age=60)
if request.method == 'GET':
user_profile = UserProfile.query.filter_by(email=data["Email"]).first()
return render_template('forget-password.html',token=token,user=user_profile)
# conf = User_Basic_infos(username=data["Username"],email=data["Email"],password=data["Password"],flags=True)
# db.session.add(conf)
# db.session.commit()
if request.method == 'POST':
reset_password=request.form["reset_password"]
reset_password2 = request.form["reset_password2"]
if reset_password != reset_password2:
return "Password Doesn't Match"
print(reset_password)
hashed_Value = generate_password_hash(reset_password)
print(hashed_Value)
print(data["Email"])
user_profile = UserProfile.query.filter_by(email=data["Email"]).first()
user_profile.password = hashed_Value
db.session.add(user_profile)
db.session.commit()
return redirect('/')
except SignatureExpired:
db.session.rollback()
db.session.commit()
return "The Token Is Expired"
except BadSignature:
db.session.rollback()
db.session.commit()
return "The token is invalid"
except Exception as e:
print(e)
db.session.rollback()
db.session.commit()
return "Some Error Occurred"
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(debug=True)