blueprint and fabric
This commit is contained in:
parent
eb9a593fd5
commit
4c797278e3
@ -1,21 +1,29 @@
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask_migrate import Migrate, MigrateCommand
|
from flask_migrate import Migrate, MigrateCommand
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_script import Manager, Command, Shell
|
|
||||||
from flask_login import LoginManager
|
from flask_login import LoginManager
|
||||||
import os, config
|
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 = Flask(__name__)
|
||||||
app.config.from_object(os.environ.get('FLASK_ENV') or 'config.DevelopementConfig')
|
app.config.from_object(cfg)
|
||||||
|
|
||||||
# инициализирует расширения
|
db.init_app(app)
|
||||||
db = SQLAlchemy(app)
|
migrate.init_app(app, db)
|
||||||
migrate = Migrate(app, db)
|
login_manager.init_app(app)
|
||||||
login_manager = LoginManager(app)
|
|
||||||
login_manager.login_view = 'login'
|
|
||||||
|
|
||||||
# import views
|
from .main import main as main_blueprint
|
||||||
from . import views
|
app.register_blueprint(main_blueprint)
|
||||||
# from . import forum_views
|
|
||||||
# from . import admin_views
|
# from .admin import main as admin_blueprint
|
||||||
|
# app.register_blueprint(admin_blueprint)
|
||||||
|
|
||||||
|
return app
|
||||||
|
5
app/main/__init__.py
Normal file
5
app/main/__init__.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from flask import Blueprint
|
||||||
|
|
||||||
|
main = Blueprint('main', __name__)
|
||||||
|
|
||||||
|
from . import views
|
@ -1,46 +1,58 @@
|
|||||||
from app import app
|
from . import main
|
||||||
from flask import render_template, request, redirect, url_for, flash
|
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 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 .forms import ContactForm, LoginForm
|
||||||
|
from werkzeug.exceptions import abort
|
||||||
|
|
||||||
|
|
||||||
@app.route('/admin/')
|
@main.route('/admin/')
|
||||||
@login_required
|
@login_required
|
||||||
def admin():
|
def admin():
|
||||||
return render_template('admin.html')
|
return render_template('admin.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@main.route('/')
|
||||||
def index():
|
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():
|
def login():
|
||||||
if current_user.is_authenticated:
|
if current_user.is_authenticated:
|
||||||
return redirect(url_for('admin'))
|
return redirect(url_for('.admin'))
|
||||||
form = LoginForm()
|
form = LoginForm()
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
user = db.session.query(User).filter(User.username == form.username.data).first()
|
user = db.session.query(User).filter(User.username == form.username.data).first()
|
||||||
if user and user.check_password(form.password.data):
|
if user and user.check_password(form.password.data):
|
||||||
login_user(user, remember=form.remember.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)
|
return redirect(nextpage)
|
||||||
else:
|
else:
|
||||||
flash("Invalid username/password", 'error')
|
flash("Invalid username/password", 'error')
|
||||||
return render_template('login.html', form=form)
|
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
|
@login_required
|
||||||
def logout():
|
def logout():
|
||||||
logout_user()
|
logout_user()
|
||||||
flash("You have been logged out.")
|
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():
|
def contact():
|
||||||
form = ContactForm()
|
form = ContactForm()
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
@ -57,6 +69,6 @@ def contact():
|
|||||||
|
|
||||||
print("\nData received. Now redirecting ...")
|
print("\nData received. Now redirecting ...")
|
||||||
flash("Message Received", "success")
|
flash("Message Received", "success")
|
||||||
return redirect(url_for('contact'))
|
return redirect(url_for('.contact'))
|
||||||
|
|
||||||
return render_template('contact.html', form=form)
|
return render_template('contact.html', form=form)
|
@ -15,7 +15,7 @@
|
|||||||
<li>Updated on: {{ current_user.updated_on }}</li>
|
<li>Updated on: {{ current_user.updated_on }}</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<p><a href="{{ url_for('logout') }}">Logout</a></p>
|
<p><a href="{{ url_for('.logout') }}">Logout</a></p>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -5,6 +5,7 @@
|
|||||||
<title>Hello</title>
|
<title>Hello</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h2>Hello{% if name %}, <p>{{ name }}</p>{% endif %}</h2>
|
<h2>Hello{% if name %}, <span>{{ name }}</span>{% endif %}</h2></body><br/>
|
||||||
|
<p><a href="{{ url_for('.admin') }}">Admin</a></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -4,11 +4,12 @@ import flask
|
|||||||
from flask import request, current_app, url_for
|
from flask import request, current_app, url_for
|
||||||
from jinja2 import Template
|
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 app.models import User, Post, Tag, Category, Feedback
|
||||||
from flask_script import Manager, Shell
|
from flask_script import Manager, Shell
|
||||||
from flask_migrate import MigrateCommand
|
from flask_migrate import MigrateCommand
|
||||||
|
|
||||||
|
app = create_app(os.getenv('FLASK_ENV') or 'config.DevelopementConfig')
|
||||||
manager = Manager(app)
|
manager = Manager(app)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user