blueprint and fabric

This commit is contained in:
Dmitry Belyaev 2020-07-04 17:11:16 +03:00
parent eb9a593fd5
commit 4c797278e3
7 changed files with 57 additions and 30 deletions

View File

@ -1,21 +1,29 @@
from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager, Command, Shell
from flask_login import LoginManager
import os, config
db = SQLAlchemy()
migrate = Migrate()
login_manager = LoginManager()
login_manager.login_view = 'main.login'
# Фабрика приложения
def create_app(cfg) -> Flask:
# создание экземпляра приложения
app = Flask(__name__)
app.config.from_object(os.environ.get('FLASK_ENV') or 'config.DevelopementConfig')
app.config.from_object(cfg)
# инициализирует расширения
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
db.init_app(app)
migrate.init_app(app, db)
login_manager.init_app(app)
# import views
from . import views
# from . import forum_views
# from . import admin_views
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
# from .admin import main as admin_blueprint
# app.register_blueprint(admin_blueprint)
return app

5
app/main/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from flask import Blueprint
main = Blueprint('main', __name__)
from . import views

View File

@ -1,46 +1,58 @@
from app import app
from flask import render_template, request, redirect, url_for, flash
from . import main
from flask import Flask, request, render_template, redirect, url_for, flash, make_response, session
from flask_login import login_required, login_user, current_user, logout_user
from .models import User, Post, Category, Feedback, db
from app.models import User, Post, Category, Feedback, db
from .forms import ContactForm, LoginForm
from werkzeug.exceptions import abort
@app.route('/admin/')
@main.route('/admin/')
@login_required
def admin():
return render_template('admin.html')
@app.route('/')
@main.route('/')
def index():
return render_template('index.html')
name = None
if current_user.is_authenticated:
name = current_user.name
return render_template('index.html', name=name)
@app.route('/login/', methods=['post', 'get'])
@main.route('/login/', methods=['post', 'get'])
def login():
if current_user.is_authenticated:
return redirect(url_for('admin'))
return redirect(url_for('.admin'))
form = LoginForm()
if form.validate_on_submit():
user = db.session.query(User).filter(User.username == form.username.data).first()
if user and user.check_password(form.password.data):
login_user(user, remember=form.remember.data)
nextpage = request.args.get('next', url_for('admin'))
nextpage = request.args.get('next', url_for('.admin'))
return redirect(nextpage)
else:
flash("Invalid username/password", 'error')
return render_template('login.html', form=form)
@app.route('/logout/')
@main.route('/user/<int:user_id>/')
def user_profile(user_id):
user = db.session.query(User).filter(User.id == user_id).first()
if not user:
abort(404)
return "Profile for user: {}".format(user.name)
@main.route('/logout/')
@login_required
def logout():
logout_user()
flash("You have been logged out.")
return redirect(url_for('login'))
return redirect(url_for('.login'))
@app.route('/contact/', methods=['get', 'post'])
@main.route('/contact/', methods=['get', 'post'])
def contact():
form = ContactForm()
if form.validate_on_submit():
@ -57,6 +69,6 @@ def contact():
print("\nData received. Now redirecting ...")
flash("Message Received", "success")
return redirect(url_for('contact'))
return redirect(url_for('.contact'))
return render_template('contact.html', form=form)

View File

@ -15,7 +15,7 @@
<li>Updated on: {{ current_user.updated_on }}</li>
</ul>
<p><a href="{{ url_for('logout') }}">Logout</a></p>
<p><a href="{{ url_for('.logout') }}">Logout</a></p>
</body>
</html>

View File

@ -5,6 +5,7 @@
<title>Hello</title>
</head>
<body>
<h2>Hello{% if name %},&nbsp;<p>{{ name }}</p>{% endif %}</h2>
<h2>Hello{% if name %},&nbsp;<span>{{ name }}</span>{% endif %}</h2></body><br/>
<p><a href="{{ url_for('.admin') }}">Admin</a></p>
</body>
</html>

View File

@ -4,11 +4,12 @@ import flask
from flask import request, current_app, url_for
from jinja2 import Template
from app import app, db
from app import create_app, db
from app.models import User, Post, Tag, Category, Feedback
from flask_script import Manager, Shell
from flask_migrate import MigrateCommand
app = create_app(os.getenv('FLASK_ENV') or 'config.DevelopementConfig')
manager = Manager(app)