Skip to content

Commit bd09d23

Browse files
authored
Merge pull request #5 from samiuelson/KG/flaskAdmin
added basic auth to admin panel
2 parents 4c3eb98 + df53203 commit bd09d23

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

dbms/rdb.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1+
from flask import redirect, Response
12
from flask_sqlalchemy import SQLAlchemy
2-
from flask_admin.contrib.sqla import ModelView
3+
from flask_admin.contrib import sqla
4+
from flask_basicauth import BasicAuth
5+
6+
from werkzeug.exceptions import HTTPException
7+
8+
class AuthException(HTTPException):
9+
def __init__(self, message):
10+
super(AuthException, self).__init__(message, Response(
11+
"You could not be authenticated. Please refresh the page.", 401,
12+
{'WWW-Authenticate': 'Basic realm="Login Required"'}
13+
))
14+
15+
16+
class ModelView(sqla.ModelView):
17+
@classmethod
18+
def setup_basic_auth(cls, basic_auth):
19+
cls.basic_auth = basic_auth
20+
def is_accessible(self):
21+
if not self.basic_auth.authenticate():
22+
raise AuthException('Not authenticated.')
23+
else:
24+
return True
25+
26+
def inaccessible_callback(self, name, **kwargs):
27+
return redirect(self.basic_auth.challenge())
328

429
db = SQLAlchemy()
530

6-
def register_admin(admin):
7-
from .models import Intent
31+
def register_admin(admin, app):
32+
from .models import Intent, BibleVerse
33+
basic_auth = BasicAuth(app)
34+
ModelView.setup_basic_auth(basic_auth)
835
admin.add_view(ModelView(Intent, db.session))
36+
admin.add_view(ModelView(BibleVerse, db.session))
37+

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ SQLAlchemy
3838
Flask-SQLAlchemy
3939
psycopg2==2.6.2
4040
Flask-Admin==1.4.2
41+
Flask-BasicAuth==0.2.0

web.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
from flask import Flask, request
44
from flask.views import MethodView
55
from flask_admin import Admin
6-
from flask_admin.contrib.sqla import ModelView
76
from flask_babel import Babel
87
from facebook.api import FacebookApi
98
from prayer import PrayerWebhook as webhook
109
from dbms.rdb import db, register_admin
1110

12-
1311
def create_app():
1412
app = Flask(__name__)
1513
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///intent.db'
1614
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
15+
app.config['BASIC_AUTH_USERNAME'] = 'john'
16+
app.config['BASIC_AUTH_PASSWORD'] = 'matrix'
1717
babel = Babel(app)
1818
admin = Admin(app, name='PrayerBot', template_mode='bootstrap3')
1919
db.init_app(app)
20-
register_admin(admin)
20+
register_admin(admin, app)
2121
return app
2222

2323
###

0 commit comments

Comments
 (0)