Use Flask-Hashids package

This commit is contained in:
Patrick Jentsch 2021-12-08 11:25:33 +01:00
parent e1004a0181
commit 63527da37f
5 changed files with 14 additions and 10 deletions

View File

@ -6,14 +6,13 @@ from flask_migrate import Migrate
from flask_paranoid import Paranoid
from flask_socketio import SocketIO
from flask_sqlalchemy import SQLAlchemy
from hashids import Hashids
from flask_hashids import Hashids
import flask_assets
assets: flask_assets.Environment = flask_assets.Environment()
db: SQLAlchemy = SQLAlchemy()
# TODO: Add 'SECRET_KEY' from as 'salt' kwarg
hashids: Hashids = Hashids(min_length=32)
hashids: Hashids = Hashids()
login: LoginManager = LoginManager()
login.login_view: str = 'auth.login'
login.login_message: str = 'Please log in to access this page.'
@ -24,14 +23,15 @@ paranoid.redirect_view: str = '/'
socketio: SocketIO = SocketIO()
def create_app(config_class: Config = Config) -> Flask:
def create_app(config: Config = Config) -> Flask:
''' Creates an initialized Flask (WSGI Application) object. '''
app: Flask = Flask(__name__)
app.config.from_object(config_class)
app.config.from_object(config)
assets.init_app(app)
config_class.init_app(app)
config.init_app(app)
db.init_app(app)
hashids.init_app(app)
login.init_app(app)
mail.init_app(app)
migrate.init_app(app, db)
@ -39,8 +39,8 @@ def create_app(config_class: Config = Config) -> Flask:
socketio.init_app(
app, message_queue=app.config['NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI'])
from .utils import HashidConverter
app.url_map.converters['hashid'] = HashidConverter
# from .utils import HashidConverter
# app.url_map.converters['hashid'] = HashidConverter
from .events import socketio as socketio_events
from .events import sqlalchemy as sqlalchemy_events

View File

@ -57,7 +57,7 @@ from .cqi import * # noqa
def connect(auth):
# the auth variable is used in a hacky way. It contains the corpus id for
# which a corpus analysis session should be started.
corpus_id = hashids.decode(auth['corpus_id'])[0]
corpus_id = hashids.decode(auth['corpus_id'])
corpus = Corpus.query.get(corpus_id)
if corpus is None:
# return {'code': 404, 'msg': 'Not Found'}

View File

@ -12,7 +12,7 @@ from ..models import User
@socketio.on('users.user.get')
@socketio_login_required
def users_user_get(user_hashid):
user_id = hashids.decode(user_hashid)[0]
user_id = hashids.decode(user_hashid)
user = User.query.get(user_id)
if user is None:
response = {'code': 404, 'msg': 'Not found'}

View File

@ -19,6 +19,9 @@ class Config:
TEMPLATES_AUTO_RELOAD = \
os.environ.get('TEMPLATES_AUTO_RELOAD', 'false').lower() == 'true'
''' # Flask-Hashids '''
HASHIDS_MIN_LENGTH = 32
''' # Flask-Login # '''
REMEMBER_COOKIE_HTTPONLY = True
REMEMBER_COOKIE_SECURE = \

View File

@ -3,6 +3,7 @@ docker
eventlet==0.30.2
Flask~=1.1
Flask-Assets
Flask-Hashids
Flask-HTTPAuth
Flask-Login
Flask-Mail