Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #101 from postatum/acls_in_db
Browse files Browse the repository at this point in the history
Refactoring related to "ACLs in database"
  • Loading branch information
jstoiko committed Sep 11, 2015
2 parents 5525143 + 33573cc commit 22927ce
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 35 deletions.
49 changes: 27 additions & 22 deletions nefertari/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,46 +172,46 @@ def src2type(cls, source):

@classmethod
def setup(cls, settings):
ES.settings = settings.mget('elasticsearch')
ES.settings.setdefault('chunk_size', 500)
cls.settings = settings.mget('elasticsearch')
cls.settings.setdefault('chunk_size', 500)

try:
_hosts = ES.settings.hosts
_hosts = cls.settings.hosts
hosts = []
for (host, port) in [
split_strip(each, ':') for each in split_strip(_hosts)]:
hosts.append(dict(host=host, port=port))

params = {}
if ES.settings.asbool('sniff'):
if cls.settings.asbool('sniff'):
params = dict(
sniff_on_start=True,
sniff_on_connection_fail=True
)

ES.api = elasticsearch.Elasticsearch(
cls.api = elasticsearch.Elasticsearch(
hosts=hosts, serializer=engine.ESJSONSerializer(),
connection_class=ESHttpConnection, **params)
log.info('Including ElasticSearch. %s' % ES.settings)
log.info('Including ElasticSearch. %s' % cls.settings)

except KeyError as e:
raise Exception(
'Bad or missing settings for elasticsearch. %s' % e)

def __init__(self, source='', index_name=None, chunk_size=None):
self.doc_type = self.src2type(source)
self.index_name = index_name or ES.settings.index_name
self.index_name = index_name or self.settings.index_name
if chunk_size is None:
chunk_size = ES.settings.asint('chunk_size')
chunk_size = self.settings.asint('chunk_size')
self.chunk_size = chunk_size

@classmethod
def create_index(cls, index_name=None):
index_name = index_name or ES.settings.index_name
index_name = index_name or cls.settings.index_name
try:
ES.api.indices.exists([index_name])
cls.api.indices.exists([index_name])
except (IndexNotFoundException, JHTTPNotFound):
ES.api.indices.create(index_name)
cls.api.indices.create(index_name)

@classmethod
def setup_mappings(cls, force=False):
Expand All @@ -224,7 +224,7 @@ def setup_mappings(cls, force=False):
Use `force=True` to make subsequent calls perform mapping
creation calls to ES.
"""
if getattr(ES, '_mappings_setup', False) and not force:
if getattr(cls, '_mappings_setup', False) and not force:
log.debug('ES mappings have been already set up for currently '
'running application. Call `setup_mappings` with '
'`force=True` to perform mappings set up again.')
Expand All @@ -234,20 +234,20 @@ def setup_mappings(cls, force=False):
try:
for model_name, model_cls in models.items():
if getattr(model_cls, '_index_enabled', False):
es = ES(model_cls.__name__)
es = cls(model_cls.__name__)
es.put_mapping(body=model_cls.get_es_mapping())
except JHTTPBadRequest as ex:
raise Exception(ex.json['extra']['data'])
ES._mappings_setup = True
cls._mappings_setup = True

def delete_mapping(self):
ES.api.indices.delete_mapping(
self.api.indices.delete_mapping(
index=self.index_name,
doc_type=self.doc_type,
)

def put_mapping(self, body, **kwargs):
ES.api.indices.put_mapping(
self.api.indices.put_mapping(
doc_type=self.doc_type,
body=body,
index=self.index_name,
Expand Down Expand Up @@ -345,7 +345,7 @@ def index_missing_documents(self, documents, request=None):
body={'ids': [d['_pk'] for d in documents]},
)
try:
response = ES.api.mget(**query_kwargs)
response = self.api.mget(**query_kwargs)
except IndexNotFoundException:
indexed_ids = set()
else:
Expand Down Expand Up @@ -403,7 +403,7 @@ def get_by_ids(self, ids, **params):
)

try:
data = ES.api.mget(**params)
data = self.api.mget(**params)
except IndexNotFoundException:
if __raise_on_empty:
raise JHTTPNotFound(
Expand Down Expand Up @@ -454,6 +454,8 @@ def build_search_params(self, params):
}
else:
_params['body'] = {"query": {"match_all": {}}}
else:
_params['body'] = params['body']

if '_limit' not in params:
params['_limit'] = self.api.count()['count']
Expand All @@ -474,6 +476,9 @@ def build_search_params(self, params):
search_fields.reverse()
search_fields = [s + '^' + str(i) for i, s in
enumerate(search_fields, 1)]
current_qs = _params['body']['query']['query_string']
if isinstance(current_qs, str):
_params['body']['query']['query_string'] = {'query': current_qs}
_params['body']['query']['query_string']['fields'] = search_fields

return _params
Expand All @@ -484,7 +489,7 @@ def do_count(self, params):
params.pop('from_', None)
params.pop('sort', None)
try:
return ES.api.count(**params)['count']
return self.api.count(**params)['count']
except IndexNotFoundException:
return 0

Expand Down Expand Up @@ -520,7 +525,7 @@ def aggregate(self, **params):

log.debug('Performing aggregation: {}'.format(_aggregations_params))
try:
response = ES.api.search(**search_params)
response = self.api.search(**search_params)
except IndexNotFoundException:
if __raise_on_empty:
raise JHTTPNotFound(
Expand Down Expand Up @@ -554,7 +559,7 @@ def get_collection(self, **params):
fields=fields)

try:
data = ES.api.search(**_params)
data = self.api.search(**_params)
except IndexNotFoundException:
if __raise_on_empty:
raise JHTTPNotFound(
Expand Down Expand Up @@ -596,7 +601,7 @@ def get_resource(self, **kw):
self.doc_type, params)

try:
data = ES.api.get_source(**params)
data = self.api.get_source(**params)
except IndexNotFoundException:
if __raise_on_empty:
raise JHTTPNotFound("{} (Index does not exist)".format(
Expand Down
36 changes: 23 additions & 13 deletions nefertari/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@
log = logging.getLogger(__name__)


ACTIONS = ['index', 'show', 'create', 'update',
'delete', 'update_many', 'delete_many',
'replace']
# All actions names(view method names) supported by nefertari
ACTIONS = [
'index', # Collection GET
'create', # Collection POST
'update_many', # Collection PATCH/PUT
'delete_many', # Collection DELETE
'collection_options', # Collection OPTIONS
'show', # Item GET
'update', # Item PATCH
'replace', # Item PUT
'delete', # Item DELETE
'item_options', # Item OPTIONS
]
PERMISSIONS = {
'index': 'view',
'show': 'view',
'create': 'create',
'update': 'update',
'update_many': 'update',
'delete': 'delete',
'delete_many': 'delete',
'replace': 'update',
'collection_options': 'options',
'item_options': 'options',
'index': 'view',
'show': 'view',
'create': 'create',
'update': 'update',
'update_many': 'update',
'replace': 'update',
'delete': 'delete',
'delete_many': 'delete',
'collection_options': 'options',
'item_options': 'options',
}
DEFAULT_ID_NAME = 'id'

Expand Down
26 changes: 26 additions & 0 deletions tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,32 @@ def test_build_search_params_search_fields(self):
assert params['index'] == 'foondex'
assert params['doc_type'] == 'Foo'

def test_build_search_params_with_body(self):
obj = es.ES('Foo', 'foondex')
params = obj.build_search_params({
'body': {'query': {'query_string': 'foo'}},
'_raw_terms': ' AND q:5',
'_limit': 10,
'_search_fields': 'a,b',
'_fields': ['a'],
'_sort': '+a,-b,c',
})
assert sorted(params.keys()) == sorted([
'body', 'doc_type', 'fields', 'from_', 'index', 'size',
'sort'])
assert params['body'] == {
'query': {
'query_string': {
'fields': ['b^1', 'a^2'],
'query': 'foo'
}
}
}
assert params['index'] == 'foondex'
assert params['doc_type'] == 'Foo'
assert params['fields'] == ['a']
assert params['sort'] == 'a:asc,b:desc,c:asc'

@patch('nefertari.elasticsearch.ES.api.count')
def test_do_count(self, mock_count):
obj = es.ES('Foo', 'foondex')
Expand Down

0 comments on commit 22927ce

Please sign in to comment.