Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
254 lines (205 loc) · 8.52 KB
/
Copy pathmain.py
File metadata and controls
254 lines (205 loc) · 8.52 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from datetime import date
from flask import Flask, abort, render_template, redirect, url_for, flash
from flask_bootstrap import Bootstrap5
from flask_ckeditor import CKEditor
from flask_gravatar import Gravatar
from flask_login import UserMixin, login_user, LoginManager, current_user, logout_user, login_required
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship, DeclarativeBase, Mapped, mapped_column
from sqlalchemy import Integer, String, Text, ForeignKey
from werkzeug.security import generate_password_hash, check_password_hash
from forms import CreatePostForm, RegisterForm, LoginForm, CommentForm
from typing import List
import os
# In the Procfile. file in this folder:
# This will tell our hosting provider to create a web worker that is able to receive HTTP requests.
# The Procfile also says to use gunicorn to serve your web app.
# And finally it specifies the Flask app object is the main.py file.
# That way the hosting provider knows about the entry point for the app and what our app is called.
# When uploading app to host provider like render, they'll automatically change the line from main:app to app:app
# You have to manually revert this
# App initialization
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('FLASK_KEY')
ckeditor = CKEditor(app)
Bootstrap5(app)
# Gravatar Initialization (random profile pics)
gravatar = Gravatar(app,
size=80,
rating='g',
default='retro',
force_default=False,
force_lower=False,
use_ssl=False,
base_url=None)
# DATABASE Initialization
class Base(DeclarativeBase):
pass
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DB_URI', 'sqlite:///posts.db')
db = SQLAlchemy(model_class=Base)
db.init_app(app)
# LOGIN Manager
login_manager = LoginManager()
login_manager.init_app(app)
def make_login(email, password):
user = User.query.filter_by(email=email).first()
if user and check_password_hash(user.password, password):
login_user(user)
return redirect(url_for('get_all_posts'))
else:
flash('Invalid email or password')
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
# DECORATOR TO MAKE ADMIN REQUIRED
def admin_required(f):
def wrap(*args, **kwargs):
if current_user.id == 1:
return f(*args, **kwargs)
else:
abort(403)
wrap.__name__ = f.__name__ # Necessary to wrap more than one function with decorator,
# otherwise: AssertionError: View function mapping is overwriting an existing endpoint function: wrap
# it is caused by trying to register a few functions with the name wrap:
return wrap
# CONFIGURE TABLES
class BlogPost(db.Model):
__tablename__ = "blog_posts"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
title: Mapped[str] = mapped_column(String(250), unique=True, nullable=False)
subtitle: Mapped[str] = mapped_column(String(250), nullable=False)
date: Mapped[str] = mapped_column(String(250), nullable=False)
body: Mapped[str] = mapped_column(Text, nullable=False)
img_url: Mapped[str] = mapped_column(String(250), nullable=False)
author: Mapped["User"] = relationship(back_populates="posts")
author_id: Mapped[int] = mapped_column(ForeignKey('users.id'), nullable=False)
comments: Mapped[List["Comment"]] = relationship(back_populates="parent_post")
class User(db.Model, UserMixin):
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(250), nullable=False)
email: Mapped[str] = mapped_column(String(250), unique=True, nullable=False)
password: Mapped[str] = mapped_column(String(250), nullable=False)
posts: Mapped[List["BlogPost"]] = relationship(back_populates="author")
comments: Mapped[List["Comment"]] = relationship(back_populates="author")
class Comment(db.Model):
__tablename__ = "comments"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
author_id: Mapped[int] = mapped_column(ForeignKey('users.id'), nullable=False)
author: Mapped["User"] = relationship(back_populates="comments")
post_id: Mapped[int] = mapped_column(ForeignKey('blog_posts.id'), nullable=False)
parent_post: Mapped["BlogPost"] = relationship(back_populates="comments")
body: Mapped[str] = mapped_column(Text, nullable=False)
with app.app_context():
db.create_all()
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
name = form.name.data
email = form.email.data
password = form.password.data
hashed_password = generate_password_hash(password, method='scrypt', salt_length=8)
try:
db.session.add(User(name=name, email=email, password=hashed_password))
db.session.commit()
except Exception as e:
db.session.rollback()
flash(f'This account has already been registered, log in here instead')
return redirect(url_for('login'))
else:
if make_login(email=email, password=password):
# flash(f'You are now registered with {name}')
return redirect(url_for('get_all_posts'))
return render_template("register.html", form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
email = form.email.data
password = form.password.data
make_login(email, password)
return redirect(url_for('get_all_posts'))
return render_template("login.html", form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('get_all_posts'))
@app.route('/')
def get_all_posts():
result = db.session.execute(db.select(BlogPost))
posts = result.scalars().all()
return render_template("index.html", all_posts=posts)
@app.route("/post/<int:post_id>", methods=['GET', 'POST'])
def show_post(post_id):
form = CommentForm()
requested_post = db.get_or_404(BlogPost, post_id)
comments = requested_post.comments
if form.validate_on_submit():
if current_user.is_anonymous:
return redirect(url_for('login'))
else:
new_comment = Comment(
author=current_user,
body=form.body.data,
parent_post=requested_post,
)
db.session.add(new_comment)
db.session.commit()
return redirect(url_for('show_post', post_id=post_id))
return render_template("post.html", post=requested_post, form=form)
@app.route("/new-post", methods=["GET", "POST"])
@admin_required
def add_new_post():
form = CreatePostForm()
if form.validate_on_submit():
new_post = BlogPost(
title=form.title.data,
subtitle=form.subtitle.data,
body=form.body.data,
img_url=form.img_url.data,
author=current_user,
date=date.today().strftime("%B %d, %Y")
)
db.session.add(new_post)
db.session.commit()
return redirect(url_for("get_all_posts"))
return render_template("make-post.html", form=form)
# TODO: Use a decorator so only an admin user can edit a post
@app.route("/edit-post/<int:post_id>", methods=["GET", "POST"])
@admin_required
def edit_post(post_id):
post = db.get_or_404(BlogPost, post_id)
edit_form = CreatePostForm(
title=post.title,
subtitle=post.subtitle,
img_url=post.img_url,
author=post.author,
body=post.body
)
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.author = current_user
post.body = edit_form.body.data
db.session.commit()
return redirect(url_for("show_post", post_id=post.id))
return render_template("make-post.html", form=edit_form, is_edit=True)
# TODO: Use a decorator so only an admin user can delete a post
@app.route("/delete/<int:post_id>")
@admin_required
def delete_post(post_id):
post_to_delete = db.get_or_404(BlogPost, post_id)
db.session.delete(post_to_delete)
db.session.commit()
return redirect(url_for('get_all_posts'))
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
if __name__ == "__main__":
app.run(debug=False, port=5002)