-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (50 loc) · 1.8 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
"""
flask app that shows a list of ideas from a database
the database is a sqlite database
the app has a form to add new ideas
each ideas is automatically deleted after 30 days
"""
from datetime import datetime #, timedelta
import shelve
from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask, render_template, request, redirect
DATABASE_PATH = 'shelve-database.db'
# intialize the shelve database
with shelve.open(DATABASE_PATH) as db:
try:
db["ideas"]
except KeyError:
db["ideas"] = []
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
idea_text = request.form['ideatext']
print(f"idea added: {idea_text}")
try:
with shelve.open(DATABASE_PATH, writeback=True) as shelf:
shelf["ideas"].append({"idea_time": datetime.now(), "idea_text": idea_text})
return redirect('/')
except:
return 'There was an issue adding your idea'
else:
with shelve.open(DATABASE_PATH, writeback=True) as shelf:
ideas = shelf["ideas"]
return render_template('index.html', ideas=ideas)
@app.route('/about')
def about():
return render_template('about.html')
def delete_old_ideas():
"delete ideas older than 30 days"
with shelve.open(DATABASE_PATH, writeback=True) as shelf:
ideas = shelf["ideas"]
for idea in ideas:
if (datetime.now() - idea["idea_time"]).days > 30:
print(f"idea deleted: {idea['idea_text']}")
ideas.remove(idea)
shelf["ideas"] = ideas
if __name__ == "__main__":
deleter = BackgroundScheduler()
deleter.add_job(func=delete_old_ideas, trigger="interval", seconds=800)
deleter.start()
app.run(debug=True)