diff --git a/fda-units-service/app.py b/fda-units-service/app.py index 36f94de0c545325ca02a20c3f751fda12e46943e..ea06cd0997c94a557968ef3a540e1920f48ee5ab 100644 --- a/fda-units-service/app.py +++ b/fda-units-service/app.py @@ -40,13 +40,12 @@ def suggest(): try: unit = str(input_json['ustring']) offset = int(input_json['offset']) - res = list_units(unit,offset) - return jsonify(res),200 + res = list_units(stringmapper(unit),offset) + return jsonify(res), 200 except Exception as e: print(e) - #str(print_exc()) - res = {"success": False, "message": "Unknown error"+str(e)+unit} - return jsonify(res) + res = {"success": False, "message": str(e)} + return jsonify(res), 500 @app.route('/api/units/validate', methods=["POST"], endpoint='validate') @swag_from('validate.yml') @@ -55,10 +54,11 @@ def valitate(): try: unit = str(input_json['ustring']) res = validator(stringmapper(unit)) + return str(res), 200 except Exception as e: print(e) - res = {"success": False, "message": "Unknown error"+str(e)+unit} - return jsonify(res) + res = {"success": False, "message": str(e)} + return jsonify(res) @app.route('/api/units/geturi', methods=["POST"], endpoint='geturi') @swag_from('geturi.yml') @@ -67,10 +67,11 @@ def geturi(): try: name = str(input_json['uname']) res = get_uri(name) + return jsonify(res), 200 except Exception as e: print(e) - res = {"success": False, "message": "Unknown error"+str(e)+unit} - return jsonify(res) + res = {"success": False, "message": str(e)} + return jsonify(res), 500 rest_server_port = 5010 eureka_client.init(eureka_server=os.getenv('EUREKA_SERVER', 'http://localhost:9090/eureka/'), diff --git a/fda-units-service/list.py b/fda-units-service/list.py index cbfaaf6c462397afabd186f9fd4a136d9efe5973..ee96da0e69dc46447d62e3016ced8c1ad5e6c303 100644 --- a/fda-units-service/list.py +++ b/fda-units-service/list.py @@ -18,14 +18,14 @@ rdf_schema = rdflib.Namespace('http://www.w3.org/2000/01/rdf-schema#') r={} def list_units(string,offset=0): - if bool(re.match('^[a-zA-Z0-9]+$',string)): + if bool(re.match('^[a-zA-Z0-9\\s]+$',string)): l_query = """ SELECT ?symbol ?name ?comment WHERE { ?unit om:symbol ?symbol . ?unit <http://www.w3.org/2000/01/rdf-schema#label> ?name . ?unit <http://www.w3.org/2000/01/rdf-schema#comment> ?comment . - FILTER regex(str(?unit),\""""+string+"""\","i") + FILTER (regex(str(?unit),\""""+string+"""\","i") && lang(?name)="en") } LIMIT 10 OFFSET """+str(offset) qres = g.query(l_query) units = list() diff --git a/fda-units-service/validate.py b/fda-units-service/validate.py index 7ca56a055be356a1a091c41d104bc40effbeb4ec..215155387f152359f202df70e9fa5583f8057424 100644 --- a/fda-units-service/validate.py +++ b/fda-units-service/validate.py @@ -16,10 +16,10 @@ def validator(value): #input str tmp = str(om)+value t_uri = rdflib.term.URIRef(tmp) - if next(g.triples((t_uri,None,om.Unit)), _exhausted) is _exhausted and next(g.triples((t_uri,None,om.PrefixedUnit)),_exhausted) is _exhausted and next(g.triples((t_uri,None,om.UnitDivision)),_exhausted) is _exhausted: - return {"valid": False} + if next(g.triples((t_uri,None,om.Unit)), _exhausted) is _exhausted and next(g.triples((t_uri,None,om.PrefixedUnit)),_exhausted) is _exhausted and next(g.triples((t_uri,None,None)),_exhausted) is _exhausted: + return False else: - return {"valid": True} + return True def stringmapper(thisstring): if ' ' in thisstring: