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_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 functools import wraps
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = 'bebra'
|
||||
|
@ -10,23 +10,32 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///Picture_Puzzle_web.db'
|
|||
db = SQLAlchemy(app)
|
||||
|
||||
class User(db.Model):
|
||||
__tablename__ = 'user'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(100), unique=True, nullable=False)
|
||||
password = db.Column(db.String(100), nullable=False)
|
||||
email = db.Column(db.String(100), unique=True, nullable=False)
|
||||
|
||||
class Post(db.Model):
|
||||
__tablename__ = 'post'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
date_created = db.Column(db.String(100), unique=False, nullable=False)
|
||||
alias = db.Column(db.String(100), unique=True, nullable=False)
|
||||
title = db.Column(db.String(100), nullable=False)
|
||||
image = db.Column(db.String(100), nullable=False)
|
||||
|
||||
class MyAdminIndexView(AdminIndexView):
|
||||
@expose('/')
|
||||
def index(self):
|
||||
def admin_login_required(view_func):
|
||||
@wraps(view_func)
|
||||
def decorated_function(*args, **kwargs):
|
||||
if not session.get('admin_logged_in'):
|
||||
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')
|
||||
|
||||
class UserAdminView(ModelView):
|
||||
|
@ -34,21 +43,32 @@ class UserAdminView(ModelView):
|
|||
form_excluded_columns = ['password']
|
||||
|
||||
class PostAdminView(ModelView):
|
||||
pass
|
||||
|
||||
class TableAdminView(ModelView):
|
||||
can_delete = True
|
||||
can_create = 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.add_view(UserAdminView(User, 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_PASSWORD = '1234'
|
||||
ADMIN_PASSWORD = '1234321'
|
||||
|
||||
@app.route("/admin/login", methods=["GET", "POST"])
|
||||
def admin_login():
|
||||
|
@ -65,7 +85,7 @@ def admin_login():
|
|||
@app.route("/admin/logout")
|
||||
def admin_logout():
|
||||
session.pop("admin_logged_in", None)
|
||||
return redirect(url_for("index"))
|
||||
return redirect(url_for("admin_login"))
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
|
|
Loading…
Reference in New Issue