-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
77 lines (57 loc) · 2.21 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
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
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
import os
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(35), nullable=False)
lastname = db.Column(db.String(35), nullable=False)
email = db.Column(db.String(50), nullable=False)
def __repr__(self):
return f'<Author: {self.firstname}>'
@app.route('/')
def index():
authors = Author.query.all()
return render_template("index.html", authors=authors)
@app.route('/<int:author_id>')
def author(author_id):
author = Author.query.get_or_404(author_id)
return render_template('author.html', author=author)
@app.route('/create/', methods=['GET', 'POST'])
def create():
if request.method == "POST":
firstname = request.form['firstname']
lastname = request.form['lastname']
email = request.form['email']
author = Author(firstname=firstname, lastname=lastname, email=email)
db.session.add(author)
db.session.commit()
return redirect(url_for('index'))
return render_template("create.html")
@app.route('/<int:author_id>/edit', methods=['GET', "POST"])
def edit(author_id):
author = Author.query.get_or_404(author_id)
if request.method == 'POST':
firstname = request.form['firstname']
lastname = request.form['lastname']
email = request.form['email']
author.firstname = firstname
author.lastname = lastname
author.email = email
db.session.add(author)
db.session.commit()
return redirect(url_for('index'))
return render_template('edit.html', author=author)
@app.route('/<int:author_id>/delete', methods=['GET', 'POST'])
def delete(author_id):
author = Author.query.get_or_404(author_id)
db.session.delete(author)
db.session.commit()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)