-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (38 loc) · 1.55 KB
/
main.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
from bottle import route, run, template,view, static_file,request,post, redirect,get
import sqlite3
DB_NAME = "expensetracker.db"
con = sqlite3.connect(DB_NAME)
def expense_tuple_to_dict(expense_tuple):
return {"id":expense_tuple[0],"description":expense_tuple[1],"amount":expense_tuple[2],"date":expense_tuple[3],"category":expense_tuple[4]}
@get('/expenses')
@view('hello_template')
def hello():
cursor = con.execute("SELECT * FROM expenses")
expenses = []
for row in cursor:
print(expense_tuple_to_dict(row))
expenses.append(expense_tuple_to_dict(row))
return {"name":"Ciao a tutti",'expenses':expenses}
@post("/expenses")
def create_expense():
if request.forms.get('_method') == 'delete':
return delete_expense(request.forms.get('id'))
data = {
"date": request.forms.get('date'),
"description" : request.forms.get('description'),
"amount": request.forms.get('amount'),
"category": request.forms.get('category')
}
print(request.forms)
cursor = con.execute("INSERT INTO expenses (date,description,amount,category) VALUES (?,?,?,?)",
(data["date"],data["description"],data["amount"],data["category"]))
con.commit()
return redirect("/expenses")
def delete_expense(id):
cursor = con.execute("DELETE FROM expenses WHERE id = ?",(id,))
con.commit()
return redirect("/expenses")
@route('/static/<filename:path>')
def send_static(filename):
return static_file(filename, root='static/')
run(host='localhost', port=8080, debug=True)