-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
353 additions
and
301 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# bytecode cache files | ||
/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
from flask_bcrypt import Bcrypt | ||
from flask_login import LoginManager | ||
from flask_mail import Mail | ||
from flaskblog.config import Config | ||
|
||
|
||
app = Flask(__name__) | ||
app.config['SECRET_KEY'] = '2f6d03141c9438db59078d7c786e9a3a' # generated with secrets.token_hex(16) | ||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' | ||
# create database instance | ||
db = SQLAlchemy(app) | ||
db = SQLAlchemy() | ||
# create brcypt instance | ||
bcrypt = Bcrypt(app) | ||
bcrypt = Bcrypt() | ||
# creat login manager instance | ||
login_manager = LoginManager(app) | ||
login_manager.login_view = 'login' | ||
login_manager = LoginManager() | ||
login_manager.login_view = 'users.login' | ||
login_manager.login_message_category = 'info' | ||
# email config for password reset | ||
app.config['MAIL_SERVER'] = 'smtp.googlemail.com' | ||
app.config['MAIL_PORT'] = '587' | ||
app.config['MAIL_USE_TLS'] = True | ||
app.config['MAIL_USERNAME'] = 'testingrokublak@gmail.com' | ||
app.config['MAIL_PASSWORD'] = 'test1234!' | ||
mail = Mail(app) | ||
# forgot/reset password email system | ||
mail = Mail() | ||
|
||
from flaskblog import routes | ||
|
||
def create_app(config_class=Config): | ||
app = Flask(__name__) | ||
# import Config class from config.py | ||
app.config.from_object(Config) | ||
|
||
# extensions | ||
db.init_app(app) | ||
bcrypt.init_app(app) | ||
login_manager.init_app(app) | ||
mail.init_app(app) | ||
|
||
# ====== import blueprints ====== | ||
from flaskblog.main.routes import main | ||
from flaskblog.users.routes import users | ||
from flaskblog.posts.routes import posts | ||
app.register_blueprint(main) | ||
app.register_blueprint(users) | ||
app.register_blueprint(posts) | ||
|
||
return app |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import os | ||
|
||
|
||
class Config: | ||
SECRET_KEY = '2f6d03141c9438db59078d7c786e9a3a' # generated with secrets.token_hex(16) - and you would prbobably not hardcode these... | ||
SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db' | ||
# email config for password reset | ||
MAIL_SERVER = 'smtp.googlemail.com' | ||
MAIL_PORT = '587' | ||
MAIL_USE_TLS = True | ||
MAIL_USERNAME = 'testingrokublak@gmail.com' # hardcoded for testing | ||
MAIL_PASSWORD = 'test1234!' # hardcoded for testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# bytecode cache files | ||
/__pycache__ |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from flask import Blueprint, render_template, request | ||
from flaskblog.models import Post | ||
|
||
|
||
main = Blueprint('main', __name__) | ||
|
||
|
||
# Homepage | ||
@main.route('/') | ||
@main.route('/home') | ||
def home(): | ||
# grab page number from URL is user types one in | ||
page = request.args.get('page', 1, type=int) | ||
# only show 5 pages per page, desc by date | ||
posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5) | ||
return render_template('home.html', posts=posts) | ||
|
||
# About page | ||
@main.route('/about') | ||
def about(): | ||
return render_template('about.html', title='About') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# bytecode cache files | ||
/__pycache__ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from flask_wtf import FlaskForm | ||
from wtforms import StringField, TextAreaField, SubmitField | ||
from wtforms.validators import DataRequired | ||
|
||
|
||
|
||
|
||
class PostForm(FlaskForm): | ||
title = StringField('Title', validators=[DataRequired()]) | ||
content = TextAreaField('Content', validators=[DataRequired()]) | ||
submit = SubmitField('Post') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from flask import (Blueprint, render_template, | ||
url_for, flash, redirect, request, abort) | ||
from flask_login import current_user, login_required | ||
from flaskblog import db | ||
from flaskblog.models import Post | ||
from flaskblog.posts.forms import PostForm | ||
|
||
|
||
posts = Blueprint('posts', __name__) | ||
|
||
|
||
# Add new post | ||
@posts.route('/posts/new', methods=['GET', 'POST']) | ||
@login_required | ||
def new_post(): | ||
postForm = PostForm() | ||
if postForm.validate_on_submit(): | ||
post = Post(title=postForm.title.data, content=postForm.content.data, author=current_user) | ||
db.session.add(post) | ||
db.session.commit() | ||
flash('Your post has been create!', 'success') | ||
return redirect(url_for('main.home')) | ||
return render_template('create_post.html', title='New Post', form=postForm, legend='New Post') | ||
|
||
|
||
# single post route | ||
@posts.route("/post/<int:post_id>") | ||
def post(post_id): | ||
post = Post.query.get_or_404(post_id) | ||
return render_template('post.html', title=post.title, post=post) | ||
|
||
|
||
# update posts | ||
@posts.route("/post/<int:post_id>/update", methods=['GET', 'POST']) | ||
@login_required | ||
def update_post(post_id): | ||
post = Post.query.get_or_404(post_id) | ||
if post.author != current_user: | ||
abort(403) | ||
update_post_form = PostForm() | ||
if update_post_form.validate_on_submit(): | ||
post.title = update_post_form.title.data | ||
post.content = update_post_form.content.data | ||
db.session.commit() | ||
flash('Your post has been updated!', 'success') | ||
return redirect(url_for('posts.post', post_id=post.id)) | ||
elif request.method == 'GET': | ||
update_post_form.title.data = post.title | ||
update_post_form.content.data = post.content | ||
return render_template('create_post.html', title='Update Post', | ||
form=update_post_form, legend='Update Post') | ||
|
||
|
||
# delete post | ||
@posts.route("/post/<int:post_id>/delete", methods=['POST']) | ||
@login_required | ||
def delete_post(post_id): | ||
post = Post.query.get_or_404(post_id) | ||
if post.author != current_user: | ||
abort(403) | ||
db.session.delete(post) | ||
db.session.commit() | ||
flash('Your post has been successfully deleted.', 'success') | ||
return redirect(url_for('main.home')) |
Oops, something went wrong.