-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.py
116 lines (78 loc) · 2.83 KB
/
blog.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
This will be a template for starting flask projects
"""
from flask import Flask, url_for, render_template, request, redirect
from werkzeug.security import generate_password_hash, check_password_hash
#import encryption as e
from hashlib import blake2b
from markupsafe import escape
import sqlite3
from create_post import *
from datetime import date
from manage_db import *
app = Flask(__name__)
@app.route('/test')
def test():
return render_template('test.html')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/page2')
def page2():
return render_template('page2.html')
#When a user clicks on a link to a post, there will have to be a way to determine which row to take from the database
#for now the page can always be /blog_post, but the vals array will be loaded with the data based on which article it is.
@app.route('/blog_post<id>')
def blog_post(id):
"""Gets all relevant info from the database and renders a template with it. """
conn = sqlite3.connect('blog.db')
c = conn.cursor()
c.execute("SELECT * from blog_posts;")
vals = []
vals = c.fetchall()
c.close()
index = int(id)
return render_template('blog_post.html', title = vals[index][1] ,content = vals[index][3])
@app.route('/user')
def user():
return render_template('user.html')
@app.route('/login',methods=['POST', 'GET'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'flaskword':
error = 'Invalid Credentials. Please try again.'
else:
return redirect(url_for('index'))
return render_template('login.html', error=error)
@app.route('/register', methods=['POST', 'GET'])
def register():
error = None
if request.method == 'POST':
conn = sqlite3.connect('blog.db')
c = conn.cursor()
username = request.form['username']
password = request.form['password']
pw = generate_password_hash(password)
c.execute("INSERT INTO users VALUES ('{}','{}!')".format(username, pw))
conn.commit()
conn.close()
return render_template('register.html', error=error)
@app.route('/create', methods=('GET', 'POST'))
def create():
val = []
if request.method == 'POST':
title = request.form['title']
body = request.form['body']
error = None
if not title:
error = 'Title is required.'
if error is not None:
flash(error)
else:
conn = sqlite3.connect('blog.db')
c = conn.cursor()
c.execute("INSERT INTO posts VALUES(NULL, ?, ?, ?);", (title, date.today(), body))
conn.commit()
conn.close()
return render_template('create.html')