Skip to content

Commit

Permalink
remove orgtypes reconcile endpoint
Browse files Browse the repository at this point in the history
fix for queries from openrefine
  • Loading branch information
drkane committed Jan 31, 2024
1 parent 0144475 commit a9c1313
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 31 deletions.
7 changes: 0 additions & 7 deletions findthatcharity/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@
"url": "https://reconciliation-api.github.io/specs/latest/",
},
},
{
"name": "Reconciliation (against specific type of organisation)",
"externalDocs": {
"description": "Reconciliation Service API v0.2",
"url": "https://reconciliation-api.github.io/specs/latest/",
},
},
]
},
)
Expand Down
5 changes: 3 additions & 2 deletions reconcile/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from .reconcile_all import api as all_api
from .reconcile_company import api as company_api
from .reconcile_orgtype import api as orgtype_api

# from .reconcile_orgtype import api as orgtype_api

api = Router(tags=["Reconciliation"])
api.add_router("", all_api)
api.add_router("/company", company_api)
api.add_router("", orgtype_api)
# api.add_router("", orgtype_api)


__all__ = ["api"]
2 changes: 1 addition & 1 deletion reconcile/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def reconcile(
for key, query in body.queries.items():
results[key] = self.reconcile_query(
query.query,
type=query.type,
type_=self._get_orgtypes_from_str(query.type),
limit=query.limit,
properties=query.properties,
type_strict=query.type_strict,
Expand Down
14 changes: 7 additions & 7 deletions reconcile/api/reconcile_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def propose_properties(request, type: str, limit: int = 500):
return reconcile.propose_properties(request, type_=type, limit=limit)


@api.post(
"/extend",
response={200: DataExtensionQueryResponse},
exclude_none=True,
)
def data_extension(request, body: DataExtensionQuery):
return reconcile.data_extension(request, body)
# @api.post(
# "/extend",
# response={200: DataExtensionQueryResponse},
# exclude_none=True,
# )
# def data_extension(request, body: DataExtensionQuery):
# return reconcile.data_extension(request, body)
2 changes: 1 addition & 1 deletion reconcile/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ServiceSpec(Schema):

class ReconciliationQuery(Schema):
query: str
type: Optional[EntityType] = None
type: Optional[EntityType | str] = None
limit: int = 10
properties: Optional[List[QueryProperty]] = None
type_strict: Optional[Literal["should", "all", "any"]] = None
Expand Down
2 changes: 1 addition & 1 deletion reconcile/companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def do_reconcile_query(
query,
orgtypes="all",
type="/Organization",
type_="/Organization",
limit=5,
properties=[],
type_strict="should",
Expand Down
22 changes: 11 additions & 11 deletions reconcile/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from findthatcharity.jinja2 import get_orgtypes
from findthatcharity.utils import normalise_name
from ftc.documents import OrganisationGroup
from ftc.models import Organisation
from ftc.models import Organisation, OrganisationType
from ftc.models.organisation_classification import OrganisationClassification
from reconcile.utils import convert_value

Expand All @@ -22,27 +22,27 @@


def do_reconcile_query(
query,
orgtypes="all",
type="/Organization",
limit=5,
properties=[],
query: str,
orgtypes: list[OrganisationType] = [],
type_: list[OrganisationType] = [],
limit: int = 5,
properties: list[dict] = [],
type_strict="should",
result_key="result",
):
if not query:
return []

if not isinstance(orgtypes, list) and orgtypes != "all":
orgtypes = orgtypes.split("+")
if type_:
orgtypes = type_ + orgtypes

properties = {p["pid"]: p["v"] for p in properties} if properties else {}
properties_parsed = {p["pid"]: p["v"] for p in properties} if properties else {}

query_template, params = recon_query(
query,
orgtypes=orgtypes,
postcode=properties.get("postalCode"),
domain=properties.get("domain"),
postcode=properties_parsed.get("postalCode"),
domain=properties_parsed.get("domain"),
)
q = OrganisationGroup.search().update_from_dict(query_template)[:limit]
result = q.execute(params=params)
Expand Down
43 changes: 42 additions & 1 deletion reconcile/tests/test_reconcile_all_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

RECON_BASE_URLS: list[Tuple[str, list[str]]] = [
("/api/v1/reconcile/", ["0.2"]),
("/api/v1/reconcile/local-authority", ["0.2"]),
# ("/api/v1/reconcile/local-authority", ["0.2"]),
("/reconcile", ["0.1"]),
("/reconcile/local-authority", ["0.1"]),
]
Expand Down Expand Up @@ -98,6 +98,47 @@ def test_reconcile(self):
registry=self.registry,
)

# POST request to /api/v1/reconcile should return a list of candidates
def test_reconcile_with_type(self):
# attach RECON RESPONSE to the search() method of self.mock_es
self.mock_es.return_value.search.return_value = RECON_RESPONSE

for base_url, schema_version, schema in get_test_cases(
"reconciliation-result-batch.json"
):
with self.subTest((base_url, schema_version)):
response = self.client.post(
base_url,
{
"queries": json.dumps(
{
"q0": {
"query": "Test",
"type": "registered-charity",
"type_strict": "should",
},
"q1": {
"query": "Test",
"type": "registered-charity",
"type_strict": "should",
},
}
),
},
)
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(list(data.keys()), ["q0", "q1"])
self.assertEqual(len(data["q0"]["result"]), 10)
self.assertEqual(data["q0"]["result"][0]["id"], "GB-CHC-1006706")

jsonschema.validate(
instance=data,
schema=schema,
cls=jsonschema.Draft7Validator,
registry=self.registry,
)

# POST request to /api/v1/reconcile should return a list of candidates
def test_reconcile_empty(self):
# attach RECON RESPONSE to the search() method of self.mock_es
Expand Down
4 changes: 4 additions & 0 deletions reconcile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def index(request, orgtype="all"):
queries = json.loads(queries)
results = {}
for query_id, query in queries.items():
if "type" in query:
query["type_"] = query.pop("type")
if query["type_"]:
query["type_"] = [OrganisationType.objects.get(slug=query["type_"])]
results[query_id] = do_reconcile_query(**query, orgtypes=orgtypes)
return JsonResponse(results)

Expand Down

0 comments on commit a9c1313

Please sign in to comment.