-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
70 lines (52 loc) · 1.77 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
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Plan(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
created_at = db.Column(db.DateTime, nullable=False,
default=datetime.utcnow)
def __repr__(self):
return '<Plan %r>' % self.name
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
name = request.form['name']
new_stuff = Plan(name=name)
try:
db.session.add(new_stuff)
db.session.commit()
return redirect('/')
except:
return "There was a problem adding new stuff."
else:
plan = Plan.query.order_by(Plan.created_at).all()
return render_template('index.html', plan=plan)
@app.route('/delete/<int:id>')
def delete(id):
plan = Plan.query.get_or_404(id)
try:
db.session.delete(grocery)
db.session.commit()
return redirect('/')
except:
return "There was a problem deleting data."
@app.route('/update/<int:id>', methods=['GET', 'POST'])
def update(id):
plan = Plan.query.get_or_404(id)
if request.method == 'POST':
plan.name = request.form['name']
try:
db.session.commit()
return redirect('/')
except:
return "There was a problem updating data."
else:
title = "Update Data"
return render_template('update.html', title=title, plan=plan)
if __name__ == '__main__':
app.run(debug=True)