main
parent
195bcfa90a
commit
15f824e2b7
48
main.py
48
main.py
|
@ -1,8 +1,8 @@
|
||||||
from flask import Flask, render_template, redirect, request, session, url_for
|
from flask import Flask, render_template, redirect, request, session, url_for
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_admin import Admin, AdminIndexView, expose
|
from flask_admin import Admin, AdminIndexView, expose, BaseView
|
||||||
from flask_admin.contrib.sqla import ModelView
|
from flask_admin.contrib.sqla import ModelView
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = 'bebra'
|
app.secret_key = 'bebra'
|
||||||
|
@ -10,23 +10,32 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///Picture_Puzzle_web.db'
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
class User(db.Model):
|
class User(db.Model):
|
||||||
|
__tablename__ = 'user'
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
username = db.Column(db.String(100), unique=True, nullable=False)
|
username = db.Column(db.String(100), unique=True, nullable=False)
|
||||||
password = db.Column(db.String(100), nullable=False)
|
password = db.Column(db.String(100), nullable=False)
|
||||||
email = db.Column(db.String(100), unique=True, nullable=False)
|
email = db.Column(db.String(100), unique=True, nullable=False)
|
||||||
|
|
||||||
class Post(db.Model):
|
class Post(db.Model):
|
||||||
|
__tablename__ = 'post'
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
date_created = db.Column(db.String(100), unique=False, nullable=False)
|
date_created = db.Column(db.String(100), unique=False, nullable=False)
|
||||||
alias = db.Column(db.String(100), unique=True, nullable=False)
|
alias = db.Column(db.String(100), unique=True, nullable=False)
|
||||||
title = db.Column(db.String(100), nullable=False)
|
title = db.Column(db.String(100), nullable=False)
|
||||||
image = db.Column(db.String(100), nullable=False)
|
image = db.Column(db.String(100), nullable=False)
|
||||||
|
|
||||||
class MyAdminIndexView(AdminIndexView):
|
def admin_login_required(view_func):
|
||||||
@expose('/')
|
@wraps(view_func)
|
||||||
def index(self):
|
def decorated_function(*args, **kwargs):
|
||||||
if not session.get('admin_logged_in'):
|
if not session.get('admin_logged_in'):
|
||||||
return redirect(url_for('admin_login'))
|
return redirect(url_for('admin_login'))
|
||||||
|
return view_func(*args, **kwargs)
|
||||||
|
return decorated_function
|
||||||
|
|
||||||
|
class MyAdminIndexView(AdminIndexView):
|
||||||
|
@expose('/')
|
||||||
|
@admin_login_required
|
||||||
|
def index(self):
|
||||||
return self.render('admin/index.html')
|
return self.render('admin/index.html')
|
||||||
|
|
||||||
class UserAdminView(ModelView):
|
class UserAdminView(ModelView):
|
||||||
|
@ -34,21 +43,32 @@ class UserAdminView(ModelView):
|
||||||
form_excluded_columns = ['password']
|
form_excluded_columns = ['password']
|
||||||
|
|
||||||
class PostAdminView(ModelView):
|
class PostAdminView(ModelView):
|
||||||
pass
|
|
||||||
|
|
||||||
class TableAdminView(ModelView):
|
|
||||||
can_delete = True
|
|
||||||
can_create = True
|
|
||||||
can_edit = True
|
can_edit = True
|
||||||
column_display_pk = True
|
can_delete = True
|
||||||
|
create_modal = True
|
||||||
|
edit_modal = True
|
||||||
|
can_export = True
|
||||||
|
export_types = ['csv']
|
||||||
|
|
||||||
|
class LogoutView(BaseView):
|
||||||
|
@expose('/')
|
||||||
|
def index(self):
|
||||||
|
session.pop("admin_logged_in", None)
|
||||||
|
return redirect(url_for("index"))
|
||||||
|
|
||||||
admin = Admin(app, name='Admin Panel', template_mode='bootstrap3', index_view=MyAdminIndexView())
|
admin = Admin(app, name='Admin Panel', template_mode='bootstrap3', index_view=MyAdminIndexView())
|
||||||
|
|
||||||
admin.add_view(UserAdminView(User, db.session))
|
admin.add_view(UserAdminView(User, db.session))
|
||||||
admin.add_view(PostAdminView(Post, db.session))
|
admin.add_view(PostAdminView(Post, db.session))
|
||||||
|
admin.add_view(LogoutView(name='Logout', endpoint='admin_logout'))
|
||||||
|
|
||||||
|
@app.before_request
|
||||||
|
def check_admin_login():
|
||||||
|
if request.path.startswith('/admin/') and not session.get('admin_logged_in'):
|
||||||
|
if request.path != '/admin/login' and request.path != '/admin/logout':
|
||||||
|
return redirect(url_for('admin_login'))
|
||||||
|
|
||||||
ADMIN_USERNAME = 'user'
|
ADMIN_USERNAME = 'user'
|
||||||
ADMIN_PASSWORD = '1234'
|
ADMIN_PASSWORD = '1234321'
|
||||||
|
|
||||||
@app.route("/admin/login", methods=["GET", "POST"])
|
@app.route("/admin/login", methods=["GET", "POST"])
|
||||||
def admin_login():
|
def admin_login():
|
||||||
|
@ -65,7 +85,7 @@ def admin_login():
|
||||||
@app.route("/admin/logout")
|
@app.route("/admin/logout")
|
||||||
def admin_logout():
|
def admin_logout():
|
||||||
session.pop("admin_logged_in", None)
|
session.pop("admin_logged_in", None)
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("admin_login"))
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
|
|
Loading…
Reference in New Issue