-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
99 lines (85 loc) · 3.26 KB
/
app.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
from flask import Flask,request,redirect,render_template
from flask_sqlalchemy import SQLAlchemy
import os
project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = "sqlite:///{}".format(os.path.join(project_dir,"mydatabse.db"))
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
db = SQLAlchemy(app)
class Expense(db.Model):
id = db.Column(db.Integer,primary_key=True)
date = db.Column(db.String(50) , nullable=False)
expensename = db.Column(db.String(50) , nullable=False)
amount = db.Column(db.Integer , nullable=False)
category = db.Column(db.String(50) , nullable=False)
@app.route("/")
def add():
return render_template("add.html")
@app.route('/addexpense',methods=["post"])
def addexp():
date = request.form["date"]
expensename = request.form["expensename"]
amount = request.form["amount"]
category = request.form["category"]
print(date + " " + expensename + " " + amount + " " + category + " " + date)
expense = Expense(date = date , expensename = expensename , amount = amount , category = category)
db.session.add(expense)
db.session.commit()
return redirect ("/expenses")
@app.route('/deleteexp/<int:id>')
def delete(id):
expense = Expense.query.filter_by(id=id).first()
db.session.delete(expense)
db.session.commit()
return redirect("/expenses")
@app.route('/updateexp/<int:id>')
def updateexp(id):
expense = Expense.query.filter_by(id = id)
return render_template("edit.html",expense=expense)
@app.route('/edit',methods=['post'])
def edit():
id = request.form["id"]
date = request.form["date"]
expensename= request.form["expensename"]
amount = request.form["amount"]
category = request.form["category"]
expense = Expense.query.filter_by(id = id).first()
expense.date = date
expense.expensename = expensename
expense.amount = amount
expense.category = category
db.session.commit()
return redirect('/expenses')
@app.route('/expenses')
def expenses():
expenses = Expense.query.all()
total = 0
t_food = 0
t_entertainment = 0
t_stationary = 0
t_business = 0
t_travel = 0
t_groceries = 0
t_medical = 0
t_other = 0
for expense in expenses:
total += expense.amount
if expense.category == "food":
t_food += expense.amount
elif expense.category == "entertainment":
t_entertainment += expense.amount
elif expense.category == "stationary":
t_stationary += expense.amount
elif expense.category == "business":
t_business += expense.amount
elif expense.category == "travel":
t_travel += expense.amount
elif expense.category == "groceries":
t_groceries += expense.amount
elif expense.category == "medical":
t_medical += expense.amount
elif expense.category == "other":
t_other += expense.amount
return render_template('expenses.html',expenses = expenses , total = total , t_stationary = t_stationary, t_business = t_business , t_entertainment = t_entertainment , t_groceries = t_groceries , t_medical = t_medical , t_travel = t_travel , t_other = t_other , t_food = t_food)
if __name__ == '__main__':
app.run(debug=True)