-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
92 lines (75 loc) · 2.59 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
from flask import Flask,request,redirect
from flask.templating import 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,"mydatabase.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('/expenses')
def expenses():
expenses=Expense.query.all()
total=0
t_business=0
t_other=0
t_food=0
t_entertainment=0
for expense in expenses:
total+=expense.amount
if(expense.category=='business'):
t_business+=expense.amount
elif(expense.category=='other'):
t_other+=expense.amount
elif(expense.category=='food'):
t_food+=expense.amount
elif(expense.category=='entertainment'):
t_entertainment+=expense.amount
return render_template('expenses.html', expenses=expenses,total=total,t_business=t_business,t_other=t_other,t_food=t_food,t_entertainment=t_entertainment)
@app.route("/addexpense", methods=["POST"])
def addexpense():
date= request.form['date']
expensename=request.form['expensename']
amount=request.form['amount']
category=request.form['category']
expense =Expense(date=date,expensename=expensename,amount=amount,category=category)
db.session.add(expense)
db.session.commit()
return redirect("/expenses")
@app.route("/delete/<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('/update/<int:id>')
def update(id):
expense=Expense.query.filter_by(id=id).first()
return render_template("update.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")
if __name__=='__main__':
app.run(debug=True)