Skip to content

Commit

Permalink
savesearch handler and collection added
Browse files Browse the repository at this point in the history
  • Loading branch information
hkethi002 committed Aug 8, 2017
1 parent 3e273f6 commit e0be214
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .handlers.reporthandler import ReportHandler
from .handlers.resolvehandler import ResolveHandler
from .handlers.roothandler import RootHandler
from .handlers.savedsearchhandler import SavedSearchHandler
from .handlers.schemahandler import SchemaHandler
from .handlers.userhandler import UserHandler
from .jobs.handlers import BatchHandler, JobsHandler, JobHandler, GearsHandler, GearHandler, RulesHandler, RuleHandler
Expand Down Expand Up @@ -107,6 +108,12 @@ def prefix(path, routes):
route('/dataexplorer/search/fields/aggregate', DataExplorerHandler, h='aggregate_field_values', m=['POST']),
route('/dataexplorer/index/fields', DataExplorerHandler, h='index_field_names', m=['POST']),

# Search Saving
route('/savesearch', SavedSearchHandler, m=['POST']),
route('/savesearch', SavedSearchHandler, h='get_all', m=['GET']),
route('/savesearch/<sid:{cid}>', SavedSearchHandler, m=['GET','DELETE']),
route('/savesearch/<sid:{cid}>', SavedSearchHandler, h='replace_search', m=['POST']),

# Users

route( '/users', UserHandler, h='get_all', m=['GET']),
Expand Down
1 change: 1 addition & 0 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def apply_env_variables(config):
'project-update.json',
'rule-add.json',
'rule-update.json',
'search-input.json',
'session.json',
'session-update.json',
'subject.json',
Expand Down
13 changes: 13 additions & 0 deletions api/dao/containerstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,16 @@ def inflate_job_info(self, analysis):

analysis['job'] = job
return analysis

class SearchStorage(ContainerStorage):

def __init__(self):
super(SearchStorage, self).__init__('searches', use_object_id=True)

def replace_el(self, search):
self.delete_el(search['_id'])
return self.create_el(search)




67 changes: 67 additions & 0 deletions api/handlers/savedsearchhandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import bson
from ..web import base
from .. import config, validators
from ..auth import require_login
from ..dao.containerstorage import SearchStorage

from ..auth import groupauth
from ..dao import noop


log = config.log
storage = SearchStorage()

class SavedSearchHandler(base.RequestHandler):

def __init__(self, request=None, response=None):
super(SavedSearchHandler, self).__init__(request, response)

@require_login
def post(self):
payload = self.request.json_body
validators.validate_data(payload, 'search-input.json', 'input', 'POST')
payload['permissions'] = [{"_id": self.uid, "access": "admin"}]
result = storage.create_el(payload)
if result.acknowledged:
if result.inserted_id:
return {'_id': result.inserted_id}
return {"hi" : "bye"}

def get_all(self):
log.debug(self.uid)
return storage.get_all_el({}, {'_id': self.uid}, {'label': 1})

def get(self, sid):
result = storage.get_el(sid)
if result is None:
self.abort(404, 'Element {} not found'.format(sid))
return result

def delete(self, sid):
search = storage.get_container(sid)
permchecker = groupauth.default(self, search)
result = permchecker(storage.exec_op)('DELETE', sid)
if result.deleted_count == 1:
return {'deleted': result.deleted_count}
else:
self.abort(404, 'Group {} not removed'.format(sid))
return result

def replace_search(self, sid):
payload = self.request.json_body
if payload.get('_id'):
del(payload['_id'])
if payload.get('permissions'):
perms = payload['permissions']
del(payload['permissions'])
validators.validate_data(payload, 'search-input.json', 'input', 'POST')
payload['_id'] = bson.ObjectId(sid)
payload['permissions'] = perms
search = storage.get_container(sid)
permchecker = groupauth.default(self, search)
permchecker(noop)('DELETE', sid)
result = storage.replace_el(payload)
if result.acknowledged:
if result.inserted_id:
return {'_id': result.inserted_id}
return {"hi" : "bye"}
46 changes: 46 additions & 0 deletions raml/schemas/definitions/search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions":{
"filter" : {
"type" : "object"

},
"search" : {
"type": "object",
"properties": {
"filters": {
"type" : "array",
"items" : {"$ref": "#/definitions/filter"}
},
"search_string": {"type" : "string"},
"all_data": {"type": "boolean"},
"return_type": {"enum": ["session", "acquisition", "analysis", "file"]}
},
"additionalProperties": false,
"required": ["return_type"]
},
"search-input":{
"type": "object",
"properties": {
"label": {"$ref": "../definitions/container.json#/definitions/label"},
"search": {"$ref": "#/definitions/search"}
},
"additionalProperties": false
},
"search-output":{
"type": "object",
"properties": {
"_id": {"$ref":"../definitions/objectid.json#"},
"label": {"$ref": "../definitions/container.json#/definitions/label"},
"uid": {"$ref":"../definitions/container.json#/definitions/uid"},
"created": {"$ref":"../definitions/created-modified.json#/definitions/created"},
"modified": {"$ref":"../definitions/created-modified.json#/definitions/modified"},
"permissions": {
"type":"array",
"items":{"$ref":"../definitions/permission.json#/definitions/permission-output-default-required"}
},
"search": {"$ref":"#/definitions/search"}
}
}
}
}
6 changes: 6 additions & 0 deletions raml/schemas/input/search-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Search",
"type": "object",
"allOf":[{"$ref":"../definitions/search.json#/definitions/search-input"}]
}
49 changes: 49 additions & 0 deletions test/integration_tests/python/test_savedsearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

def test_search_saving(as_admin, data_builder):

# Try posting a malformed search
r = as_admin.post('/savesearch', json={"not-label":"random-string"})
assert r.status_code == 400

# Try getting a non-existent saved search
r = as_admin.get('/savesearch/000000000000000000000000')
assert r.status_code == 404

# Save a search
r = as_admin.post('/savesearch', json={'label': 'search1', 'search': {'return_type': 'session'}})
assert r.ok
search = r.json()['_id']

# Get all searched user has access to
r = as_admin.get('/savesearch')
assert r.ok

# Get the saved search by id
r = as_admin.get('/savesearch/' + search)
assert r.ok
assert r.json()['label'] == 'search1'

# Malformed search replace
payload = {'label': 'good-label', 'search' : { 'not-return-type' : 'not-container'}}
r = as_admin.post('/savesearch/' + search, json=payload)
assert r.status_code == 400

# Replace search
r = as_admin.get('/savesearch/' + search)
assert r.ok
assert r.json()['label'] == 'search1'
payload = r.json()
payload['label'] = 'newSearch'
r = as_admin.post('/savesearch/' + search, json=payload)
assert r.ok
assert r.json()['_id'] == search
r = as_admin.get('/savesearch/' + search)
assert r.ok
assert r.json()['label'] == 'newSearch'

# Delete saved search
r = as_admin.delete('/savesearch/' + search)
assert r.ok
r = as_admin.get('/savesearch')
assert r.ok
assert len(r.json()) == 0

0 comments on commit e0be214

Please sign in to comment.