Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
e520934a
Commit
e520934a
authored
Nov 04, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
4b6bdfaa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
6 deletions
+21
-6
application/libs/flask_elasticsearch/elasticsearch.py
application/libs/flask_elasticsearch/elasticsearch.py
+21
-6
No files found.
application/libs/flask_elasticsearch/elasticsearch.py
View file @
e520934a
...
...
@@ -21,11 +21,20 @@ from application.common import ConfigHelper
class
FlaskElasticsearch
:
def
__init__
(
self
,
app
=
None
):
"""
Initialize a FlaskElasticsearch instance.
:param app: Flask application instance for Elasticsearch initialization.
"""
if
app
is
not
None
:
self
.
init_app
(
app
)
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'
):
app
.
teardown_appcontext
(
self
.
teardown
)
else
:
...
...
@@ -33,7 +42,11 @@ class FlaskElasticsearch:
@
staticmethod
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
()
if
hasattr
(
ctx
,
'elasticsearch'
):
ctx
.
elasticsearch
=
None
...
...
@@ -41,7 +54,12 @@ class FlaskElasticsearch:
raise
RuntimeError
(
exception
)
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
()
if
ctx
is
not
None
:
if
not
hasattr
(
ctx
,
'elasticsearch'
):
...
...
@@ -61,9 +79,6 @@ class FlaskElasticsearch:
if
current_app
:
config_helper
=
ConfigHelper
(
current_app
)
cfg
=
config_helper
.
Elasticsearch
if
cfg
is
None
:
raise
KeyError
(
'Key Elasticsearch is not defined'
)
host
=
cfg
.
Host
or
'localhost'
port
=
int
(
cfg
.
Port
)
or
9200
user
=
cfg
.
User
or
None
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment