-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (98 loc) · 3.83 KB
/
main.py
File metadata and controls
117 lines (98 loc) · 3.83 KB
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
117
from flask import Flask, render_template, redirect, url_for, request
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, URL
from flask_ckeditor import CKEditor, CKEditorField
from datetime import datetime
## Delete this code:
# import requests
# posts = requests.get("https://api.npoint.io/43644ec4f0013682fc0d").json()
app = Flask(__name__)
app.config['SECRET_KEY'] = '8BYkEfBA6O6donzWlSihBXox7C0sKR6b'
ckeditor = CKEditor(app)
Bootstrap(app)
##CONNECT TO DB
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///posts.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
##CONFIGURE TABLE
class BlogPost(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(250), unique=True, nullable=False)
subtitle = db.Column(db.String(250), nullable=False)
date = db.Column(db.String(250), nullable=False)
body = db.Column(db.Text, nullable=False)
author = db.Column(db.String(250), nullable=False)
img_url = db.Column(db.String(250), nullable=False)
##WTForm
class CreatePostForm(FlaskForm):
title = StringField("Blog Post Title", validators=[DataRequired()])
subtitle = StringField("Subtitle", validators=[DataRequired()])
author = StringField("Your Name", validators=[DataRequired()])
img_url = StringField("Blog Image URL", validators=[DataRequired(), URL()])
# body = StringField("Blog Content", validators=[DataRequired()])
body = CKEditorField("Blog Content", validators=[DataRequired()])
submit = SubmitField("Submit Post")
@app.route('/')
def get_all_posts():
posts = BlogPost.query.all()
return render_template("index.html", all_posts=posts)
@app.route("/post/<int:index>")
def show_post(index):
requested_post = None
posts = BlogPost.query.all()
for blog_post in posts:
if blog_post.id == index:
requested_post = blog_post
return render_template("post.html", post=requested_post)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
@app.route("/new-post", methods = ["POST", "GET"])
def new_post():
post_form = CreatePostForm()
if post_form.validate_on_submit():
new_blog = BlogPost(
title = post_form.title.data,
subtitle = post_form.subtitle.data,
img_url = post_form.img_url.data,
body = post_form.body.data,
author = post_form.author.data,
date = datetime.now().strftime("%B %d, %Y")
)
db.session.add(new_blog)
db.session.commit()
return redirect(url_for("get_all_posts"))
return render_template("make-post.html", post_form = post_form, is_edit = False)
@app.route("/edit-post/<int:post_id>", methods = ["GET", "POST"])
def edit_post(post_id):
post = BlogPost.query.get(post_id)
edit_form = CreatePostForm(
title = post.title,
subtitle = post.subtitle,
img_url = post.img_url,
body = post.body,
author = post.author,
)
if edit_form.validate_on_submit():
post.title = edit_form.title.data
post.subtitle = edit_form.subtitle.data
post.img_url = edit_form.img_url.data
post.body = edit_form.body.data
post.author = edit_form.author.data
db.session.commit()
return redirect(url_for("show_post", index = post_id))
return render_template("make-post.html", post_form = edit_form, is_edit = True)
@app.route("/delete/<int:post_id>")
def delete(post_id):
delete_post = BlogPost.query.get(post_id)
db.session.delete(delete_post)
db.session.commit()
return redirect(url_for("get_all_posts"))
if __name__ == "__main__":
app.run(debug = True)