Improve context passing and implement superuser override for redactions
This commit is contained in:
@@ -25,13 +25,13 @@ def initialise_opensearch():
|
||||
return client
|
||||
|
||||
|
||||
def construct_query(query, fields, results):
|
||||
def construct_query(query, fields, size):
|
||||
if not fields:
|
||||
fields = settings.OPENSEARCH_MAIN_SEARCH_FIELDS
|
||||
if not results:
|
||||
results = 5
|
||||
if not size:
|
||||
size = 5
|
||||
query = {
|
||||
"size": results,
|
||||
"size": size,
|
||||
"query": {
|
||||
"query_string": {
|
||||
"query": query,
|
||||
@@ -61,7 +61,7 @@ def construct_query(query, fields, results):
|
||||
return query
|
||||
|
||||
|
||||
def filter_blacklisted(response):
|
||||
def filter_blacklisted(user, response):
|
||||
pp.pprint(response["hits"]["hits"])
|
||||
print("LEN", len(response["hits"]["hits"]))
|
||||
response["redacted"] = 0
|
||||
@@ -79,15 +79,24 @@ def filter_blacklisted(response):
|
||||
if blacklisted_item in str(content):
|
||||
# Remove the item
|
||||
if item in response["hits"]["hits"]:
|
||||
response["hits"]["hits"].remove(item)
|
||||
if not user.is_superuser:
|
||||
response["hits"]["hits"].remove(item)
|
||||
# Let the UI know something was redacted
|
||||
response["redacted"] += 1
|
||||
response["exemption"] = True
|
||||
|
||||
|
||||
def run_main_query(client, query, fields=None, results=None):
|
||||
search_query = construct_query(query, fields, results)
|
||||
def run_main_query(client, user, query, fields=None, size=None):
|
||||
if fields:
|
||||
for field in fields:
|
||||
if field not in settings.OPENSEARCH_MAIN_SEARCH_FIELDS:
|
||||
return False
|
||||
if size:
|
||||
if size not in settings.OPENSEARCH_MAIN_SIZES:
|
||||
return False
|
||||
search_query = construct_query(query, fields, size)
|
||||
# fmt: off
|
||||
response = client.search(body=search_query,
|
||||
index=settings.OPENSEARCH_INDEX_MAIN)
|
||||
filter_blacklisted(response)
|
||||
filter_blacklisted(user, response)
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user