From 63527da37f86b9427ee8c9841f6ddb5720f0495a Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Wed, 8 Dec 2021 11:25:33 +0100
Subject: [PATCH] Use Flask-Hashids package
---
app/__init__.py | 16 ++++++++--------
app/corpora/cqi_over_socketio/__init__.py | 2 +-
app/events/socketio.py | 2 +-
config.py | 3 +++
requirements.txt | 1 +
5 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/app/__init__.py b/app/__init__.py
index 457c0934..37b0961f 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -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
diff --git a/app/corpora/cqi_over_socketio/__init__.py b/app/corpora/cqi_over_socketio/__init__.py
index 68f28d8f..ff698400 100644
--- a/app/corpora/cqi_over_socketio/__init__.py
+++ b/app/corpora/cqi_over_socketio/__init__.py
@@ -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'}
diff --git a/app/events/socketio.py b/app/events/socketio.py
index ceb43a4f..94b4c0c7 100644
--- a/app/events/socketio.py
+++ b/app/events/socketio.py
@@ -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'}
diff --git a/config.py b/config.py
index 79f7f9b6..e28325a1 100644
--- a/config.py
+++ b/config.py
@@ -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 = \
diff --git a/requirements.txt b/requirements.txt
index fbff2916..97839593 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -3,6 +3,7 @@ docker
eventlet==0.30.2
Flask~=1.1
Flask-Assets
+Flask-Hashids
Flask-HTTPAuth
Flask-Login
Flask-Mail