-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
savesearch handler and collection added
- Loading branch information
Showing
7 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |