Skip to content

Commit bfc03b4

Browse files
committed
added admin
1 parent a250a11 commit bfc03b4

File tree

11 files changed

+42
-8
lines changed

11 files changed

+42
-8
lines changed

.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ EXPOSE 8085
1818

1919

2020
# Run gunicorn when the container launches
21-
CMD ["gunicorn", "-b", "0.0.0.0:8085", "wsgi:app"]
21+
CMD ["gunicorn", "-c", "gunicorn_config.py", "wsgi:app"]

App/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ def load_config(app, overrides):
1414
app.config["JWT_TOKEN_LOCATION"] = ["cookies", "headers"]
1515
app.config["JWT_COOKIE_SECURE"] = True
1616
app.config["JWT_COOKIE_CSRF_PROTECT"] = False
17+
app.config['FLASK_ADMIN_SWATCH'] = 'darkly'
1718
for key in overrides:
1819
app.config[key] = overrides[key]

App/default_config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
SQLALCHEMY_DATABASE_URI="sqlite:///temp-database.db"
2-
SECRET_KEY="secret key"
3-
DEBUG=True
2+
SECRET_KEY="secret key"

App/main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
from werkzeug.utils import secure_filename
66
from werkzeug.datastructures import FileStorage
77

8+
89
from App.database import init_db
910
from App.config import load_config
1011

12+
1113
from App.controllers import (
1214
setup_jwt,
1315
add_auth_context
1416
)
1517

16-
from App.views import views
18+
from App.views import views, setup_admin
1719

1820
def add_views(app):
1921
for view in views:
@@ -29,12 +31,10 @@ def create_app(overrides={}):
2931
add_views(app)
3032
init_db(app)
3133
jwt = setup_jwt(app)
32-
34+
setup_admin(app)
3335
@jwt.invalid_token_loader
3436
@jwt.unauthorized_loader
3537
def custom_unauthorized_response(error):
3638
return render_template('401.html', error=error), 401
37-
3839
app.app_context().push()
39-
return app
40-
40+
return app

App/static/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
html {
2+
padding: 0;
3+
}

App/templates/admin/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends 'admin/master.html' %}
2+
3+
{% block body %}
4+
<p>Hello world</p>
5+
{% endblock %}

App/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .user import user_views
44
from .index import index_views
55
from .auth import auth_views
6+
from .admin import setup_admin
67

78

89
views = [user_views, index_views, auth_views]

App/views/admin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from flask_admin.contrib.sqla import ModelView
2+
from flask_jwt_extended import jwt_required, current_user, unset_jwt_cookies, set_access_cookies
3+
from flask_admin import Admin
4+
from App.models import db, User
5+
6+
class AdminView(ModelView):
7+
8+
@jwt_required()
9+
def is_accessible(self):
10+
return current_user is not None
11+
12+
def inaccessible_callback(self, name, **kwargs):
13+
# redirect to login page if user doesn't have access
14+
flash("Login to access admin")
15+
return redirect(url_for('index_page', next=request.url))
16+
17+
def setup_admin(app):
18+
admin = Admin(app, name='FlaskMVC', template_mode='bootstrap3')
19+
admin.add_view(AdminView(User, db.session))

App/views/auth.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from flask import Blueprint, render_template, jsonify, request, flash, send_from_directory, flash, redirect, url_for
22
from flask_jwt_extended import jwt_required, current_user, unset_jwt_cookies, set_access_cookies
33

4+
45
from.index import index_views
56

67
from App.controllers import (
@@ -10,6 +11,8 @@
1011
auth_views = Blueprint('auth_views', __name__, template_folder='../templates')
1112

1213

14+
15+
1316
'''
1417
Page/Action Routes
1518
'''

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Flask-Migrate==3.1.0
1616
Werkzeug==2.2.3
1717
gevent==22.10.2
1818
mysqlclient==2.1.1
19+
Flask-Admin==1.6.1

wsgi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from flask.cli import with_appcontext, AppGroup
44

55
from App.database import db, get_migrate
6+
from App.models import User
67
from App.main import create_app
78
from App.controllers import ( create_user, get_all_users_json, get_all_users, initialize )
89

10+
911
# This commands file allow you to create convenient CLI commands for testing controllers
1012

1113
app = create_app()

0 commit comments

Comments
 (0)