Commit e520934a authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 4b6bdfaa
...@@ -21,11 +21,20 @@ from application.common import ConfigHelper ...@@ -21,11 +21,20 @@ from application.common import ConfigHelper
class FlaskElasticsearch: class FlaskElasticsearch:
def __init__(self, app=None): def __init__(self, app=None):
"""
Initialize a FlaskElasticsearch instance.
:param app: Flask application instance for Elasticsearch initialization.
"""
if app is not None: if app is not None:
self.init_app(app) self.init_app(app)
def init_app(self, app: Flask) -> None: def init_app(self, app: Flask) -> None:
"""Initialize the app""" """
Initialize the app with Elasticsearch connection.
:param app: Flask application instance for Elasticsearch initialization.
"""
if hasattr(app, 'teardown_appcontext'): if hasattr(app, 'teardown_appcontext'):
app.teardown_appcontext(self.teardown) app.teardown_appcontext(self.teardown)
else: else:
...@@ -33,7 +42,11 @@ class FlaskElasticsearch: ...@@ -33,7 +42,11 @@ class FlaskElasticsearch:
@staticmethod @staticmethod
def teardown(exception): def teardown(exception):
"""Clears the Elasticsearch connection after each request.""" """
Clear the Elasticsearch connection after each request.
:param exception: Exception instance, if any.
"""
ctx = current_app._get_current_object() ctx = current_app._get_current_object()
if hasattr(ctx, 'elasticsearch'): if hasattr(ctx, 'elasticsearch'):
ctx.elasticsearch = None ctx.elasticsearch = None
...@@ -41,7 +54,12 @@ class FlaskElasticsearch: ...@@ -41,7 +54,12 @@ class FlaskElasticsearch:
raise RuntimeError(exception) raise RuntimeError(exception)
def __getattr__(self, item: Any) -> Any: def __getattr__(self, item: Any) -> Any:
"""Lazy initialization of Elasticsearch connection on first use.""" """
Lazy initialization of Elasticsearch connection on first use.
:param item: Attribute to get from the Elasticsearch connection.
:return: Value of the requested attribute.
"""
ctx = current_app._get_current_object() ctx = current_app._get_current_object()
if ctx is not None: if ctx is not None:
if not hasattr(ctx, 'elasticsearch'): if not hasattr(ctx, 'elasticsearch'):
...@@ -61,9 +79,6 @@ class FlaskElasticsearch: ...@@ -61,9 +79,6 @@ class FlaskElasticsearch:
if current_app: if current_app:
config_helper = ConfigHelper(current_app) config_helper = ConfigHelper(current_app)
cfg = config_helper.Elasticsearch cfg = config_helper.Elasticsearch
if cfg is None:
raise KeyError('Key Elasticsearch is not defined')
host = cfg.Host or 'localhost' host = cfg.Host or 'localhost'
port = int(cfg.Port) or 9200 port = int(cfg.Port) or 9200
user = cfg.User or None user = cfg.User or None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment