-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
88 lines (55 loc) · 2 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
84
85
86
87
88
import os
from datetime import timedelta
from flask import Flask, render_template, request, url_for, flash, redirect
import sqlite3 as sql
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24).hex()
app.permanent_session_lifetime = timedelta(hours=2)
def get_db_connection():
connection = sql.connect('db/database.db')
connection.row_factory = sql.Row
return connection
def add_post(title, content):
if request.method == 'POST':
connection = sql.connect('db/database.db')
with open('db/schema.sql') as f:
connection.executescript(f.read())
cursor = connection.cursor()
title = request.form['title']
content = request.form['content']
cursor.execute(f"INSERT INTO posts (title, content) VALUES (?, ?)", (f'{title}', f'{content}'))
connection.commit()
connection.close()
@app.route('/')
def home(): # put application's code here
conn = get_db_connection()
posts = conn.execute('SELECT * FROM posts').fetchall()
conn.close()
return render_template("index.html", posts=posts)
@app.route('/about')
def about(): # put application's code here
return render_template('about.html')
@app.route('/contact')
def contact(): # put application's code here
return render_template("contact.html")
@app.route('/login')
def login(): # put application's code here
return render_template("login.html")
@app.route('/create', methods=['GET', 'POST'])
def create():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
if not title:
flash('Title is required!')
elif not content:
flash('Content is required!')
else:
add_post(title, content)
return redirect(url_for('home'))
return render_template("create.html")
@app.route('/logout')
def logout(): # put application's code here
pass
if __name__ == '__main__':
app.run(host="localhost",port=3000, debug=True)