Skip to content

Commit

Permalink
added admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Snickdx committed Apr 19, 2024
1 parent a250a11 commit bfc03b4
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ EXPOSE 8085


# Run gunicorn when the container launches
CMD ["gunicorn", "-b", "0.0.0.0:8085", "wsgi:app"]
CMD ["gunicorn", "-c", "gunicorn_config.py", "wsgi:app"]
1 change: 1 addition & 0 deletions App/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ def load_config(app, overrides):
app.config["JWT_TOKEN_LOCATION"] = ["cookies", "headers"]
app.config["JWT_COOKIE_SECURE"] = True
app.config["JWT_COOKIE_CSRF_PROTECT"] = False
app.config['FLASK_ADMIN_SWATCH'] = 'darkly'
for key in overrides:
app.config[key] = overrides[key]
3 changes: 1 addition & 2 deletions App/default_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
SQLALCHEMY_DATABASE_URI="sqlite:///temp-database.db"
SECRET_KEY="secret key"
DEBUG=True
SECRET_KEY="secret key"
10 changes: 5 additions & 5 deletions App/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage


from App.database import init_db
from App.config import load_config


from App.controllers import (
setup_jwt,
add_auth_context
)

from App.views import views
from App.views import views, setup_admin

def add_views(app):
for view in views:
Expand All @@ -29,12 +31,10 @@ def create_app(overrides={}):
add_views(app)
init_db(app)
jwt = setup_jwt(app)

setup_admin(app)
@jwt.invalid_token_loader
@jwt.unauthorized_loader
def custom_unauthorized_response(error):
return render_template('401.html', error=error), 401

app.app_context().push()
return app

return app
3 changes: 3 additions & 0 deletions App/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
html {
padding: 0;
}
5 changes: 5 additions & 0 deletions App/templates/admin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends 'admin/master.html' %}

{% block body %}
<p>Hello world</p>
{% endblock %}
1 change: 1 addition & 0 deletions App/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .user import user_views
from .index import index_views
from .auth import auth_views
from .admin import setup_admin


views = [user_views, index_views, auth_views]
Expand Down
19 changes: 19 additions & 0 deletions App/views/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from flask_admin.contrib.sqla import ModelView
from flask_jwt_extended import jwt_required, current_user, unset_jwt_cookies, set_access_cookies
from flask_admin import Admin
from App.models import db, User

class AdminView(ModelView):

@jwt_required()
def is_accessible(self):
return current_user is not None

def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
flash("Login to access admin")
return redirect(url_for('index_page', next=request.url))

def setup_admin(app):
admin = Admin(app, name='FlaskMVC', template_mode='bootstrap3')
admin.add_view(AdminView(User, db.session))
3 changes: 3 additions & 0 deletions App/views/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Blueprint, render_template, jsonify, request, flash, send_from_directory, flash, redirect, url_for
from flask_jwt_extended import jwt_required, current_user, unset_jwt_cookies, set_access_cookies


from.index import index_views

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




'''
Page/Action Routes
'''
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Flask-Migrate==3.1.0
Werkzeug==2.2.3
gevent==22.10.2
mysqlclient==2.1.1
Flask-Admin==1.6.1
2 changes: 2 additions & 0 deletions wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from flask.cli import with_appcontext, AppGroup

from App.database import db, get_migrate
from App.models import User
from App.main import create_app
from App.controllers import ( create_user, get_all_users_json, get_all_users, initialize )


# This commands file allow you to create convenient CLI commands for testing controllers

app = create_app()
Expand Down

0 comments on commit bfc03b4

Please sign in to comment.