-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflask_elastic.py
executable file
·27 lines (22 loc) · 1.04 KB
/
flask_elastic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from flask import current_app
from elasticsearch import Elasticsearch
class Elastic(object):
"""
A thin wrapper around elasticsearch.Elasticsearch()
"""
def __init__(self, app=None, **kwargs):
if app is not None:
self.init_app(app, **kwargs)
def init_app(self, app, **kwargs):
app.config.setdefault('ELASTICSEARCH_URL', 'http://localhost:9200/')
# using the app factory pattern _app_ctx_stack.top is None so what
# do we register on? app.extensions looks a little hackish (I don't
# know flask well enough to be sure), but that's how it's done in
# flask-pymongo so let's use it for now.
app.extensions['elastic'] = \
Elasticsearch(app.config['ELASTICSEARCH_URL'], **kwargs)
def __getattr__(self, item):
if not 'elastic' in current_app.extensions.keys():
raise Exception(
'not initialised, did you forget to call init_app?')
return getattr(current_app.extensions['elastic'], item)