Skip to content

Commit

Permalink
blueprint structure added
Browse files Browse the repository at this point in the history
  • Loading branch information
rokublac committed Aug 1, 2018
1 parent bf5713a commit 898cfea
Show file tree
Hide file tree
Showing 31 changed files with 353 additions and 301 deletions.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

2 changes: 2 additions & 0 deletions flaskblog/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# bytecode cache files
/__pycache__
44 changes: 28 additions & 16 deletions flaskblog/__init__.py
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 modified flaskblog/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified flaskblog/__pycache__/models.cpython-36.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions flaskblog/config.py
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
2 changes: 2 additions & 0 deletions flaskblog/main/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# bytecode cache files
/__pycache__
Empty file added flaskblog/main/__init__.py
Empty file.
Empty file added flaskblog/main/forms.py
Empty file.
21 changes: 21 additions & 0 deletions flaskblog/main/routes.py
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')
7 changes: 4 additions & 3 deletions flaskblog/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flaskblog import db, login_manager, app
from flaskblog import db, login_manager
from flask import current_app
from flask_login import UserMixin
from datetime import datetime
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
Expand All @@ -18,12 +19,12 @@ class User(db.Model, UserMixin):
posts = db.relationship('Post', backref='author', lazy=True)

def get_reset_token(self, expires_sec=1800):
s = Serializer(app.config['SECRET_KEY'], expires_sec)
s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
return s.dumps({'user_id':self.id}).decode('utf-8')

@staticmethod
def verify_reset_token(token):
s = Serializer(app.config['SECRET_KEY'])
s = Serializer(current_app.config['SECRET_KEY'])
try:
user_id = s.loads(token)['user_id']
except:
Expand Down
2 changes: 2 additions & 0 deletions flaskblog/posts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# bytecode cache files
/__pycache__
Empty file added flaskblog/posts/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions flaskblog/posts/forms.py
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')
64 changes: 64 additions & 0 deletions flaskblog/posts/routes.py
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'))
Loading

0 comments on commit 898cfea

Please sign in to comment.