Skip to content

Commit

Permalink
WORKING. Clean ups and more test to do
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Loembe committed Mar 28, 2024
1 parent 9c33dc7 commit f5bcfb3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 21 deletions.
3 changes: 2 additions & 1 deletion python/nistoar/midas/dbio/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ def select_records(self, perm: Permissions = ACLs.OWN, **constraints) -> Iterato
raise NotImplementedError()

@abstractmethod
def select_constraint_records(self, perm: Permissions = ACLs.OWN, **cst) -> Iterator[ProjectRecord]:
def select_constraint_records(self,filter:dict, perm: Permissions = ACLs.OWN) -> Iterator[ProjectRecord]:
"""
return an iterator of project records for which the given user has at least one of the
permissions and the records meet all the constraints given
Expand All @@ -1069,6 +1069,7 @@ def select_constraint_records(self, perm: Permissions = ACLs.OWN, **cst) -> Iter
:param str **cst: a json that describes all the constraints the records should meet.
the schema of this json is the query structure used by mongodb.
"""
print("BASE PYY")
raise NotImplementedError()

def is_connected(self) -> bool:
Expand Down
15 changes: 12 additions & 3 deletions python/nistoar/midas/dbio/inmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,27 @@ def select_records(self, perm: base.Permissions=base.ACLs.OWN) -> Iterator[base.
yield deepcopy(rec)
break

def select_constraint_records(self,perm: base.Permissions=base.ACLs.OWN, **cst) -> Iterator[base.ProjectRecord]:
if(base.DBClient.check_query_structure(cst) == True):
def select_constraint_records(self,filter:dict,perm: base.Permissions=base.ACLs.OWN,) -> Iterator[base.ProjectRecord]:
print("MORRAY INMEM")
print(isinstance(filter,dict))
print(filter)
print(base.DBClient.check_query_structure(filter))
if(base.DBClient.check_query_structure(filter) == True):
try:
if isinstance(perm, str):
perm = [perm]
print(perm)
if isinstance(perm, (list, tuple)):
perm = set(perm)
for rec in self._db[self._projcoll].values():
rec = base.ProjectRecord(self._projcoll, rec, self)
for p in perm:
if(rec.authorized(p)):
if (rec.searched(cst) == True):
print("can search")
print(filter)
if (rec.searched(filter) == True):
print('Yessir')
print(rec)
yield deepcopy(rec)
break
except Exception as ex:
Expand Down
8 changes: 4 additions & 4 deletions python/nistoar/midas/dbio/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ def select_records(self, perm: base.Permissions=base.ACLs.OWN) -> Iterator[base.
raise base.DBIOException("Failed while selecting records: " + str(ex), cause=ex)


def select_constraint_records(self, perm: base.Permissions=base.ACLs.OWN, **cst) -> Iterator[base.ProjectRecord]:
if(base.DBClient.check_query_structure(cst) == True):
def select_constraint_records(self,filter:dict, perm: base.Permissions=base.ACLs.OWN) -> Iterator[base.ProjectRecord]:
if(base.DBClient.check_query_structure(filter) == True):
if isinstance(perm, str):
perm = [perm]
if isinstance(perm, (list, tuple)):
Expand All @@ -220,12 +220,12 @@ def select_constraint_records(self, perm: base.Permissions=base.ACLs.OWN, **cst)
else:
constraints = {"acls."+perm.pop(): {"$in": idents}}

cst["$and"].append(constraints)
filter["$and"].append(constraints)

try:

coll = self.native[self._projcoll]
for rec in coll.find(cst):
for rec in coll.find(filter):
yield base.ProjectRecord(self._projcoll, rec)

except Exception as ex:
Expand Down
31 changes: 26 additions & 5 deletions python/nistoar/midas/dbio/wsgi/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from collections.abc import Mapping, Sequence, Callable
from typing import Iterator
from urllib.parse import parse_qs
import json

from nistoar.pdr.publish.service.wsgi import SubApp, Handler # same infrastructure as publishing service
from nistoar.pdr.publish.prov import PubAgent
Expand Down Expand Up @@ -466,13 +467,18 @@ def do_POST(self, path):
"""
try:
input = self.get_json_body()
print("PROJECT.PY DO_POST")
print(input)
except self.FatalError as ex:
return self.send_fatal_error(ex)

print("PATH BEFORE RSTRIP"+path)
path = path.rstrip('/')
print("PATH AFTER RSTRIP"+path)
if not path:
return self.create_record(input)
elif path == ':selected':
print("RIGHT CONDITION")
return self.adv_select_records(input)
else:
return send_error_resp(400, "Selection resource not found")
Expand All @@ -482,18 +488,29 @@ def adv_select_records(self, input: Mapping):
submit a record search
:param dict filter: the search constraints for the search.
"""
print(input)
print("wesh")
filter = input.get("filter", {})
print(filter)
if not filter:
print("Noooo filter")
perms = input.get("permissions", [])
print(perms)
if not perms:
print("Noooo perms")
perms = [ dbio.ACLs.OWN ]

# sort the results by the best permission type permitted
sortd = SortByPerm()
#sortd = SortByPerm()
result=[]
for rec in self._adv_select_records(filter, perms):
sortd.add_record(rec)
out = [rec.to_dict() for rec in sortd.sorted()]
#sortd.add_record(rec)
print(rec)
result.append(rec)

return self.send_json(out, ashead=ashead)
#out = [rec.to_dict() for rec in sortd.sorted()]

return self.send_json(result, ashead=ashead)

def _adv_select_records(self, filter, perms) -> Iterator[ProjectRecord]:
"""
Expand All @@ -502,7 +519,11 @@ def _adv_select_records(self, filter, perms) -> Iterator[ProjectRecord]:
This base implementation passes the query directly to the generic DBClient instance.
:return: a generator that iterates through the matched records
"""
return self._dbcli.select_contraint_records(filter, perms)
print('__ADV_SELECT')
print(json.dumps(filter))
print("Before json.loads()")
print(perms)
return self._dbcli.select_constraint_records(filter, perms)

def create_record(self, newdata: Mapping):
"""
Expand Down
5 changes: 1 addition & 4 deletions python/tests/nistoar/midas/dbio/data/asc_and.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"$and": [
{
"status.state": "edit"
},
{
"name": "test3"
"data.title": "test"
}
]
}
8 changes: 4 additions & 4 deletions python/tests/nistoar/midas/dbio/test_inmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,17 @@ def test_select_constraint_records(self):
constraint_wrong = {'$a,nkd': [
{'$okn,r': [{'name': 'test 2'}, {'name': 'test3'}]}]}
with self.assertRaises(SyntaxError) as context:
recs = list(self.cli.select_constraint_records(base.ACLs.READ,**constraint_wrong))
recs = list(self.cli.select_constraint_records(constraint_wrong,base.ACLs.READ))
self.assertEqual(str(context.exception), "Wrong query format")
recs = list(self.cli.select_constraint_records(**constraint_or))
recs = list(self.cli.select_constraint_records(constraint_or))
self.assertEqual(len(recs), 2)
self.assertEqual(recs[0].id, "pdr0:0006")
self.assertEqual(recs[1].id, "pdr0:0003")

recs = list(self.cli.select_constraint_records(**constraint_and))
recs = list(self.cli.select_constraint_records(constraint_and))
self.assertEqual(len(recs), 1)
self.assertEqual(recs[0].id, "pdr0:0003")
recs = list(self.cli.select_constraint_records(base.ACLs.READ,**constraint_andor))
recs = list(self.cli.select_constraint_records(constraint_andor,base.ACLs.READ))
self.assertEqual(len(recs), 2)
self.assertEqual(recs[0].id, "pdr0:0006")
self.assertEqual(recs[1].id, "pdr0:0003")
Expand Down
76 changes: 76 additions & 0 deletions python/tests/nistoar/midas/dbio/wsgi/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,76 @@ def test_create_handler_name(self):
self.assertTrue(hdlr.acceptable())
hdlr._env['HTTP_ACCEPT'] = "text/html,*/*"
self.assertTrue(hdlr.acceptable())

def test_select_constraints(self):
print('==============================')
path = ""
req = {
'REQUEST_METHOD': 'POST',
'PATH_INFO': self.rootpath + path
}
req['wsgi.input'] = StringIO(json.dumps({"name":"Superconductor Metrology",
"data": {
"title": "Superconductor Metrology"}
}))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("201 ", self.resp[0])
resp=self.body2dict(body)
self.assertEqual(resp['data']['title'],'Superconductor Metrology')

req = {
'REQUEST_METHOD': 'POST',
'PATH_INFO': self.rootpath + path
}
req['wsgi.input'] = StringIO(json.dumps({"name":"Standard Reference Materials",
"data": {
"title": "Standard Reference Materials"}
}))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("201 ", self.resp[0])
resp=self.body2dict(body)
self.assertEqual(resp['data']['title'],'Standard Reference Materials')

req = {
'REQUEST_METHOD': 'POST',
'PATH_INFO': self.rootpath + path
}
req['wsgi.input'] = StringIO(json.dumps({"name":"Supplementary material for:",
"data": {
"title": "Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy"}
}))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
self.assertIn("201 ", self.resp[0])
resp=self.body2dict(body)
self.assertEqual(resp['data']['title'],'Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy')


path = "mdm1:0005/"
req = {
'REQUEST_METHOD': 'GET',
'PATH_INFO': self.rootpath + path
}
print('====**************====')
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()
resp = self.body2dict(body)
self.assertEqual(resp['data']['title'],'Supplementary material for: The detection of carbon dioxide leaks using quasi-tomographic laser absorption spectroscopy')

path=":selected"
req = {
'REQUEST_METHOD': 'POST',
'PATH_INFO': self.rootpath + path
}
print(json.dumps( {"filter": {"data.title": "Standard Reference Materials"}}))
req['wsgi.input'] = StringIO(json.dumps( {"filter": {"$and": [ {"data.title": "Standard Reference Materials"} ]},
"permissions": ["read", "write"]} ))
hdlr = self.app.create_handler(req, self.start, path, nistr)
body = hdlr.handle()




def test_get_name(self):
Expand All @@ -117,8 +187,12 @@ def test_get_name(self):
'REQUEST_METHOD': 'GET',
'PATH_INFO': self.rootpath + path
}

hdlr = self.app.create_handler(req, self.start, path, nistr)
prec = self.create_record("goob")
prec = self.create_record("goob1")
prec = self.create_record("goob2")

body = hdlr.handle()
self.assertIn("200 ", self.resp[0])
resp = self.body2dict(body)
Expand Down Expand Up @@ -228,6 +302,7 @@ def test_get_full(self):
}
hdlr = self.app.create_handler(req, self.start, path, nistr)
prec = self.create_record()

body = hdlr.handle()
self.assertIn("200 ", self.resp[0])
resp = self.body2dict(body)
Expand Down Expand Up @@ -470,6 +545,7 @@ def test_getput_data(self):
self.assertTrue(isinstance(hdlr, prj.ProjectDataHandler))
body = hdlr.handle()
self.assertIn("200 ", self.resp[0])

self.assertEqual(self.body2dict(body), {"color": "red", "pos": {"vec": [1,2,3]}})

self.resp = []
Expand Down

0 comments on commit f5bcfb3

Please sign in to comment.