-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
83 lines (72 loc) · 2.37 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
# this line imports functionality into our project, so we
# don't have to write it ourselves!
from flask import Flask, render_template, request, redirect
import sqlite3
import dotenv
import requests
def send_simple_message():
return requests.post(
"https://api.mailgun.net/v3/sandboxd6c2ea4c2c884250a549810fc39cb5c7.mailgun.org/messages",
auth=("api", "YOUR_API_KEY"),
data={"from": "Excited User <[email protected]>",
"to": ["[email protected]", "[email protected]"],
"subject": "Hello Likhith",
"text": "This is a remainder to Test!"})
app = Flask(__name__)
items = []
db_path = 'checklist.db'
def create_table():
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS checklist (id INTEGER PRIMARY KEY AUTOINCREMENT, item TEXT)''')
conn.commit()
conn.close()
def get_items():
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute("SELECT * FROM checklist")
items = c.fetchall()
conn.close()
return items
def add_item(item):
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute("INSERT INTO checklist (item) VALUES (?)", (item,))
conn.commit()
conn.close()
def update_item(item_id, new_item):
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute("UPDATE checklist SET item = ? WHERE id = ?", (new_item, item_id))
conn.commit()
conn.close()
def delete_item(item_id):
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute("DELETE FROM checklist WHERE id = ?", (item_id,))
conn.commit()
conn.close()
@app.route('/')
def checklist():
create_table()
items = get_items()
return render_template('checklist.html', items=items)
@app.route('/add', methods=['POST'])
def add():
item = request.form['item']
add_item(item)
return redirect('/')
@app.route('/edit/<int:item_id>', methods=['GET', 'POST'])
def edit(item_id):
if request.method == 'POST':
new_item = request.form['item']
update_item(item_id, new_item)
return redirect('/')
else:
items = get_items()
item = next((x[1] for x in items if x[0] == item_id), None)
return render_template('edit.html', item=item, item_id=item_id)
@app.route('/delete/<int:item_id>')
def delete(item_id):
delete_item(item_id)
return redirect('/')